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 *));
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);
/* Shutdown TSRM (call once for the entire process) */
-TSRM_FUNC void tsrm_shutdown()
+TSRM_API void tsrm_shutdown()
{
int i;
/* 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;
{
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;
resource_types_table[i].ctor((*thread_resources_ptr)->storage[i]);
}
}
+ if (tsrm_new_thread_end_handler) {
+ tsrm_new_thread_end_handler(thread_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();
/* Allocate a mutex */
-TSRM_FUNC MUTEX_T tsrm_mutex_alloc( void )
+TSRM_API MUTEX_T tsrm_mutex_alloc( void )
{
MUTEX_T mutexp;
/* 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
/* 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
/* 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
}
+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
*/
#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
#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 */