Skip to content

Commit

Permalink
help valgrind to understand coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Jul 30, 2018
1 parent e466a0f commit a0ac887
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Support tinyc compiler
* Remove deprecated module (asio), please use coroutine module
* Improve memory for container
* Help valgrind to understand coroutines

### Bugs fixed

Expand Down
23 changes: 23 additions & 0 deletions src/tbox/coroutine/impl/coroutine.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
*/
#include "coroutine.h"
#include "scheduler.h"
#ifdef TB_CONFIG_VALGRIND_HAVE_VALGRIND_STACK_REGISTER
# include "valgrind/valgrind.h"
#endif

/* //////////////////////////////////////////////////////////////////////////////////////
* macros
Expand Down Expand Up @@ -139,6 +142,11 @@ tb_coroutine_t* tb_coroutine_init(tb_co_scheduler_ref_t scheduler, tb_coroutine_
coroutine->context = tb_context_make(coroutine->stackbase - stacksize, stacksize, tb_coroutine_entry);
tb_assert_and_check_break(coroutine->context);

#ifdef TB_CONFIG_VALGRIND_HAVE_VALGRIND_STACK_REGISTER
// register valgrind stack
coroutine->valgrind_stack_id = VALGRIND_STACK_REGISTER(coroutine->stackbase - stacksize, coroutine->stackbase);
#endif

// ok
ok = tb_true;

Expand Down Expand Up @@ -178,6 +186,11 @@ tb_coroutine_t* tb_coroutine_reinit(tb_coroutine_t* coroutine, tb_coroutine_func
tb_coroutine_check(coroutine);
#endif

#ifdef TB_CONFIG_VALGRIND_HAVE_VALGRIND_STACK_REGISTER
// deregister valgrind stack
VALGRIND_STACK_DEREGISTER(coroutine->valgrind_stack_id);
#endif

// remake coroutine
if (stacksize > coroutine->stacksize)
coroutine = (tb_coroutine_t*)tb_ralloc_bytes(coroutine, sizeof(tb_coroutine_t) + stacksize + sizeof(tb_uint16_t));
Expand All @@ -200,6 +213,11 @@ tb_coroutine_t* tb_coroutine_reinit(tb_coroutine_t* coroutine, tb_coroutine_func
coroutine->context = tb_context_make(coroutine->stackbase - stacksize, stacksize, tb_coroutine_entry);
tb_assert_and_check_break(coroutine->context);

#ifdef TB_CONFIG_VALGRIND_HAVE_VALGRIND_STACK_REGISTER
// re-register valgrind stack
coroutine->valgrind_stack_id = VALGRIND_STACK_REGISTER(coroutine->stackbase - stacksize, coroutine->stackbase);
#endif

// ok
ok = tb_true;

Expand Down Expand Up @@ -227,6 +245,11 @@ tb_void_t tb_coroutine_exit(tb_coroutine_t* coroutine)
tb_coroutine_check(coroutine);
#endif

#ifdef TB_CONFIG_VALGRIND_HAVE_VALGRIND_STACK_REGISTER
// deregister valgrind stack
VALGRIND_STACK_DEREGISTER(coroutine->valgrind_stack_id);
#endif

// exit it
tb_free(coroutine);
}
Expand Down
5 changes: 5 additions & 0 deletions src/tbox/coroutine/impl/coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ typedef struct __tb_coroutine_t
// the guard
tb_uint16_t guard;

#ifdef TB_CONFIG_VALGRIND_HAVE_VALGRIND_STACK_REGISTER
// the valgrind stack id, helo valgrind to understand coroutine
tb_uint_t valgrind_stack_id;
#endif

}tb_coroutine_t;

/* //////////////////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions src/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ function check_interfaces()

-- add the interfaces for systemv
add_cfuncs("systemv", nil, {"sys/sem.h", "sys/ipc.h"}, "semget", "semtimedop")

-- add the interfaces for valgrind
add_cfuncs("valgrind", nil, "valgrind/valgrind.h", "VALGRIND_STACK_REGISTER(0, 0)")
end

-- include project directories
Expand Down

0 comments on commit a0ac887

Please sign in to comment.