Skip to content

Commit 7aa71d3

Browse files
authored
make uthread internal function override cross platform (#465)
* make function override cross platform * minor
1 parent be69039 commit 7aa71d3

3 files changed

Lines changed: 54 additions & 29 deletions

File tree

async_simple/uthread/internal/thread.cc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,6 @@ thread_context::thread_context(std::function<void()> func, size_t stack_size)
131131

132132
thread_context::~thread_context() {}
133133

134-
__attribute__((weak))
135-
void stack_deleter::operator()(char* ptr) const noexcept {
136-
delete[] ptr;
137-
}
138-
139-
__attribute__((weak))
140-
stack_holder get_stack_holder(unsigned stack_size) {
141-
return stack_holder(new char[stack_size]);
142-
}
143-
144134
stack_holder thread_context::make_stack() {
145135
return get_stack_holder(stack_size_);
146136
}

async_simple/uthread/internal/thread.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,40 @@ struct stack_deleter {
4343
void operator()(char* ptr) const noexcept;
4444
};
4545
using stack_holder = std::unique_ptr<char[], stack_deleter>;
46+
using stack_deleter_fn = void (*)(char*) noexcept;
47+
using stack_allocator_fn = stack_holder (*)(unsigned);
48+
49+
inline void default_stack_deleter(char* ptr) noexcept { delete[] ptr; }
50+
51+
inline stack_holder default_get_stack_holder(unsigned stack_size) {
52+
return stack_holder(new char[stack_size]);
53+
}
54+
55+
inline stack_deleter_fn& get_stack_deleter() noexcept {
56+
static stack_deleter_fn fn = &default_stack_deleter;
57+
return fn;
58+
}
59+
60+
inline stack_allocator_fn& get_stack_allocator() noexcept {
61+
static stack_allocator_fn fn = &default_get_stack_holder;
62+
return fn;
63+
}
64+
65+
inline void set_stack_deleter(stack_deleter_fn fn) noexcept {
66+
get_stack_deleter() = fn ? fn : &default_stack_deleter;
67+
}
68+
69+
inline void set_stack_allocator(stack_allocator_fn fn) noexcept {
70+
get_stack_allocator() = fn ? fn : &default_get_stack_holder;
71+
}
72+
73+
inline void stack_deleter::operator()(char* ptr) const noexcept {
74+
get_stack_deleter()(ptr);
75+
}
76+
77+
inline stack_holder get_stack_holder(unsigned stack_size) {
78+
return get_stack_allocator()(stack_size);
79+
}
4680

4781
class thread_context {
4882
const size_t stack_size_;

async_simple/uthread/test/UthreadAllocTest.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,35 @@
99
#include "async_simple/uthread/Uthread.h"
1010

1111
using namespace std;
12+
using namespace async_simple::uthread::internal;
1213

1314
static std::atomic<unsigned> get_stack_holder_count = 0;
1415
static std::atomic<unsigned> delete_stack_holder_count = 0;
1516

16-
namespace async_simple {
17-
namespace uthread {
18-
namespace internal {
19-
20-
stack_holder get_stack_holder(unsigned stack_size) {
21-
get_stack_holder_count++;
22-
return stack_holder(new char[stack_size]);
23-
}
24-
25-
void stack_deleter::operator()(char* ptr) const noexcept {
26-
delete_stack_holder_count++;
27-
delete[] ptr;
28-
}
29-
}
30-
}
31-
}
32-
3317
namespace async_simple {
3418
namespace uthread {
3519
class UthreadAllocSwapTest : public FUTURE_TESTBASE {
3620
public:
3721
UthreadAllocSwapTest() : _executor(4) {}
38-
void caseSetUp() override {}
39-
void caseTearDown() override {}
22+
23+
void caseSetUp() override {
24+
get_stack_holder_count = 0;
25+
delete_stack_holder_count = 0;
26+
27+
set_stack_allocator([](unsigned stack_size) -> stack_holder {
28+
get_stack_holder_count++;
29+
return stack_holder(new char[stack_size]);
30+
});
31+
set_stack_deleter([](char* ptr) noexcept {
32+
delete_stack_holder_count++;
33+
delete[] ptr;
34+
});
35+
}
36+
37+
void caseTearDown() override {
38+
set_stack_allocator(nullptr);
39+
set_stack_deleter(nullptr);
40+
}
4041

4142
template <class Func>
4243
void delayedTask(Func&& func, std::size_t ms) {

0 commit comments

Comments
 (0)