Skip to content

Commit 7244b48

Browse files
committed
refactor code
1 parent 4fd6690 commit 7244b48

File tree

7 files changed

+30
-30
lines changed

7 files changed

+30
-30
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@
44
#include <deque>
55
#include <unordered_map>
66

7-
#include "futex_state.h"
7+
#include "block_state.h"
88

99
struct CoroBase;
1010

11-
struct FutexQueues {
11+
struct BlockManager {
1212
// TODO(kmitkin): due to usage in as_atomic functions rewrite to custom hash
1313
// table & linked list
1414
std::unordered_map<std::uintptr_t, std::deque<CoroBase *>> queues;
1515

16-
void Push(FutexState state, CoroBase *coro) {
16+
void BlockOn(BlockState state, CoroBase *coro) {
1717
if (!queues.contains(state.addr)) {
1818
queues[state.addr] = std::deque<CoroBase *>{};
1919
}
2020
queues[state.addr].push_back(coro);
2121
}
2222

23-
bool IsBlocked(const FutexState &state, CoroBase *coro) {
23+
bool IsBlocked(const BlockState &state, CoroBase *coro) {
2424
return state.addr &&
2525
std::find(queues[state.addr].begin(), queues[state.addr].end(),
2626
coro) != queues[state.addr].end();
2727
}
2828

29-
std::size_t Pop(std::intptr_t addr, std::size_t max_wakes) {
29+
std::size_t UnblockOn(std::intptr_t addr, std::size_t max_wakes) {
3030
if (!queues.contains(addr)) [[unlikely]] {
3131
return 0;
3232
}
@@ -38,12 +38,12 @@ struct FutexQueues {
3838
return wakes;
3939
}
4040

41-
void PopAll(std::intptr_t addr) {
41+
void UnblockAllOn(std::intptr_t addr) {
4242
if (!queues.contains(addr)) {
4343
return;
4444
}
4545
queues[addr].clear();
4646
}
4747
};
4848

49-
extern FutexQueues futex_queues;
49+
extern BlockManager block_manager;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include <cstdint>
4-
struct FutexState {
4+
struct BlockState {
55
std::intptr_t addr;
66
long value;
77

runtime/include/blocking_primitives.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
#include <mutex>
33

4-
#include "futex.h"
4+
#include "block_manager.h"
55
#include "lib.h"
66
#include "verifying_macro.h"
77

@@ -27,13 +27,13 @@ struct mutex {
2727

2828
as_atomic void unlock() {
2929
locked = 0;
30-
futex_queues.PopAll(
30+
block_manager.UnblockAllOn(
3131
state.addr); // To have the ability schedule any coroutine
3232
}
3333

3434
private:
3535
int locked{0};
36-
FutexState state{reinterpret_cast<std::intptr_t>(&locked), locked};
36+
BlockState state{reinterpret_cast<std::intptr_t>(&locked), locked};
3737

3838
friend struct condition_variable;
3939
};
@@ -48,7 +48,7 @@ struct shared_mutex {
4848
}
4949
as_atomic void unlock() {
5050
locked = 0;
51-
futex_queues.PopAll(state.addr);
51+
block_manager.UnblockAllOn(state.addr);
5252
}
5353
as_atomic void lock_shared() {
5454
while (locked == -1) {
@@ -59,12 +59,12 @@ struct shared_mutex {
5959
}
6060
as_atomic void unlock_shared() {
6161
--locked;
62-
futex_queues.PopAll(state.addr);
62+
block_manager.UnblockAllOn(state.addr);
6363
}
6464

6565
private:
6666
int locked{0};
67-
FutexState state{reinterpret_cast<std::intptr_t>(&locked), locked};
67+
BlockState state{reinterpret_cast<std::intptr_t>(&locked), locked};
6868
};
6969

7070
struct condition_variable {
@@ -76,9 +76,9 @@ struct condition_variable {
7676
lock.lock();
7777
}
7878

79-
as_atomic void notify_one() { futex_queues.Pop(addr, 1); }
79+
as_atomic void notify_one() { block_manager.UnblockOn(addr, 1); }
8080

81-
as_atomic void notify_all() { futex_queues.PopAll(addr); }
81+
as_atomic void notify_all() { block_manager.UnblockAllOn(addr); }
8282

8383
private:
8484
std::intptr_t addr;

runtime/include/lib.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
#include <utility>
1010
#include <vector>
1111

12-
#include "futex.h"
12+
#include "block_manager.h"
1313

1414
#define panic() assert(false)
1515

1616
struct CoroBase;
1717

18-
struct FutexQueues;
18+
struct BlockManager;
1919

2020
// Current executing coroutine.
2121
extern std::shared_ptr<CoroBase> this_coro;
2222

2323
// Scheduler context
2424
extern boost::context::fiber_context sched_ctx;
2525

26-
extern FutexQueues futex_queues;
26+
extern BlockManager block_manager;
2727

2828
extern "C" void CoroYield();
2929

@@ -67,14 +67,14 @@ struct CoroBase : public std::enable_shared_from_this<CoroBase> {
6767
// Terminate the coroutine.
6868
void Terminate();
6969

70-
void SetBlocked(const FutexState& state) {
70+
void SetBlocked(const BlockState& state) {
7171
fstate = state;
72-
futex_queues.Push(state, this);
72+
block_manager.BlockOn(state, this);
7373
}
7474

75-
FutexState GetFutexState() { return fstate; }
75+
BlockState GetBlockState() { return fstate; }
7676

77-
bool IsBlocked() { return futex_queues.IsBlocked(fstate, this); }
77+
bool IsBlocked() { return block_manager.IsBlocked(fstate, this); }
7878

7979
// Checks if the coroutine is parked.
8080
bool IsParked() const;
@@ -99,7 +99,7 @@ struct CoroBase : public std::enable_shared_from_this<CoroBase> {
9999
// Is coroutine returned.
100100
bool is_returned{};
101101
// Futex state on which coroutine is blocked.
102-
FutexState fstate{};
102+
BlockState fstate{};
103103
// Name.
104104
std::string_view name;
105105
boost::context::fiber_context ctx;

runtime/include/pct_strategy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ struct PctStrategy : public BaseStrategyWithThreads<TargetObj, Verifier> {
3737
// debug(stderr, "prior: %d, number %d\n", priorities[i], i);
3838
if (!threads[i].empty() && threads[i].back()->IsBlocked()) {
3939
// debug(stderr, "blocked on %p val %d\n",
40-
// threads[i].back()->GetFutexState().addr,
41-
// threads[i].back()->GetFutexState().value);
40+
// threads[i].back()->GetBlockState().addr,
41+
// threads[i].back()->GetBlockState().value);
4242
// dual waiting if request finished, but follow up isn't
4343
// skip dual tasks that already have finished the request
4444
// section(follow-up will be executed in another task, so we can't

runtime/lib.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Task this_coro{};
1212

1313
boost::context::fiber_context sched_ctx;
1414

15-
FutexQueues futex_queues;
15+
BlockManager block_manager;
1616

1717
namespace ltest {
1818
std::vector<TaskBuilder> task_builders{};

syscall_intercept/hook.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <cerrno>
77

8-
#include "runtime/include/futex.h"
8+
#include "runtime/include/block_manager.h"
99
#include "runtime/include/lib.h"
1010
#include "runtime/include/logger.h"
1111
#include "runtime/include/syscall_trap.h"
@@ -25,7 +25,7 @@ static int hook(long syscall_number, long arg0, long arg1, long arg2, long arg3,
2525
(unsigned long)arg0, arg1, arg2, *((int *)arg0));
2626
arg1 = arg1 & FUTEX_CMD_MASK;
2727
if (arg1 == FUTEX_WAIT || arg1 == FUTEX_WAIT_BITSET) {
28-
auto fstate = FutexState{arg0, arg2};
28+
auto fstate = BlockState{arg0, arg2};
2929
if (fstate.CanBeBlocked()) {
3030
this_coro->SetBlocked(fstate);
3131
CoroYield();
@@ -36,7 +36,7 @@ static int hook(long syscall_number, long arg0, long arg1, long arg2, long arg3,
3636
}
3737
} else if (arg1 == FUTEX_WAKE || arg1 == FUTEX_WAKE_BITSET) {
3838
debug(stderr, "caught wake\n");
39-
*result = futex_queues.Pop(arg0, arg2);
39+
*result = block_manager.Pop(arg0, arg2);
4040
} else {
4141
assert(false && "unsupported futex call");
4242
}

0 commit comments

Comments
 (0)