Skip to content

Commit

Permalink
main: Add support for external threads attaching/detaching re context.
Browse files Browse the repository at this point in the history
This commit allows to create an event loop context and keep a reference
to it in the caller application. After that, this and other threads
are allowed to attach to and detach from this context as needed. Threads
are synchronized by calling re_thread_enter/leave. The re context can
be destroyed by calling mem_deref.

This is useful to allow threads that are not running event loop to
invoke methods on an event loop context that is created and managed by
another thread.
  • Loading branch information
Lastique committed Jun 24, 2022

Verified

This commit was signed with the committer’s verified signature.
sanjayankur31 Ankur Sinha
1 parent 7b28227 commit 0f7cac1
Showing 2 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions include/re_main.h
Original file line number Diff line number Diff line change
@@ -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_dettach(void);

int re_thread_init(void);
void re_thread_close(void);
void re_thread_enter(void);
40 changes: 39 additions & 1 deletion src/main/main.c
Original file line number Diff line number Diff line change
@@ -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,44 @@ void re_thread_leave(void)
}


/**
* Attach the current thread to re context
*/
int re_thread_attach(struct re *context)
{
struct re *re;

if (context == NULL) {
re_thread_dettach();
return 0;
}

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_dettach(void)
{
call_once(&flag, re_once);

tss_set(key, NULL);
}


/**
* Set an external mutex for this thread
*

0 comments on commit 0f7cac1

Please sign in to comment.