2 Unix SMB/CIFS implementation.
3 SMB client library implementation (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/>.
21 * This code is based in the ideas in openssl
22 * but somewhat simpler and expended to include
23 * thread local storage.
27 #include "smb_threads.h"
29 /*********************************************************
30 Functions to vector the locking primitives used internally
32 *********************************************************/
34 const struct smb_thread_functions *global_tfp;
36 /*********************************************************
38 *********************************************************/
40 void **global_lock_array;
42 /*********************************************************
43 Function to set the locking primitives used by libsmbclient.
44 *********************************************************/
46 int smb_thread_set_functions(const struct smb_thread_functions *tf)
52 #if defined(PARANOID_MALLOC_CHECKER)
58 /* Here we initialize any static locks we're using. */
59 global_lock_array = (void **)malloc(sizeof(void *) *NUM_GLOBAL_LOCKS);
61 #if defined(PARANOID_MALLOC_CHECKER)
62 #define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
65 if (global_lock_array == NULL) {
69 for (i = 0; i < NUM_GLOBAL_LOCKS; i++) {
71 if (asprintf(&name, "global_lock_%d", i) == -1) {
72 SAFE_FREE(global_lock_array);
75 if (global_tfp->create_mutex(name,
76 &global_lock_array[i],
78 smb_panic("smb_thread_set_functions: create mutexes failed");
87 /* Test. - pthread implementations. */
94 SMB_THREADS_DEF_PTHREAD_IMPLEMENTATION(tf);
96 static smb_thread_once_t ot = SMB_THREAD_ONCE_INIT;
99 static void init_fn(void)
104 /* Non-thread safe init case. */
111 if ((ret = SMB_THREAD_CREATE_TLS("test_tls", pkey)) != 0) {
112 printf("Create tls once error: %d\n", ret);
117 int test_threads(void)
121 smb_thread_set_functions(&tf);
123 SMB_THREAD_ONCE(&ot, init_fn);
125 if ((ret = SMB_THREAD_CREATE_MUTEX("test", plock)) != 0) {
126 printf("Create lock error: %d\n", ret);
128 if ((ret = SMB_THREAD_LOCK(plock, SMB_THREAD_LOCK)) != 0) {
129 printf("lock error: %d\n", ret);
131 if ((SMB_THREAD_LOCK(plock, SMB_THREAD_UNLOCK)) != 0) {
132 printf("unlock error: %d\n", ret);
134 SMB_THREAD_DESTROY_MUTEX(plock);
135 SMB_THREAD_DESTROY_TLS(pkey);