diff --git a/include/re_main.h b/include/re_main.h index 9ede43bbd..34d7ab5a4 100644 --- a/include/re_main.h +++ b/include/re_main.h @@ -4,6 +4,7 @@ * Copyright (C) 2010 Creytiv.com */ +struct re; enum { #ifndef FD_READ @@ -44,6 +45,10 @@ int re_main(re_signal_h *signalh); void re_cancel(void); int re_debug(struct re_printf *pf, void *unused); +int re_alloc(struct re **rep); +int re_thread_attach(struct re *re); +void re_thread_detach(void); + int re_thread_init(void); void re_thread_close(void); void re_thread_enter(void); diff --git a/src/main/main.c b/src/main/main.c index 55b61ac72..0e65e7806 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -133,7 +133,7 @@ static void thread_destructor(void *arg) } -static int re_alloc(struct re **rep) +int re_alloc(struct re **rep) { struct re *re; int err; @@ -1320,6 +1320,42 @@ void re_thread_leave(void) } +/** + * Attach the current thread to re context + */ +int re_thread_attach(struct re *context) +{ + struct re *re; + + if (!context) + return EINVAL; + + call_once(&flag, re_once); + + re = tss_get(key); + if (re) { + if (re != context) + return EALREADY; + return 0; + } + + tss_set(key, context); + + return 0; +} + + +/** + * Detach the current thread from re context + */ +void re_thread_detach(void) +{ + call_once(&flag, re_once); + + tss_set(key, NULL); +} + + /** * Set an external mutex for this thread *