xref: /PHP-5.3/TSRM/TSRM.h (revision 927bf09c)
1 /*
2    +----------------------------------------------------------------------+
3    | Thread Safe Resource Manager                                         |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1999-2011, Andi Gutmans, Sascha Schumann, Zeev Suraski |
6    | This source file is subject to the TSRM license, that is bundled     |
7    | with this package in the file LICENSE                                |
8    +----------------------------------------------------------------------+
9    | Authors:  Zeev Suraski <zeev@zend.com>                               |
10    +----------------------------------------------------------------------+
11 */
12 
13 #ifndef TSRM_H
14 #define TSRM_H
15 
16 #if !defined(__CYGWIN__) && defined(WIN32)
17 # define TSRM_WIN32
18 # include "tsrm_config.w32.h"
19 #else
20 # include <tsrm_config.h>
21 #endif
22 
23 #ifdef TSRM_WIN32
24 #	ifdef TSRM_EXPORTS
25 #		define TSRM_API __declspec(dllexport)
26 #	else
27 #		define TSRM_API __declspec(dllimport)
28 #	endif
29 #elif defined(__GNUC__) && __GNUC__ >= 4
30 #	define TSRM_API __attribute__ ((visibility("default")))
31 #else
32 #	define TSRM_API
33 #endif
34 
35 #ifdef _WIN64
36 typedef __int64 tsrm_intptr_t;
37 typedef unsigned __int64 tsrm_uintptr_t;
38 #else
39 typedef long tsrm_intptr_t;
40 typedef unsigned long tsrm_uintptr_t;
41 #endif
42 
43 /* Only compile multi-threading functions if we're in ZTS mode */
44 #ifdef ZTS
45 
46 #ifdef TSRM_WIN32
47 # ifndef TSRM_INCLUDE_FULL_WINDOWS_HEADERS
48 #  define WIN32_LEAN_AND_MEAN
49 # endif
50 # include <windows.h>
51 # include <shellapi.h>
52 #elif defined(GNUPTH)
53 # include <pth.h>
54 #elif defined(PTHREADS)
55 # include <pthread.h>
56 #elif defined(TSRM_ST)
57 # include <st.h>
58 #elif defined(BETHREADS)
59 #include <kernel/OS.h>
60 #include <TLS.h>
61 #endif
62 
63 typedef int ts_rsrc_id;
64 
65 /* Define THREAD_T and MUTEX_T */
66 #ifdef TSRM_WIN32
67 # define THREAD_T DWORD
68 # define MUTEX_T CRITICAL_SECTION *
69 #elif defined(GNUPTH)
70 # define THREAD_T pth_t
71 # define MUTEX_T pth_mutex_t *
72 #elif defined(PTHREADS)
73 # define THREAD_T pthread_t
74 # define MUTEX_T pthread_mutex_t *
75 #elif defined(NSAPI)
76 # define THREAD_T SYS_THREAD
77 # define MUTEX_T CRITICAL
78 #elif defined(PI3WEB)
79 # define THREAD_T PIThread *
80 # define MUTEX_T PISync *
81 #elif defined(TSRM_ST)
82 # define THREAD_T st_thread_t
83 # define MUTEX_T st_mutex_t
84 #elif defined(BETHREADS)
85 # define THREAD_T thread_id
86 typedef struct {
87   sem_id sem;
88   int32 ben;
89 } beos_ben;
90 # define MUTEX_T beos_ben *
91 #endif
92 
93 typedef void (*ts_allocate_ctor)(void *, void ***);
94 typedef void (*ts_allocate_dtor)(void *, void ***);
95 
96 #define THREAD_HASH_OF(thr,ts)  (unsigned long)thr%(unsigned long)ts
97 
98 #ifdef __cplusplus
99 extern "C" {
100 #endif
101 
102 /* startup/shutdown */
103 TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_level, char *debug_filename);
104 TSRM_API void tsrm_shutdown(void);
105 
106 /* allocates a new thread-safe-resource id */
107 TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor);
108 
109 /* fetches the requested resource for the current thread */
110 TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id);
111 #define ts_resource(id)			ts_resource_ex(id, NULL)
112 
113 /* frees all resources allocated for the current thread */
114 TSRM_API void ts_free_thread(void);
115 
116 /* frees all resources allocated for all threads except current */
117 void ts_free_worker_threads(void);
118 
119 /* deallocates all occurrences of a given id */
120 TSRM_API void ts_free_id(ts_rsrc_id id);
121 
122 
123 /* Debug support */
124 #define TSRM_ERROR_LEVEL_ERROR	1
125 #define TSRM_ERROR_LEVEL_CORE	2
126 #define TSRM_ERROR_LEVEL_INFO	3
127 
128 typedef void (*tsrm_thread_begin_func_t)(THREAD_T thread_id, void ***tsrm_ls);
129 typedef void (*tsrm_thread_end_func_t)(THREAD_T thread_id, void ***tsrm_ls);
130 
131 
132 TSRM_API int tsrm_error(int level, const char *format, ...);
133 TSRM_API void tsrm_error_set(int level, char *debug_filename);
134 
135 /* utility functions */
136 TSRM_API THREAD_T tsrm_thread_id(void);
137 TSRM_API MUTEX_T tsrm_mutex_alloc(void);
138 TSRM_API void tsrm_mutex_free(MUTEX_T mutexp);
139 TSRM_API int tsrm_mutex_lock(MUTEX_T mutexp);
140 TSRM_API int tsrm_mutex_unlock(MUTEX_T mutexp);
141 
142 TSRM_API void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begin_handler);
143 TSRM_API void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
144 
145 /* these 3 APIs should only be used by people that fully understand the threading model
146  * used by PHP/Zend and the selected SAPI. */
147 TSRM_API void *tsrm_new_interpreter_context(void);
148 TSRM_API void *tsrm_set_interpreter_context(void *new_ctx);
149 TSRM_API void tsrm_free_interpreter_context(void *context);
150 
151 #define TSRM_SHUFFLE_RSRC_ID(rsrc_id)		((rsrc_id)+1)
152 #define TSRM_UNSHUFFLE_RSRC_ID(rsrc_id)		((rsrc_id)-1)
153 
154 #define TSRMLS_FETCH()			void ***tsrm_ls = (void ***) ts_resource_ex(0, NULL)
155 #define TSRMLS_FETCH_FROM_CTX(ctx)	void ***tsrm_ls = (void ***) ctx
156 #define TSRMLS_SET_CTX(ctx)		ctx = (void ***) tsrm_ls
157 #define TSRMG(id, type, element)	(((type) (*((void ***) tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(id)])->element)
158 #define TSRMLS_D	void ***tsrm_ls
159 #define TSRMLS_DC	, TSRMLS_D
160 #define TSRMLS_C	tsrm_ls
161 #define TSRMLS_CC	, TSRMLS_C
162 
163 #ifdef __cplusplus
164 }
165 #endif
166 
167 #else /* non ZTS */
168 
169 #define TSRMLS_FETCH()
170 #define TSRMLS_FETCH_FROM_CTX(ctx)
171 #define TSRMLS_SET_CTX(ctx)
172 #define TSRMLS_D	void
173 #define TSRMLS_DC
174 #define TSRMLS_C
175 #define TSRMLS_CC
176 
177 #endif /* ZTS */
178 
179 #endif /* TSRM_H */
180