Skip to content

Commit

Permalink
Added a timeout to the conditional variable to wait for thread startup.
Browse files Browse the repository at this point in the history
  • Loading branch information
phabrics committed Aug 30, 2024
1 parent 68b0a84 commit d5692dd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion host/display/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ int tme_display_init(struct tme_element *element,
#ifdef TME_THREADS_FIBER
tme_thread_create(&display->tme_display_thread, tme_display_th_update, display);
#else
tme_threads_set_main(tme_display_update, display, TME_TIME_SET_USEC(16667));
tme_threads_set_main(tme_display_update, display, &display->tme_display_mutex, TME_TIME_SET_USEC(16667));
#endif

/* fill the element: */
Expand Down
22 changes: 17 additions & 5 deletions libtme/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
static struct tme_threads_t {
tme_threads_fn1 tme_threads_run;
void *tme_threads_arg;
tme_mutex_t *tme_threads_mutex;
tme_time_t tme_threads_delay;
} tme_threads;
static tme_cond_t tme_cond_start;
static tme_time_t tme_cond_delay = TME_TIME_SET_SEC(5);
#ifdef TME_THREADS_POSIX
pthread_rwlock_t tme_rwlock_suspere;

Expand Down Expand Up @@ -79,17 +81,19 @@ tme_event_t win32_stderr;
#endif

// Set main thread loop iteration function & argument
void tme_threads_set_main(tme_threads_fn1 run, void *arg, tme_time_t delay) {
void tme_threads_set_main(tme_threads_fn1 run, void *arg, tme_mutex_t *mutex, tme_time_t delay) {
tme_threads.tme_threads_run = run;
tme_threads.tme_threads_arg = arg;
tme_threads.tme_threads_mutex = mutex;
tme_threads.tme_threads_delay = delay;
tme_cond_notify(&tme_cond_start,TRUE);
// tme_cond_notify(&tme_cond_start,TRUE);
}

int tme_threads_init() {
/* initialize the threading system: */
tme_threads.tme_threads_run = tme_threads_main_iter;
tme_threads.tme_threads_arg = 0;
tme_threads.tme_threads_mutex = NULL;
tme_threads.tme_threads_delay = 0;
_tme_threads_init();

Expand Down Expand Up @@ -118,24 +122,32 @@ int tme_threads_init() {
}

void tme_threads_run(void) {
_tme_thread_suspended();
tme_thread_enter(tme_threads.tme_threads_mutex);

/* Run the main loop */
#if defined(__EMSCRIPTEN__) && defined(TME_THREADS_POSIX)
// Receives a function to call and some user data to provide it.
emscripten_request_animation_frame_loop(tme_threads.tme_threads_run, tme_threads.tme_threads_arg);
#else
if(tme_threads.tme_threads_run)
for(;;) {
if(tme_threads.tme_threads_delay) tme_thread_sleep_yield(tme_threads.tme_threads_delay, NULL);
if(tme_threads.tme_threads_delay)
tme_thread_sleep_yield(tme_threads.tme_threads_delay, NULL);
(*tme_threads.tme_threads_run)(tme_threads.tme_threads_arg);
}
else
(*(tme_threads_fn)tme_threads.tme_threads_arg)();
#endif
tme_thread_exit(tme_threads.tme_threads_mutex);
}

void tme_thread_enter(tme_mutex_t *mutex) {
_tme_thread_resumed();
tme_cond_wait_yield(&tme_cond_start, mutex);
_tme_thread_resumed();
if(mutex) {
tme_mutex_lock(mutex);
tme_cond_sleep_yield(&tme_cond_start, mutex, tme_cond_delay);
}
}

#ifdef WIN32
Expand Down
2 changes: 1 addition & 1 deletion tme/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ssize_t tme_zlib_write _TME_P((struct tme_zlib_handle *hand, const void *buf, s
typedef void (*tme_threads_fn) _TME_P((void));
typedef int (*tme_threads_fn1) _TME_P((void *));

void tme_threads_set_main _TME_P((tme_threads_fn1 run, void *arg, tme_time_t delay));
void tme_threads_set_main _TME_P((tme_threads_fn1 run, void *arg, tme_mutex_t *mutex, tme_time_t delay));
void tme_threads_run _TME_P((void));
void tme_thread_enter _TME_P((tme_mutex_t *mutex));

Expand Down
6 changes: 2 additions & 4 deletions tmesh/tmesh.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,7 @@ _tmesh_th(int *interactive)
struct tmesh_io *io;
struct _tmesh_input *input;

// tme_thread_enter(NULL);
_tme_thread_resumed();
tme_thread_enter(NULL);

/* this command may have changed the current io, so reload: */
io = _tmesh_io;
Expand Down Expand Up @@ -517,8 +516,7 @@ _tmesh_th(int *interactive)
}
}

_tme_thread_suspended();
// tme_thread_exit(NULL);
tme_thread_exit(NULL);
}

static void
Expand Down

0 comments on commit d5692dd

Please sign in to comment.