f1a39a6e281894655951dfcb55fe585c1962776c
[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 typedef bool smb_thread_once_t;
24 #define SMB_THREAD_ONCE_INIT false
25 #define SMB_THREAD_ONCE_IS_INITIALIZED(val) ((val) == true)
26 #define SMB_THREAD_ONCE_INITIALIZE(val) ((val) = true)
27
28 enum smb_thread_lock_type {
29         SMB_THREAD_LOCK = 1,
30         SMB_THREAD_UNLOCK
31 };
32
33 struct smb_thread_functions {
34         /* Mutex and tls functions. */
35         int (*create_mutex)(const char *lockname,
36                         void **pplock,
37                         const char *location);
38         void (*destroy_mutex)(void *plock,
39                         const char *location);
40         int (*lock_mutex)(void *plock, enum smb_thread_lock_type lock_type,
41                         const char *location);
42
43         /* Thread local storage. */
44         int (*create_tls)(const char *keyname,
45                         void **ppkey,
46                         const char *location);
47         void (*destroy_tls)(void **pkey,
48                         const char *location);
49         int (*set_tls)(void *pkey, const void *pval, const char *location);
50         void *(*get_tls)(void *pkey, const char *location);
51 };
52
53 int smb_thread_set_functions(const struct smb_thread_functions *tf);
54 void smb_thread_once(smb_thread_once_t *ponce, void (*init_fn)(void));
55
56 extern const struct smb_thread_functions *global_tfp;
57
58 /* Define the pthread version of the functions. */
59
60 #define SMB_THREADS_DEF_PTHREAD_IMPLEMENTATION(tf) \
61  \
62 static int smb_create_mutex_pthread(const char *lockname, void **pplock, const char *location) \
63 { \
64         pthread_mutex_t *pmut = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); \
65         if (!pmut) { \
66                 return ENOMEM; \
67         } \
68         pthread_mutex_init(pmut, NULL); \
69         *pplock = (void *)pmut; \
70         return 0; \
71 } \
72  \
73 static void smb_destroy_mutex_pthread(void *plock, const char *location) \
74 { \
75         pthread_mutex_destroy((pthread_mutex_t *)plock); \
76         free(plock); \
77 } \
78  \
79 static int smb_lock_pthread(void *plock, enum smb_thread_lock_type lock_type, const char *location) \
80 { \
81         if (lock_type == SMB_THREAD_UNLOCK) { \
82                 return pthread_mutex_unlock((pthread_mutex_t *)plock); \
83         } else { \
84                 return pthread_mutex_lock((pthread_mutex_t *)plock); \
85         } \
86 } \
87  \
88 static int smb_create_tls_pthread(const char *keyname, void **ppkey, const char *location) \
89 { \
90         int ret; \
91         pthread_key_t *pkey; \
92         pkey = (pthread_key_t *)malloc(sizeof(pthread_key_t)); \
93         if (!pkey) { \
94                 return ENOMEM; \
95         } \
96         ret = pthread_key_create(pkey, NULL); \
97         if (ret) { \
98                 free(pkey); \
99                 return ret; \
100         } \
101         *ppkey = (void *)pkey; \
102         return 0; \
103 } \
104  \
105 static void smb_destroy_tls_pthread(void **ppkey, const char *location) \
106 { \
107         if (*ppkey) { \
108                 pthread_key_delete(*(pthread_key_t *)ppkey); \
109                 free(*ppkey); \
110                 *ppkey = NULL; \
111         } \
112 } \
113  \
114 static int smb_set_tls_pthread(void *pkey, const void *pval, const char *location) \
115 { \
116         return pthread_setspecific(*(pthread_key_t *)pkey, pval); \
117 } \
118  \
119 static void *smb_get_tls_pthread(void *pkey, const char *location) \
120 { \
121         return pthread_getspecific(*(pthread_key_t *)pkey); \
122 } \
123  \
124 static const struct smb_thread_functions (tf) = { \
125                         smb_create_mutex_pthread, \
126                         smb_destroy_mutex_pthread, \
127                         smb_lock_pthread, \
128                         smb_create_tls_pthread, \
129                         smb_destroy_tls_pthread, \
130                         smb_set_tls_pthread, \
131                         smb_get_tls_pthread }
132
133 #endif