17 #ifndef _TBB_co_context_H 18 #define _TBB_co_context_H 24 typedef LPVOID coroutine_type;
31 #pragma warning(disable:1478) 33 #pragma clang diagnostic push 34 #pragma clang diagnostic ignored "-Wdeprecated-declarations" 49 #define MAP_ANONYMOUS MAP_ANON 84 : my_state(arg ? co_suspended : co_executing)
94 __TBB_ASSERT(1 << my_state & (1 << co_suspended | 1 << co_executing), NULL);
95 if (my_state == co_suspended)
97 my_state = co_destroyed;
105 my_state = co_suspended;
130 c = CreateFiber(stack_size, co_local_wait_for_all, arg);
135 c = IsThreadAFiber() ? GetCurrentFiber() :
136 ConvertThreadToFiberEx(
nullptr, FIBER_FLAG_FLOAT_SWITCH);
143 prev_coroutine = GetCurrentFiber();
145 SwitchToFiber(new_coroutine);
156 const size_t page_aligned_stack_size = (stack_size + (REG_PAGE_SIZE - 1)) & ~(REG_PAGE_SIZE - 1);
157 const size_t protected_stack_size = page_aligned_stack_size + 2 * REG_PAGE_SIZE;
160 uintptr_t stack_ptr = (uintptr_t)mmap(NULL, protected_stack_size, PROT_NONE, MAP_PRIVATE |
MAP_ANONYMOUS |
MAP_STACK, -1, 0);
164 int err = mprotect((
void*)(stack_ptr + REG_PAGE_SIZE), page_aligned_stack_size, PROT_READ | PROT_WRITE);
168 c.
my_stack = (
void*)(stack_ptr + REG_PAGE_SIZE);
180 typedef void(*coroutine_func_t)();
181 makecontext(&c.
my_context, (coroutine_func_t)co_local_wait_for_all,
sizeof(arg) /
sizeof(
int), arg);
205 #pragma warning(pop) // 1478 warning 207 #pragma clang diagnostic pop // "-Wdeprecated-declarations"
#define __TBB_ASSERT_EX(predicate, comment)
"Extended" version is useful to suppress warnings if a variable is only used with an assert ...
void resume(co_context &target)
coroutine_type my_coroutine
void co_local_wait_for_all(void *)
void swap_coroutine(coroutine_type &prev_coroutine, coroutine_type &new_coroutine)
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
void create_coroutine(coroutine_type &c, size_t stack_size, void *arg)
co_context(size_t stack_size, void *arg)
void current_coroutine(coroutine_type &c)
static size_t default_page_size()
void destroy_coroutine(coroutine_type &c)