1999-04-23 11:00:02 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Thread Safe Resource Manager |
|
|
|
|
+----------------------------------------------------------------------+
|
2007-01-01 09:36:18 +00:00
|
|
|
| Copyright (c) 1999-2007, Andi Gutmans, Sascha Schumann, Zeev Suraski |
|
2000-08-31 19:02:14 +00:00
|
|
|
| This source file is subject to the TSRM license, that is bundled |
|
|
|
|
| with this package in the file LICENSE |
|
1999-04-23 11:00:02 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2000-08-31 19:02:14 +00:00
|
|
|
| Authors: Zeev Suraski <zeev@zend.com> |
|
1999-04-23 11:00:02 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2000-07-02 23:16:54 +00:00
|
|
|
#ifndef TSRM_H
|
|
|
|
#define TSRM_H
|
1999-04-20 23:58:02 +00:00
|
|
|
|
2002-05-29 05:16:58 +00:00
|
|
|
/* #ifndef WIN32 */
|
2005-02-21 10:01:34 +00:00
|
|
|
#ifndef WIN32
|
2005-03-17 08:15:23 +00:00
|
|
|
# include <tsrm_config.h>
|
1999-04-24 09:01:30 +00:00
|
|
|
#endif
|
|
|
|
|
2001-07-28 12:16:43 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
# define TSRM_WIN32
|
2002-08-07 14:47:42 +00:00
|
|
|
# include "tsrm_config.w32.h"
|
2001-07-28 12:16:43 +00:00
|
|
|
#endif
|
2001-07-28 10:46:11 +00:00
|
|
|
|
|
|
|
#ifdef TSRM_WIN32
|
|
|
|
# ifdef TSRM_EXPORTS
|
|
|
|
# define TSRM_API __declspec(dllexport)
|
|
|
|
# else
|
|
|
|
# define TSRM_API __declspec(dllimport)
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define TSRM_API
|
|
|
|
#endif
|
|
|
|
|
2000-08-31 23:44:10 +00:00
|
|
|
/* Only compile multi-threading functions if we're in ZTS mode */
|
|
|
|
#ifdef ZTS
|
|
|
|
|
2000-09-02 15:03:19 +00:00
|
|
|
#ifdef TSRM_WIN32
|
2003-06-25 05:05:06 +00:00
|
|
|
# ifndef TSRM_INCLUDE_FULL_WINDOWS_HEADERS
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# endif
|
1999-04-24 09:01:30 +00:00
|
|
|
# include <windows.h>
|
2003-12-03 14:26:41 +00:00
|
|
|
# include <shellapi.h>
|
1999-12-05 21:55:43 +00:00
|
|
|
#elif defined(GNUPTH)
|
|
|
|
# include <pth.h>
|
1999-04-24 09:01:30 +00:00
|
|
|
#elif defined(PTHREADS)
|
|
|
|
# include <pthread.h>
|
2001-06-21 16:54:05 +00:00
|
|
|
#elif defined(TSRM_ST)
|
|
|
|
# include <st.h>
|
2002-10-05 11:26:17 +00:00
|
|
|
#elif defined(BETHREADS)
|
|
|
|
#include <kernel/OS.h>
|
|
|
|
#include <TLS.h>
|
1999-04-24 09:01:30 +00:00
|
|
|
#endif
|
1999-04-20 23:58:02 +00:00
|
|
|
|
|
|
|
typedef int ts_rsrc_id;
|
|
|
|
|
|
|
|
/* Define THREAD_T and MUTEX_T */
|
2000-09-02 15:03:19 +00:00
|
|
|
#ifdef TSRM_WIN32
|
1999-04-20 23:58:02 +00:00
|
|
|
# define THREAD_T DWORD
|
1999-12-20 20:02:25 +00:00
|
|
|
# define MUTEX_T CRITICAL_SECTION *
|
1999-12-05 21:55:43 +00:00
|
|
|
#elif defined(GNUPTH)
|
1999-12-30 15:52:08 +00:00
|
|
|
# define THREAD_T pth_t
|
1999-12-05 21:55:43 +00:00
|
|
|
# define MUTEX_T pth_mutex_t *
|
1999-04-20 23:58:02 +00: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-21 16:54:05 +00:00
|
|
|
#elif defined(TSRM_ST)
|
|
|
|
# define THREAD_T st_thread_t
|
|
|
|
# define MUTEX_T st_mutex_t
|
2002-10-05 11:26:17 +00:00
|
|
|
#elif defined(BETHREADS)
|
|
|
|
# define THREAD_T thread_id
|
|
|
|
typedef struct {
|
|
|
|
sem_id sem;
|
|
|
|
int32 ben;
|
|
|
|
} beos_ben;
|
|
|
|
# define MUTEX_T beos_ben *
|
1999-04-20 23:58:02 +00:00
|
|
|
#endif
|
|
|
|
|
2001-07-27 10:08:26 +00:00
|
|
|
typedef void (*ts_allocate_ctor)(void *, void ***);
|
|
|
|
typedef void (*ts_allocate_dtor)(void *, void ***);
|
1999-04-20 23:58:02 +00:00
|
|
|
|
1999-10-05 15:15:01 +00:00
|
|
|
#define THREAD_HASH_OF(thr,ts) (unsigned long)thr%(unsigned long)ts
|
1999-04-20 23:58:02 +00:00
|
|
|
|
1999-08-14 09:35:52 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
1999-04-20 23:58:02 +00:00
|
|
|
|
|
|
|
/* startup/shutdown */
|
2000-11-18 02:41:14 +00:00
|
|
|
TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_level, char *debug_filename);
|
1999-12-05 16:21:37 +00:00
|
|
|
TSRM_API void tsrm_shutdown(void);
|
1999-04-20 23:58:02 +00:00
|
|
|
|
|
|
|
/* allocates a new thread-safe-resource id */
|
2001-07-27 10:08:26 +00: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-20 23:58:02 +00:00
|
|
|
|
|
|
|
/* fetches the requested resource for the current thread */
|
2000-06-16 14:38:05 +00: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-20 23:58:02 +00:00
|
|
|
|
|
|
|
/* frees all resources allocated for the current thread */
|
1999-12-05 16:21:37 +00:00
|
|
|
TSRM_API void ts_free_thread(void);
|
1999-04-20 23:58:02 +00:00
|
|
|
|
2006-03-14 15:16:07 +00:00
|
|
|
/* frees all resources allocated for all threads except current */
|
|
|
|
void ts_free_worker_threads(void);
|
|
|
|
|
1999-04-20 23:58:02 +00:00
|
|
|
/* deallocates all occurrences of a given id */
|
1999-08-14 09:35:52 +00:00
|
|
|
TSRM_API void ts_free_id(ts_rsrc_id id);
|
1999-04-20 23:58:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Debug support */
|
2000-11-17 23:31:15 +00:00
|
|
|
#define TSRM_ERROR_LEVEL_ERROR 1
|
2000-11-18 02:41:14 +00:00
|
|
|
#define TSRM_ERROR_LEVEL_CORE 2
|
|
|
|
#define TSRM_ERROR_LEVEL_INFO 3
|
2001-07-30 01:46:35 +00:00
|
|
|
|
|
|
|
typedef void (*tsrm_thread_begin_func_t)(THREAD_T thread_id, void ***tsrm_ls);
|
|
|
|
typedef void (*tsrm_thread_end_func_t)(THREAD_T thread_id, void ***tsrm_ls);
|
|
|
|
|
|
|
|
|
2000-11-18 02:41:14 +00:00
|
|
|
TSRM_API int tsrm_error(int level, const char *format, ...);
|
|
|
|
TSRM_API void tsrm_error_set(int level, char *debug_filename);
|
1999-04-20 23:58:02 +00:00
|
|
|
|
|
|
|
/* utility functions */
|
1999-08-14 09:35:52 +00: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);
|
|
|
|
|
2001-07-30 01:46:35 +00:00
|
|
|
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);
|
1999-08-14 09:35:52 +00:00
|
|
|
|
2005-04-27 22:19:54 +00:00
|
|
|
/* these 3 APIs should only be used by people that fully understand the threading model
|
|
|
|
* used by PHP/Zend and the selected SAPI. */
|
|
|
|
TSRM_API void *tsrm_new_interpreter_context(void);
|
|
|
|
TSRM_API void *tsrm_set_interpreter_context(void *new_ctx);
|
|
|
|
TSRM_API void tsrm_free_interpreter_context(void *context);
|
|
|
|
|
2001-07-27 10:08:26 +00:00
|
|
|
#define TSRM_SHUFFLE_RSRC_ID(rsrc_id) ((rsrc_id)+1)
|
|
|
|
#define TSRM_UNSHUFFLE_RSRC_ID(rsrc_id) ((rsrc_id)-1)
|
|
|
|
|
2001-07-28 10:46:11 +00:00
|
|
|
#define TSRMLS_FETCH() void ***tsrm_ls = (void ***) ts_resource_ex(0, NULL)
|
2003-06-25 05:05:06 +00:00
|
|
|
#define TSRMLS_FETCH_FROM_CTX(ctx) void ***tsrm_ls = (void ***) ctx
|
2005-08-05 22:28:23 +00:00
|
|
|
#define TSRMLS_SET_CTX(ctx) ctx = (void ***) tsrm_ls
|
2001-07-28 10:46:11 +00:00
|
|
|
#define TSRMG(id, type, element) (((type) (*((void ***) tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(id)])->element)
|
2001-07-27 10:08:26 +00:00
|
|
|
#define TSRMLS_D void ***tsrm_ls
|
|
|
|
#define TSRMLS_DC , TSRMLS_D
|
|
|
|
#define TSRMLS_C tsrm_ls
|
|
|
|
#define TSRMLS_CC , TSRMLS_C
|
|
|
|
|
1999-08-14 09:35:52 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
1999-04-20 23:58:02 +00:00
|
|
|
|
2001-07-28 10:46:11 +00:00
|
|
|
#else /* non ZTS */
|
2001-07-28 00:46:42 +00:00
|
|
|
|
2001-07-28 10:46:11 +00:00
|
|
|
#define TSRMLS_FETCH()
|
2003-06-25 05:05:06 +00:00
|
|
|
#define TSRMLS_FETCH_FROM_CTX(ctx)
|
|
|
|
#define TSRMLS_SET_CTX(ctx)
|
2001-08-02 16:03:17 +00:00
|
|
|
#define TSRMLS_D void
|
2001-07-28 00:46:42 +00:00
|
|
|
#define TSRMLS_DC
|
|
|
|
#define TSRMLS_C
|
|
|
|
#define TSRMLS_CC
|
|
|
|
|
2000-08-31 23:44:10 +00:00
|
|
|
#endif /* ZTS */
|
|
|
|
|
2000-07-02 23:16:54 +00:00
|
|
|
#endif /* TSRM_H */
|