1999-04-23 19:00:02 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Thread Safe Resource Manager |
|
|
|
|
+----------------------------------------------------------------------+
|
2000-09-01 03:02:14 +08:00
|
|
|
| Copyright (c) 1999, 2000, Andi Gutmans, Sascha Schumann, Zeev Suraski|
|
|
|
|
| This source file is subject to the TSRM license, that is bundled |
|
|
|
|
| with this package in the file LICENSE |
|
1999-04-23 19:00:02 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-09-01 03:02:14 +08:00
|
|
|
| Authors: Zeev Suraski <zeev@zend.com> |
|
1999-04-23 19:00:02 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2000-07-03 07:16:54 +08:00
|
|
|
#ifndef TSRM_H
|
|
|
|
#define TSRM_H
|
1999-04-21 07:58:02 +08:00
|
|
|
|
1999-04-25 00:14:18 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
1999-04-25 07:22:09 +08:00
|
|
|
# include "tsrm_config.h"
|
1999-04-24 17:01:30 +08:00
|
|
|
#endif
|
|
|
|
|
2000-09-01 07:44:10 +08:00
|
|
|
/* Only compile multi-threading functions if we're in ZTS mode */
|
|
|
|
#ifdef ZTS
|
|
|
|
|
2000-09-03 00:28:50 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
# define TSRM_WIN32
|
|
|
|
#endif
|
|
|
|
|
2000-09-02 23:03:19 +08:00
|
|
|
#ifdef TSRM_WIN32
|
1999-04-24 17:01:30 +08:00
|
|
|
# include <windows.h>
|
1999-12-06 05:55:43 +08:00
|
|
|
#elif defined(GNUPTH)
|
|
|
|
# include <pth.h>
|
1999-04-24 17:01:30 +08:00
|
|
|
#elif defined(PTHREADS)
|
|
|
|
# include <pthread.h>
|
2001-06-22 00:54:05 +08:00
|
|
|
#elif defined(TSRM_ST)
|
|
|
|
# include <st.h>
|
1999-04-24 17:01:30 +08:00
|
|
|
#endif
|
1999-04-21 07:58:02 +08:00
|
|
|
|
|
|
|
typedef int ts_rsrc_id;
|
|
|
|
|
2000-09-02 23:03:19 +08:00
|
|
|
#ifdef TSRM_WIN32
|
1999-04-29 02:35:42 +08:00
|
|
|
# ifdef TSRM_EXPORTS
|
|
|
|
# define TSRM_API __declspec(dllexport)
|
|
|
|
# else
|
|
|
|
# define TSRM_API __declspec(dllimport)
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define TSRM_API
|
|
|
|
#endif
|
|
|
|
|
1999-04-21 07:58:02 +08:00
|
|
|
|
|
|
|
/* Define THREAD_T and MUTEX_T */
|
2000-09-02 23:03:19 +08:00
|
|
|
#ifdef TSRM_WIN32
|
1999-04-21 07:58:02 +08:00
|
|
|
# define THREAD_T DWORD
|
1999-12-21 04:02:25 +08:00
|
|
|
# define MUTEX_T CRITICAL_SECTION *
|
1999-12-06 05:55:43 +08:00
|
|
|
#elif defined(GNUPTH)
|
1999-12-30 23:52:08 +08:00
|
|
|
# define THREAD_T pth_t
|
1999-12-06 05:55:43 +08:00
|
|
|
# define MUTEX_T pth_mutex_t *
|
1999-04-21 07:58:02 +08:00
|
|
|
#elif defined(PTHREADS)
|
|
|
|
# define THREAD_T pthread_t
|
|
|
|
# define MUTEX_T pthread_mutex_t *
|
|
|
|
#elif defined(NSAPI)
|
|
|
|
# define THREAD_T SYS_THREAD
|
|
|
|
# define MUTEX_T CRITICAL
|
|
|
|
#elif defined(PI3WEB)
|
|
|
|
# define THREAD_T PIThread *
|
|
|
|
# define MUTEX_T PISync *
|
2001-06-22 00:54:05 +08:00
|
|
|
#elif defined(TSRM_ST)
|
|
|
|
# define THREAD_T st_thread_t
|
|
|
|
# define MUTEX_T st_mutex_t
|
1999-04-21 07:58:02 +08:00
|
|
|
#endif
|
|
|
|
|
2001-07-27 18:08:26 +08:00
|
|
|
typedef void (*ts_allocate_ctor)(void *, void ***);
|
|
|
|
typedef void (*ts_allocate_dtor)(void *, void ***);
|
1999-04-21 07:58:02 +08:00
|
|
|
|
1999-10-05 23:15:01 +08:00
|
|
|
#define THREAD_HASH_OF(thr,ts) (unsigned long)thr%(unsigned long)ts
|
1999-04-21 07:58:02 +08:00
|
|
|
|
1999-08-14 17:35:52 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
1999-04-21 07:58:02 +08:00
|
|
|
|
|
|
|
/* startup/shutdown */
|
2000-11-18 10:41:14 +08:00
|
|
|
TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_level, char *debug_filename);
|
1999-12-06 00:21:37 +08:00
|
|
|
TSRM_API void tsrm_shutdown(void);
|
1999-04-21 07:58:02 +08:00
|
|
|
|
|
|
|
/* allocates a new thread-safe-resource id */
|
2001-07-27 18:08:26 +08:00
|
|
|
TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor);
|
1999-04-21 07:58:02 +08:00
|
|
|
|
|
|
|
/* fetches the requested resource for the current thread */
|
2000-06-16 22:38:05 +08:00
|
|
|
TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id);
|
|
|
|
#define ts_resource(id) ts_resource_ex(id, NULL)
|
1999-04-21 07:58:02 +08:00
|
|
|
|
|
|
|
/* frees all resources allocated for the current thread */
|
1999-12-06 00:21:37 +08:00
|
|
|
TSRM_API void ts_free_thread(void);
|
1999-04-21 07:58:02 +08:00
|
|
|
|
|
|
|
/* deallocates all occurrences of a given id */
|
1999-08-14 17:35:52 +08:00
|
|
|
TSRM_API void ts_free_id(ts_rsrc_id id);
|
1999-04-21 07:58:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Debug support */
|
2000-11-18 07:31:15 +08:00
|
|
|
#define TSRM_ERROR_LEVEL_ERROR 1
|
2000-11-18 10:41:14 +08:00
|
|
|
#define TSRM_ERROR_LEVEL_CORE 2
|
|
|
|
#define TSRM_ERROR_LEVEL_INFO 3
|
|
|
|
TSRM_API int tsrm_error(int level, const char *format, ...);
|
|
|
|
TSRM_API void tsrm_error_set(int level, char *debug_filename);
|
1999-04-21 07:58:02 +08:00
|
|
|
|
|
|
|
/* utility functions */
|
1999-08-14 17:35:52 +08:00
|
|
|
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));
|
|
|
|
|
2001-07-27 18:08:26 +08:00
|
|
|
#define TSRM_SHUFFLE_RSRC_ID(rsrc_id) ((rsrc_id)+1)
|
|
|
|
#define TSRM_UNSHUFFLE_RSRC_ID(rsrc_id) ((rsrc_id)-1)
|
|
|
|
|
|
|
|
#define TSRMLS_FETCH() void ***tsrm_ls = ts_resource_ex(0, NULL)
|
|
|
|
#define TSRMG(id, type, element) (((type) (*tsrm_ls)[TSRM_UNSHUFFLE_RSRC_ID(id)])->element)
|
|
|
|
#define TSRMLS_D void ***tsrm_ls
|
|
|
|
#define TSRMLS_DC , TSRMLS_D
|
|
|
|
#define TSRMLS_C tsrm_ls
|
|
|
|
#define TSRMLS_CC , TSRMLS_C
|
|
|
|
|
1999-08-14 17:35:52 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
1999-04-21 07:58:02 +08:00
|
|
|
|
2000-09-01 07:44:10 +08:00
|
|
|
#endif /* ZTS */
|
|
|
|
|
2000-07-03 07:16:54 +08:00
|
|
|
#endif /* TSRM_H */
|