Add the thread functions to top level lib/util.
[ira/wip.git] / lib / util / smb_threads.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB client library implementation (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 /*
21  * This code is based in the ideas in openssl
22  * but somewhat simpler and expended to include
23  * thread local storage.
24  */
25
26 #include "includes.h"
27
28 #define NUM_GLOBAL_LOCKS 1
29
30 /*********************************************************
31  Functions to vector the locking primitives used internally
32  by libsmbclient.
33 *********************************************************/
34
35 const struct smb_thread_functions *global_tfp;
36
37 /*********************************************************
38  Dynamic lock array.
39 *********************************************************/
40
41 void **global_lock_array;
42
43 /*********************************************************
44  Function to set the locking primitives used by libsmbclient.
45 *********************************************************/
46
47 int smb_thread_set_functions(const struct smb_thread_functions *tf)
48 {
49         int i;
50
51         global_tfp = tf;
52
53         /* Here we initialize any static locks we're using. */
54         global_lock_array = (void **)SMB_MALLOC_ARRAY(void *, NUM_GLOBAL_LOCKS);
55         if (global_lock_array == NULL) {
56                 return ENOMEM;
57         }
58
59         for (i = 0; i < NUM_GLOBAL_LOCKS; i++) {
60                 char *name = NULL;
61                 if (asprintf(&name, "global_lock_%d", i) == -1) {
62                         SAFE_FREE(global_lock_array);
63                         return ENOMEM;
64                 }
65                 global_tfp->create_mutex(name,
66                                 &global_lock_array[i],
67                                 __location__);
68                 SAFE_FREE(name);
69         }
70
71         return 0;
72 }
73
74 #if 0
75 /* Test. - pthread implementations. */
76 #include <pthread.h>
77
78 #ifdef malloc
79 #undef malloc
80 #endif
81
82 SMB_THREADS_DEF_PTHREAD_IMPLEMENTATION(tf);
83
84 /* Test function. */
85 int test_threads(void)
86 {
87         int ret;
88         void *plock = NULL;
89
90         smb_thread_set_functions(&tf);
91
92         if ((ret = SMB_THREAD_CREATE_MUTEX("test", plock)) != 0) {
93                 printf("Create lock error: %d\n", ret);
94         }
95         if ((ret = SMB_THREAD_LOCK(plock, SMB_THREAD_LOCK)) != 0) {
96                 printf("lock error: %d\n", ret);
97         }
98         if ((SMB_THREAD_LOCK(plock, SMB_THREAD_UNLOCK)) != 0) {
99                 printf("unlock error: %d\n", ret);
100         }
101         SMB_THREAD_DESTROY_MUTEX(plock);
102
103         return 0;
104 }
105 #endif