4443c3eae43b30dd2bc64aeb86bd843524dad638
[ira/wip.git] / lib / util / smb_threads.h
1 /*
2         Unix SMB/CIFS implementation.
3         SMB thread interface functions.
4         Copyright (C) Jeremy Allison, 2009.
5
6         This program is free software; you can redistribute it and/or modify
7         it under the terms of the GNU General Public License as published by
8         the Free Software Foundation; either version 3 of the License, or
9         (at your option) any later version.
10
11         This program is distributed in the hope that it will be useful,
12         but WITHOUT ANY WARRANTY; without even the implied warranty of
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14         GNU General Public License for more details.
15
16         You should have received a copy of the GNU General Public License
17         along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef _smb_threads_h_
21 #define _smb_threads_h_
22
23 /* Data types needed for smb_thread_once call. */
24
25 #if defined(HAVE_PTHREAD_H)
26 #include <pthread.h>
27 #define smb_thread_once_t pthread_once_t
28 #define SMB_THREAD_ONCE_INIT PTHREAD_ONCE_INIT
29 #define SMB_THREAD_ONCE_IS_INITIALIZED(val) (true)
30 #define SMB_THREAD_ONCE_INITIALIZE(val)
31 #else
32 #define smb_thread_once_t bool
33 #define SMB_THREAD_ONCE_INIT false
34 #define SMB_THREAD_ONCE_IS_INITIALIZED(val) ((val) == true)
35 #define SMB_THREAD_ONCE_INITIALIZE(val) ((val) = true)
36 #endif
37
38 enum smb_thread_lock_type {
39         SMB_THREAD_LOCK = 1,
40         SMB_THREAD_UNLOCK
41 };
42
43 struct smb_thread_functions {
44         /* Mutex and tls functions. */
45         int (*create_mutex)(const char *lockname,
46                         void **pplock,
47                         const char *location);
48         void (*destroy_mutex)(void *plock,
49                         const char *location);
50         int (*lock_mutex)(void *plock, enum smb_thread_lock_type lock_type,
51                         const char *location);
52
53         /* Once initialization. */
54         int (*smb_thread_once)(smb_thread_once_t *p_once, void (*init_fn)(void));
55
56         /* Thread local storage. */
57         int (*create_tls)(const char *keyname,
58                         void **ppkey,
59                         const char *location);
60         void (*destroy_tls)(void **pkey,
61                         const char *location);
62         int (*set_tls)(void *pkey, const void *pval, const char *location);
63         void *(*get_tls)(void *pkey, const char *location);
64 };
65
66 int smb_thread_set_functions(const struct smb_thread_functions *tf);
67
68 extern const struct smb_thread_functions *global_tfp;
69
70 /* Define the pthread version of the functions. */
71
72 #define SMB_THREADS_DEF_PTHREAD_IMPLEMENTATION(tf) \
73  \
74 static int smb_create_mutex_pthread(const char *lockname, void **pplock, const char *location) \
75 { \
76         pthread_mutex_t *pmut = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); \
77         if (!pmut) { \
78                 return ENOMEM; \
79         } \
80         pthread_mutex_init(pmut, NULL); \
81         *pplock = (void *)pmut; \
82         return 0; \
83 } \
84  \
85 static void smb_destroy_mutex_pthread(void *plock, const char *location) \
86 { \
87         pthread_mutex_destroy((pthread_mutex_t *)plock); \
88         free(plock); \
89 } \
90  \
91 static int smb_lock_pthread(void *plock, enum smb_thread_lock_type lock_type, const char *location) \
92 { \
93         if (lock_type == SMB_THREAD_UNLOCK) { \
94                 return pthread_mutex_unlock((pthread_mutex_t *)plock); \
95         } else { \
96                 return pthread_mutex_lock((pthread_mutex_t *)plock); \
97         } \
98 } \
99  \
100 static int smb_thread_once_pthread(smb_thread_once_t *p_once, void (*init_fn)(void)) \
101 { \
102         return pthread_once(p_once, init_fn); \
103 } \
104  \
105 static int smb_create_tls_pthread(const char *keyname, void **ppkey, const char *location) \
106 { \
107         int ret; \
108         pthread_key_t *pkey; \
109         pkey = (pthread_key_t *)malloc(sizeof(pthread_key_t)); \
110         if (!pkey) { \
111                 return ENOMEM; \
112         } \
113         ret = pthread_key_create(pkey, NULL); \
114         if (ret) { \
115                 free(pkey); \
116                 return ret; \
117         } \
118         *ppkey = (void *)pkey; \
119         return 0; \
120 } \
121  \
122 static void smb_destroy_tls_pthread(void **ppkey, const char *location) \
123 { \
124         if (*ppkey) { \
125                 pthread_key_delete(*(pthread_key_t *)ppkey); \
126                 free(*ppkey); \
127                 *ppkey = NULL; \
128         } \
129 } \
130  \
131 static int smb_set_tls_pthread(void *pkey, const void *pval, const char *location) \
132 { \
133         return pthread_setspecific(*(pthread_key_t *)pkey, pval); \
134 } \
135  \
136 static void *smb_get_tls_pthread(void *pkey, const char *location) \
137 { \
138         return pthread_getspecific(*(pthread_key_t *)pkey); \
139 } \
140  \
141 static const struct smb_thread_functions (tf) = { \
142                         smb_create_mutex_pthread, \
143                         smb_destroy_mutex_pthread, \
144                         smb_lock_pthread, \
145                         smb_thread_once_pthread, \
146                         smb_create_tls_pthread, \
147                         smb_destroy_tls_pthread, \
148                         smb_set_tls_pthread, \
149                         smb_get_tls_pthread }
150
151 #endif