From 7c1660e415140a105e380abce03afb30342dc6c8 Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Fri, 8 Apr 2022 08:10:14 +0800 Subject: [PATCH] optimize share stack mode for fiber --- lib_fiber/c/src/fiber/fiber_unix.c | 60 +++++++++++++++-------------- lib_fiber/samples/fiber/valgrind.sh | 2 +- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/lib_fiber/c/src/fiber/fiber_unix.c b/lib_fiber/c/src/fiber/fiber_unix.c index ea4a18f58..f1b30cdf7 100644 --- a/lib_fiber/c/src/fiber/fiber_unix.c +++ b/lib_fiber/c/src/fiber/fiber_unix.c @@ -76,37 +76,27 @@ static void swap_fcontext(FIBER_UNIX *from, FIBER_UNIX *to) static void fiber_stack_save(FIBER_UNIX *curr, const char *stack_top) { - // If the fiber isn't in exiting status, and not the origin fiber, - // the fiber's running stack should be copied from the shared running - // stack to the fiber's private memory. - if (curr->fiber.status != FIBER_STATUS_EXITING && curr->fiber.id > 0) { - curr->dlen = fiber_share_stack_bottom() - stack_top; - if (curr->dlen > curr->size) { - stack_free(curr->buff); - curr->buff = (char *) stack_alloc(curr->dlen); - curr->size = curr->dlen; - } - memcpy(curr->buff, stack_top, curr->dlen); + curr->dlen = fiber_share_stack_bottom() - stack_top; + if (curr->dlen > curr->size) { + stack_free(curr->buff); + curr->buff = (char *) stack_alloc(curr->dlen); + curr->size = curr->dlen; } + memcpy(curr->buff, stack_top, curr->dlen); } -static void fiber_stack_restore(void) +static void fiber_stack_restore(FIBER_UNIX *curr) { - FIBER_UNIX *curr = (FIBER_UNIX *) acl_fiber_running(); - - if (curr->fiber.oflag & ACL_FIBER_ATTR_SHARE_STACK - && curr->fiber.status != FIBER_STATUS_EXITING) { - // After coming back, the current fiber's stack should be - // restored and copied from its private memory to the shared - // stack running memory. - if (curr->dlen > 0) { - char *bottom = fiber_share_stack_bottom(); - memcpy(bottom - curr->dlen, curr->buff, curr->dlen); - fiber_share_stack_set_dlen(curr->dlen); - } - // else if from->dlen == 0, the fiber must be the origin fiber - // that its fiber id should be 0. + // After coming back, the current fiber's stack should be + // restored and copied from its private memory to the shared + // stack running memory. + if (curr->dlen > 0) { + char *bottom = fiber_share_stack_bottom(); + memcpy(bottom - curr->dlen, curr->buff, curr->dlen); + fiber_share_stack_set_dlen(curr->dlen); } + // else if from->dlen == 0, the fiber must be the origin fiber + // that its fiber id should be 0. } #endif @@ -115,8 +105,14 @@ static void fiber_unix_swap(FIBER_UNIX *from, FIBER_UNIX *to) { // The shared stack mode isn't supported in USE_BOOST_JMP current, // which may be supported in future. + // If the fiber isn't in exiting status, and not the origin fiber, + // the fiber's running stack should be copied from the shared running + // stack to the fiber's private memory. #if defined(SHARE_STACK) - if (from->fiber.oflag & ACL_FIBER_ATTR_SHARE_STACK) { + if (from->fiber.oflag & ACL_FIBER_ATTR_SHARE_STACK + && from->fiber.status != FIBER_STATUS_EXITING + && from->fiber.id > 0) { + char stack_top = 0; fiber_stack_save(from, &stack_top); } @@ -148,7 +144,15 @@ static void fiber_unix_swap(FIBER_UNIX *from, FIBER_UNIX *to) #endif #if defined(SHARE_STACK) - fiber_stack_restore(); + { + FIBER_UNIX *curr = (FIBER_UNIX *) acl_fiber_running(); + + if (curr->fiber.oflag & ACL_FIBER_ATTR_SHARE_STACK + && curr->fiber.status != FIBER_STATUS_EXITING) { + + fiber_stack_restore(curr); + } + } #endif } diff --git a/lib_fiber/samples/fiber/valgrind.sh b/lib_fiber/samples/fiber/valgrind.sh index 3b0c04315..bb972305d 100755 --- a/lib_fiber/samples/fiber/valgrind.sh +++ b/lib_fiber/samples/fiber/valgrind.sh @@ -1,4 +1,4 @@ #!/bin/sh #valgrind --tool=memcheck --leak-check=yes --leak-check=full --show-reachable=yes --max-stackframe=3426305034400000 -v ./fiber -n 10 -m 20 -valgrind --tool=memcheck --leak-check=yes --leak-check=full --show-reachable=yes -v ./fiber -c 2 -n 2 -S +valgrind --tool=memcheck --leak-check=yes --leak-check=full --show-reachable=yes -v ./fiber -c 100 -n 100 -S