2 Unix SMB/CIFS implementation.
3 SMB thread interface functions.
4 Copyright (C) Jeremy Allison, 2009.
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.
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.
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/>.
20 #ifndef _smb_threads_h_
21 #define _smb_threads_h_
23 /* Data types needed for smb_thread_once call. */
25 #if defined(HAVE_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)
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)
38 enum smb_thread_lock_type {
43 struct smb_thread_functions {
44 /* Mutex and tls functions. */
45 int (*create_mutex)(const char *lockname,
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);
53 /* Once initialization. */
54 int (*smb_thread_once)(smb_thread_once_t *p_once, void (*init_fn)(void));
56 /* Thread local storage. */
57 int (*create_tls)(const char *keyname,
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);
66 int smb_thread_set_functions(const struct smb_thread_functions *tf);
68 extern const struct smb_thread_functions *global_tfp;
70 /* Define the pthread version of the functions. */
72 #define SMB_THREADS_DEF_PTHREAD_IMPLEMENTATION(tf) \
74 static int smb_create_mutex_pthread(const char *lockname, void **pplock, const char *location) \
76 pthread_mutex_t *pmut = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); \
80 pthread_mutex_init(pmut, NULL); \
81 *pplock = (void *)pmut; \
85 static void smb_destroy_mutex_pthread(void *plock, const char *location) \
87 pthread_mutex_destroy((pthread_mutex_t *)plock); \
91 static int smb_lock_pthread(void *plock, enum smb_thread_lock_type lock_type, const char *location) \
93 if (lock_type == SMB_THREAD_UNLOCK) { \
94 return pthread_mutex_unlock((pthread_mutex_t *)plock); \
96 return pthread_mutex_lock((pthread_mutex_t *)plock); \
100 static int smb_thread_once_pthread(smb_thread_once_t *p_once, void (*init_fn)(void)) \
102 return pthread_once(p_once, init_fn); \
105 static int smb_create_tls_pthread(const char *keyname, void **ppkey, const char *location) \
108 pthread_key_t *pkey; \
109 pkey = (pthread_key_t *)malloc(sizeof(pthread_key_t)); \
113 ret = pthread_key_create(pkey, NULL); \
118 *ppkey = (void *)pkey; \
122 static void smb_destroy_tls_pthread(void **ppkey, const char *location) \
125 pthread_key_delete(*(pthread_key_t *)ppkey); \
131 static int smb_set_tls_pthread(void *pkey, const void *pval, const char *location) \
133 return pthread_setspecific(*(pthread_key_t *)pkey, pval); \
136 static void *smb_get_tls_pthread(void *pkey, const char *location) \
138 return pthread_getspecific(*(pthread_key_t *)pkey); \
141 static const struct smb_thread_functions (tf) = { \
142 smb_create_mutex_pthread, \
143 smb_destroy_mutex_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 }