Back to index

php5  5.3.10
TSRM.h
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Thread Safe Resource Manager                                         |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1999-2011, Andi Gutmans, Sascha Schumann, Zeev Suraski |
00006    | This source file is subject to the TSRM license, that is bundled     |
00007    | with this package in the file LICENSE                                |
00008    +----------------------------------------------------------------------+
00009    | Authors:  Zeev Suraski <zeev@zend.com>                               |
00010    +----------------------------------------------------------------------+
00011 */
00012 
00013 #ifndef TSRM_H
00014 #define TSRM_H
00015 
00016 #if !defined(__CYGWIN__) && defined(WIN32)
00017 # define TSRM_WIN32
00018 # include "tsrm_config.w32.h"
00019 #else
00020 # include <tsrm_config.h>
00021 #endif
00022 
00023 #ifdef TSRM_WIN32
00024 #      ifdef TSRM_EXPORTS
00025 #             define TSRM_API __declspec(dllexport)
00026 #      else
00027 #             define TSRM_API __declspec(dllimport)
00028 #      endif
00029 #elif defined(__GNUC__) && __GNUC__ >= 4
00030 #      define TSRM_API __attribute__ ((visibility("default")))
00031 #else
00032 #      define TSRM_API
00033 #endif
00034 
00035 #ifdef _WIN64
00036 typedef __int64 tsrm_intptr_t;
00037 typedef unsigned __int64 tsrm_uintptr_t;
00038 #else
00039 typedef long tsrm_intptr_t;
00040 typedef unsigned long tsrm_uintptr_t;
00041 #endif
00042 
00043 /* Only compile multi-threading functions if we're in ZTS mode */
00044 #ifdef ZTS
00045 
00046 #ifdef TSRM_WIN32
00047 # ifndef TSRM_INCLUDE_FULL_WINDOWS_HEADERS
00048 #  define WIN32_LEAN_AND_MEAN
00049 # endif
00050 # include <windows.h>
00051 # include <shellapi.h>
00052 #elif defined(GNUPTH)
00053 # include <pth.h>
00054 #elif defined(PTHREADS)
00055 # include <pthread.h>
00056 #elif defined(TSRM_ST)
00057 # include <st.h>
00058 #elif defined(BETHREADS)
00059 #include <kernel/OS.h> 
00060 #include <TLS.h>
00061 #endif
00062 
00063 typedef int ts_rsrc_id;
00064 
00065 /* Define THREAD_T and MUTEX_T */
00066 #ifdef TSRM_WIN32
00067 # define THREAD_T DWORD
00068 # define MUTEX_T CRITICAL_SECTION *
00069 #elif defined(GNUPTH)
00070 # define THREAD_T pth_t
00071 # define MUTEX_T pth_mutex_t *
00072 #elif defined(PTHREADS)
00073 # define THREAD_T pthread_t
00074 # define MUTEX_T pthread_mutex_t *
00075 #elif defined(NSAPI)
00076 # define THREAD_T SYS_THREAD
00077 # define MUTEX_T CRITICAL
00078 #elif defined(PI3WEB)
00079 # define THREAD_T PIThread *
00080 # define MUTEX_T PISync *
00081 #elif defined(TSRM_ST)
00082 # define THREAD_T st_thread_t
00083 # define MUTEX_T st_mutex_t
00084 #elif defined(BETHREADS)
00085 # define THREAD_T thread_id
00086 typedef struct {
00087   sem_id sem;
00088   int32 ben;
00089 } beos_ben;
00090 # define MUTEX_T beos_ben * 
00091 #endif
00092 
00093 typedef void (*ts_allocate_ctor)(void *, void ***);
00094 typedef void (*ts_allocate_dtor)(void *, void ***);
00095 
00096 #define THREAD_HASH_OF(thr,ts)  (unsigned long)thr%(unsigned long)ts
00097 
00098 #ifdef __cplusplus
00099 extern "C" {
00100 #endif
00101 
00102 /* startup/shutdown */
00103 TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_level, char *debug_filename);
00104 TSRM_API void tsrm_shutdown(void);
00105 
00106 /* allocates a new thread-safe-resource id */
00107 TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor);
00108 
00109 /* fetches the requested resource for the current thread */
00110 TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id);
00111 #define ts_resource(id)                   ts_resource_ex(id, NULL)
00112 
00113 /* frees all resources allocated for the current thread */
00114 TSRM_API void ts_free_thread(void);
00115 
00116 /* frees all resources allocated for all threads except current */
00117 void ts_free_worker_threads(void);
00118 
00119 /* deallocates all occurrences of a given id */
00120 TSRM_API void ts_free_id(ts_rsrc_id id);
00121 
00122 
00123 /* Debug support */
00124 #define TSRM_ERROR_LEVEL_ERROR     1
00125 #define TSRM_ERROR_LEVEL_CORE      2
00126 #define TSRM_ERROR_LEVEL_INFO      3
00127 
00128 typedef void (*tsrm_thread_begin_func_t)(THREAD_T thread_id, void ***tsrm_ls);
00129 typedef void (*tsrm_thread_end_func_t)(THREAD_T thread_id, void ***tsrm_ls);
00130 
00131 
00132 TSRM_API int tsrm_error(int level, const char *format, ...);
00133 TSRM_API void tsrm_error_set(int level, char *debug_filename);
00134 
00135 /* utility functions */
00136 TSRM_API THREAD_T tsrm_thread_id(void);
00137 TSRM_API MUTEX_T tsrm_mutex_alloc(void);
00138 TSRM_API void tsrm_mutex_free(MUTEX_T mutexp);
00139 TSRM_API int tsrm_mutex_lock(MUTEX_T mutexp);
00140 TSRM_API int tsrm_mutex_unlock(MUTEX_T mutexp);
00141 
00142 TSRM_API void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begin_handler);
00143 TSRM_API void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
00144 
00145 /* these 3 APIs should only be used by people that fully understand the threading model
00146  * used by PHP/Zend and the selected SAPI. */
00147 TSRM_API void *tsrm_new_interpreter_context(void);
00148 TSRM_API void *tsrm_set_interpreter_context(void *new_ctx);
00149 TSRM_API void tsrm_free_interpreter_context(void *context);
00150 
00151 #define TSRM_SHUFFLE_RSRC_ID(rsrc_id)            ((rsrc_id)+1)
00152 #define TSRM_UNSHUFFLE_RSRC_ID(rsrc_id)          ((rsrc_id)-1)
00153 
00154 #define TSRMLS_FETCH()                    void ***tsrm_ls = (void ***) ts_resource_ex(0, NULL)
00155 #define TSRMLS_FETCH_FROM_CTX(ctx) void ***tsrm_ls = (void ***) ctx
00156 #define TSRMLS_SET_CTX(ctx)        ctx = (void ***) tsrm_ls
00157 #define TSRMG(id, type, element)   (((type) (*((void ***) tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(id)])->element)
00158 #define TSRMLS_D     void ***tsrm_ls
00159 #define TSRMLS_DC    , TSRMLS_D
00160 #define TSRMLS_C     tsrm_ls
00161 #define TSRMLS_CC    , TSRMLS_C
00162 
00163 #ifdef __cplusplus
00164 }
00165 #endif
00166 
00167 #else /* non ZTS */
00168 
00169 #define TSRMLS_FETCH()
00170 #define TSRMLS_FETCH_FROM_CTX(ctx)
00171 #define TSRMLS_SET_CTX(ctx)
00172 #define TSRMLS_D     void
00173 #define TSRMLS_DC
00174 #define TSRMLS_C
00175 #define TSRMLS_CC
00176 
00177 #endif /* ZTS */
00178 
00179 #endif /* TSRM_H */