Skip to content

Add TSRM free thread handlers #15522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions TSRM/TSRM.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ static MUTEX_T tsrm_env_mutex; /* tsrm environ mutex */
/* New thread handlers */
static tsrm_thread_begin_func_t tsrm_new_thread_begin_handler = NULL;
static tsrm_thread_end_func_t tsrm_new_thread_end_handler = NULL;
static tsrm_thread_free_begin_func_t tsrm_free_thread_begin_handler = NULL;
static tsrm_thread_free_end_func_t tsrm_free_thread_end_handler = NULL;
static tsrm_shutdown_func_t tsrm_shutdown_handler = NULL;

/* Debug support */
Expand Down Expand Up @@ -228,6 +230,8 @@ TSRM_API void tsrm_shutdown(void)
}
tsrm_new_thread_begin_handler = NULL;
tsrm_new_thread_end_handler = NULL;
tsrm_free_thread_begin_handler = NULL;
tsrm_free_thread_end_handler = NULL;
tsrm_shutdown_handler = NULL;

tsrm_reserved_pos = 0;
Expand Down Expand Up @@ -516,6 +520,10 @@ void ts_free_thread(void)

TSRM_ASSERT(!in_main_thread);

if (tsrm_free_thread_begin_handler) {
tsrm_free_thread_begin_handler(thread_id);
}

tsrm_mutex_lock(tsmm_mutex);
hash_value = THREAD_HASH_OF(thread_id, tsrm_tls_table_size);
thread_resources = tsrm_tls_table[hash_value];
Expand All @@ -538,6 +546,10 @@ void ts_free_thread(void)
thread_resources = thread_resources->next;
}
tsrm_mutex_unlock(tsmm_mutex);

if (tsrm_free_thread_end_handler) {
tsrm_free_thread_end_handler(thread_id);
}
}/*}}}*/

/* deallocates all occurrences of a given id */
Expand Down Expand Up @@ -711,6 +723,24 @@ TSRM_API void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread
}/*}}}*/


TSRM_API void *tsrm_set_free_thread_begin_handler(tsrm_thread_free_begin_func_t free_thread_begin_handler)
{/*{{{*/
void *retval = (void *) tsrm_free_thread_begin_handler;

tsrm_free_thread_begin_handler = free_thread_begin_handler;
return retval;
}/*}}}*/


TSRM_API void *tsrm_set_free_thread_end_handler(tsrm_thread_free_end_func_t free_thread_end_handler)
{/*{{{*/
void *retval = (void *) tsrm_free_thread_end_handler;

tsrm_free_thread_end_handler = free_thread_end_handler;
return retval;
}/*}}}*/


TSRM_API void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler)
{/*{{{*/
void *retval = (void *) tsrm_shutdown_handler;
Expand Down
5 changes: 4 additions & 1 deletion TSRM/TSRM.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ TSRM_API void ts_apply_for_id(ts_rsrc_id id, void (*cb)(void *));

typedef void (*tsrm_thread_begin_func_t)(THREAD_T thread_id);
typedef void (*tsrm_thread_end_func_t)(THREAD_T thread_id);
typedef void (*tsrm_thread_free_begin_func_t)(THREAD_T thread_id);
typedef void (*tsrm_thread_free_end_func_t)(THREAD_T thread_id);
typedef void (*tsrm_shutdown_func_t)(void);


TSRM_API int tsrm_error(int level, const char *format, ...);
TSRM_API void tsrm_error_set(int level, const char *debug_filename);

Expand All @@ -133,6 +134,8 @@ TSRM_API int tsrm_sigmask(int how, const sigset_t *set, sigset_t *oldset);

TSRM_API void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begin_handler);
TSRM_API void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
TSRM_API void *tsrm_set_free_thread_begin_handler(tsrm_thread_free_begin_func_t free_thread_begin_handler);
TSRM_API void *tsrm_set_free_thread_end_handler(tsrm_thread_free_end_func_t free_thread_end_handler);
TSRM_API void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler);

TSRM_API void *tsrm_get_ls_cache(void);
Expand Down
Loading