Skip to content

Commit

Permalink
feat(fibers): Add GetStackMargin method
Browse files Browse the repository at this point in the history
  • Loading branch information
BagritsevichStepan committed Jan 15, 2025
1 parent 42a54c9 commit fdd3ae4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions util/fibers/detail/fiber_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ ctx::fiber_context FiberInterface::Terminate() {
return scheduler_->Preempt();
}

size_t FiberInterface::GetStackMargin(const void* stack_address) const {
return static_cast<const uint8_t*>(stack_address) - stack_bottom_;
}

void FiberInterface::CheckStackMargin() {
uint32_t check_margin = absl::GetFlag(FLAGS_fiber_safety_margin);
if (check_margin == 0)
Expand Down
4 changes: 3 additions & 1 deletion util/fibers/detail/fiber_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ using FI_SleepHook =

class Scheduler;


class FiberInterface {
friend class Scheduler;

Expand Down Expand Up @@ -222,6 +221,8 @@ class FiberInterface {

void CheckStackMargin();

size_t GetStackMargin(const void* stack_address) const;

protected:
static constexpr uint16_t kTerminatedBit = 0x1;
static constexpr uint16_t kBusyBit = 0x2;
Expand Down Expand Up @@ -260,6 +261,7 @@ class FiberInterface {
char name_[24];
uint32_t stack_size_ = 0;
uint8_t* stack_bottom_ = nullptr;

private:
#ifndef NDEBUG
std::function<std::string()> stacktrace_print_cb_;
Expand Down
11 changes: 8 additions & 3 deletions util/fibers/fibers.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,19 @@ inline std::string_view GetName() {
return fb2::detail::FiberActive()->name();
}

/* Returns the margin between the provided stack address
and the bottom of the fiber's stack. */
inline uint32_t GetStackMargin(const void* stack_address) {
return fb2::detail::FiberActive()->GetStackMargin(stack_address);
}

inline void CheckSafetyMargin() {
fb2::detail::FiberActive()->CheckStackMargin();
}

class PrintLocalsCallback {
public:
template<typename Fn>
PrintLocalsCallback(Fn&& fn) {
public:
template <typename Fn> PrintLocalsCallback(Fn&& fn) {
fb2::detail::FiberActive()->SetPrintStacktraceCb(std::forward<Fn>(fn));
}

Expand Down

0 comments on commit fdd3ae4

Please sign in to comment.