Skip to content

Commit

Permalink
使用按需增长的栈。
Browse files Browse the repository at this point in the history
之前为了简化实现,使用了 malloc 分配栈。现在改咯。用正统的设计了。
  • Loading branch information
microcai committed Dec 17, 2024
1 parent d104275 commit 6a1ae9d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion µasync/include/universal_fiber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
#include <setjmp.h>
#endif

#ifdef __linux__
#include <sys/mman.h>
#endif

#include <exception>
#include <atomic>
#include <cassert>
Expand Down Expand Up @@ -97,12 +101,20 @@ struct FiberContextAlloctor
{
FiberContext* allocate()
{
#ifdef __linux__
return (FiberContext*) mmap(0, sizeof(FiberContext), PROT_READ|PROT_WRITE, MAP_GROWSDOWN|MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0);
#else
return (FiberContext*) malloc(sizeof(FiberContext));
#endif
}

void deallocate(FiberContext* ctx)
{
#ifdef __linux__
munmap(ctx, sizeof(FiberContext));
#else
free(ctx);
#endif
}
};

Expand Down Expand Up @@ -393,6 +405,7 @@ static inline void __coroutine_entry_point(FiberContext* ctx)

getcontext(&helper_ctx);
typedef void (*__func)(void);
typedef void (*__func_arg1)(FiberContext*);

alignas(64) static thread_local char helper_stack[256];

Expand All @@ -401,7 +414,7 @@ static inline void __coroutine_entry_point(FiberContext* ctx)
helper_ctx.uc_stack.ss_size = 256;
helper_ctx.uc_link = __current_yield_ctx; // self_ctx->uc_link;

makecontext(&helper_ctx, [](void* ctx){FiberContextAlloctor{}.deallocate((FiberContext*)ctx); }, 1, ctx);
makecontext(&helper_ctx, (__func) (__func_arg1) [](FiberContext* ctx){FiberContextAlloctor{}.deallocate(ctx); }, 1, ctx);
setcontext(&helper_ctx);
#else
__please_delete_me = ctx;
Expand Down

0 comments on commit 6a1ae9d

Please sign in to comment.