summaryrefslogtreecommitdiff
path: root/TSRM/TSRM.h
blob: 8674a5d0278970b96c9d1315a11d3050ccb446f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
   +----------------------------------------------------------------------+
   | Thread Safe Resource Manager                                         |
   +----------------------------------------------------------------------+
   | Copyright (c) 1998, 1999 Zeev Suraski                                |
   +----------------------------------------------------------------------+
   | This source file is subject to the Zend license, that is bundled     |
   | with this package in the file LICENSE.  If you did not receive a     |
   | copy of the Zend license, please mail us at zend@zend.com so we can  |
   | send you a copy immediately.                                         |
   +----------------------------------------------------------------------+
   | Author:  Zeev Suraski <zeev@zend.com>                                |
   +----------------------------------------------------------------------+
*/


#ifndef _TSRM_H
#define _TSRM_H

#ifdef HAVE_CONFIG_H
# include "tsrm_config.h"
#endif

#if WIN32||WINNT
# include <windows.h>
#elif defined(PTHREADS)
# include <pthread.h>
#endif

typedef int ts_rsrc_id;

#if WIN32||WINNT
#	ifdef TSRM_EXPORTS
#	define TSRM_API __declspec(dllexport)
#	else
#	define TSRM_API __declspec(dllimport)
#	endif
#else
#	define TSRM_API
#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 MUTEX_T void *
#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 *
#endif


#define THREAD_HASH_OF(thr,ts)  thr%ts


/* startup/shutdown */
TSRM_FUNC int tsrm_startup(int expected_threads, int expected_resources, int debug_status);
TSRM_FUNC 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));

/* fetches the requested resource for the current thread */
TSRM_FUNC void *ts_resource(ts_rsrc_id id);

/* frees all resources allocated for the current thread */
TSRM_FUNC void ts_free_thread();

/* deallocates all occurrences of a given id */
TSRM_FUNC void ts_free_id(ts_rsrc_id id);


/* Debug support */
TSRM_FUNC 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);

#endif /* _TSRM_H */