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