summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--TSRM/TSRM.c48
-rw-r--r--TSRM/TSRM.h42
2 files changed, 62 insertions, 28 deletions
diff --git a/TSRM/TSRM.c b/TSRM/TSRM.c
index a7e9e640aa..8178d37035 100644
--- a/TSRM/TSRM.c
+++ b/TSRM/TSRM.c
@@ -51,13 +51,17 @@ static int resource_types_table_size;
static MUTEX_T tsmm_mutex; /* thread-safe memory manager mutex */
+/* New thread handlers */
+static void (*tsrm_new_thread_begin_handler)();
+static void (*tsrm_new_thread_end_handler)();
+
/* Debug support */
static int tsrm_debug(const char *format, ...);
static int tsrm_debug_status;
/* Startup TSRM (call once for the entire process) */
-TSRM_FUNC int tsrm_startup(int expected_threads, int expected_resources, int debug_status)
+TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_status)
{
tsrm_tls_table_size = expected_threads;
tsrm_tls_table = (tsrm_tls_entry **) calloc(tsrm_tls_table_size, sizeof(tsrm_tls_entry *));
@@ -75,6 +79,8 @@ TSRM_FUNC int tsrm_startup(int expected_threads, int expected_resources, int deb
tsmm_mutex = tsrm_mutex_alloc();
+ tsrm_new_thread_begin_handler = tsrm_new_thread_end_handler = NULL;
+
tsrm_debug_status = debug_status;
tsrm_debug("Started up TSRM, %d expected threads, %d expected resources\n", expected_threads, expected_resources);
@@ -83,7 +89,7 @@ TSRM_FUNC int tsrm_startup(int expected_threads, int expected_resources, int deb
/* Shutdown TSRM (call once for the entire process) */
-TSRM_FUNC void tsrm_shutdown()
+TSRM_API void tsrm_shutdown()
{
int i;
@@ -114,7 +120,7 @@ TSRM_FUNC void tsrm_shutdown()
/* allocates a new thread-safe-resource id */
-TSRM_FUNC ts_rsrc_id ts_allocate_id(size_t size, void (*ctor)(void *resource), void (*dtor)(void *resource))
+TSRM_API ts_rsrc_id ts_allocate_id(size_t size, void (*ctor)(void *resource), void (*dtor)(void *resource))
{
ts_rsrc_id new_id;
int i;
@@ -170,6 +176,10 @@ static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_
{
int i;
+ if (tsrm_new_thread_begin_handler) {
+ tsrm_new_thread_begin_handler(thread_id);
+ }
+
(*thread_resources_ptr) = (tsrm_tls_entry *) malloc(sizeof(tsrm_tls_entry));
(*thread_resources_ptr)->storage = (void **) malloc(sizeof(void *)*id_count);
(*thread_resources_ptr)->count = id_count;
@@ -181,6 +191,9 @@ static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_
resource_types_table[i].ctor((*thread_resources_ptr)->storage[i]);
}
}
+ if (tsrm_new_thread_end_handler) {
+ tsrm_new_thread_end_handler(thread_id);
+ }
}
@@ -278,7 +291,7 @@ void ts_free_id(ts_rsrc_id id)
*/
/* Obtain the current thread id */
-TSRM_FUNC THREAD_T tsrm_thread_id(void)
+TSRM_API THREAD_T tsrm_thread_id(void)
{
#ifdef WIN32
return GetCurrentThreadId();
@@ -293,7 +306,7 @@ TSRM_FUNC THREAD_T tsrm_thread_id(void)
/* Allocate a mutex */
-TSRM_FUNC MUTEX_T tsrm_mutex_alloc( void )
+TSRM_API MUTEX_T tsrm_mutex_alloc( void )
{
MUTEX_T mutexp;
@@ -315,7 +328,7 @@ TSRM_FUNC MUTEX_T tsrm_mutex_alloc( void )
/* Free a mutex */
-TSRM_FUNC void tsrm_mutex_free( MUTEX_T mutexp )
+TSRM_API void tsrm_mutex_free( MUTEX_T mutexp )
{
if (mutexp) {
#ifdef WIN32
@@ -335,7 +348,7 @@ TSRM_FUNC void tsrm_mutex_free( MUTEX_T mutexp )
/* Lock a mutex */
-TSRM_FUNC int tsrm_mutex_lock( MUTEX_T mutexp )
+TSRM_API int tsrm_mutex_lock( MUTEX_T mutexp )
{
//tsrm_debug("Mutex locked thread: %ld\n",tsrm_thread_id());
#ifdef WIN32
@@ -351,7 +364,7 @@ TSRM_FUNC int tsrm_mutex_lock( MUTEX_T mutexp )
/* Unlock a mutex */
-TSRM_FUNC int tsrm_mutex_unlock( MUTEX_T mutexp )
+TSRM_API int tsrm_mutex_unlock( MUTEX_T mutexp )
{
//tsrm_debug("Mutex unlocked thread: %ld\n",tsrm_thread_id());
#ifdef WIN32
@@ -366,6 +379,25 @@ TSRM_FUNC int tsrm_mutex_unlock( MUTEX_T mutexp )
}
+TSRM_API void *tsrm_set_new_thread_begin_handler(void (*new_thread_begin_handler)(THREAD_T thread_id))
+{
+ void *retval = (void *) tsrm_new_thread_begin_handler;
+
+ tsrm_new_thread_begin_handler = new_thread_begin_handler;
+ return retval;
+}
+
+
+TSRM_API void *tsrm_set_new_thread_end_handler(void (*new_thread_end_handler)(THREAD_T thread_id))
+{
+ void *retval = (void *) tsrm_new_thread_end_handler;
+
+ tsrm_new_thread_end_handler = new_thread_end_handler;
+ return retval;
+}
+
+
+
/*
* Debug support
*/
diff --git a/TSRM/TSRM.h b/TSRM/TSRM.h
index 8674a5d027..6addd6c230 100644
--- a/TSRM/TSRM.h
+++ b/TSRM/TSRM.h
@@ -40,14 +40,6 @@ typedef int ts_rsrc_id;
#endif
-/* Define TSRM_FUNC */
-#ifdef __cplusplus
-#define TSRM_FUNC extern "C" TSRM_API
-#else
-#define TSRM_FUNC TSRM_API
-#endif
-
-
/* Define THREAD_T and MUTEX_T */
#if defined(WIN32)
# define THREAD_T DWORD
@@ -66,32 +58,42 @@ typedef int ts_rsrc_id;
#define THREAD_HASH_OF(thr,ts) thr%ts
+#ifdef __cplusplus
+extern "C" {
+#endif
/* startup/shutdown */
-TSRM_FUNC int tsrm_startup(int expected_threads, int expected_resources, int debug_status);
-TSRM_FUNC void tsrm_shutdown();
+TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_status);
+TSRM_API void tsrm_shutdown();
/* allocates a new thread-safe-resource id */
-TSRM_FUNC ts_rsrc_id ts_allocate_id(size_t size, void (*ctor)(void *resource), void (*dtor)(void *resource));
+TSRM_API ts_rsrc_id ts_allocate_id(size_t size, void (*ctor)(void *resource), void (*dtor)(void *resource));
/* fetches the requested resource for the current thread */
-TSRM_FUNC void *ts_resource(ts_rsrc_id id);
+TSRM_API void *ts_resource(ts_rsrc_id id);
/* frees all resources allocated for the current thread */
-TSRM_FUNC void ts_free_thread();
+TSRM_API void ts_free_thread();
/* deallocates all occurrences of a given id */
-TSRM_FUNC void ts_free_id(ts_rsrc_id id);
+TSRM_API void ts_free_id(ts_rsrc_id id);
/* Debug support */
-TSRM_FUNC void tsrm_debug_set(int status);
+TSRM_API void tsrm_debug_set(int status);
/* utility functions */
-TSRM_FUNC THREAD_T tsrm_thread_id(void);
-TSRM_FUNC MUTEX_T tsrm_mutex_alloc(void);
-TSRM_FUNC void tsrm_mutex_free(MUTEX_T mutexp);
-TSRM_FUNC int tsrm_mutex_lock(MUTEX_T mutexp);
-TSRM_FUNC int tsrm_mutex_unlock(MUTEX_T mutexp);
+TSRM_API THREAD_T tsrm_thread_id(void);
+TSRM_API MUTEX_T tsrm_mutex_alloc(void);
+TSRM_API void tsrm_mutex_free(MUTEX_T mutexp);
+TSRM_API int tsrm_mutex_lock(MUTEX_T mutexp);
+TSRM_API int tsrm_mutex_unlock(MUTEX_T mutexp);
+
+TSRM_API void *tsrm_set_new_thread_begin_handler(void (*new_thread_begin_handler)(THREAD_T thread_id));
+TSRM_API void *tsrm_set_new_thread_end_handler(void (*new_thread_end_handler)(THREAD_T thread_id));
+
+#ifdef __cplusplus
+}
+#endif
#endif /* _TSRM_H */