Provide a libsmbclient interface for programs requiring threads
[jra/samba/.git] / source3 / libsmb / libsmb_thread_impl.c
1 /* 
2    Unix SMB/Netbios implementation.
3    SMB client library implementation
4    Copyright (C) Derrell Lipman 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 #include "includes.h"
21 #include "libsmbclient.h"
22 #include "libsmb_internal.h"
23
24
25 /**
26  * Initialize for an arbitrary thread implementation. The caller should
27  * provide, as parameters, pointers to functions to implement the requisite
28  * low-level thread functionality. A function must be provided for each
29  * parameter; none may be null.
30  *
31  * If the thread implementation is POSIX Threads (pthreads), then the much
32  * simpler smbc_thread_pthread() function may be used instead of this one.
33  *
34  * @param create_mutex
35  *   Create a mutex. This function should expect three parameters: lockname,
36  *   pplock, and location. It should create a unique mutex for each unique
37  *   lockname it is provided, and return the mutex identifier in *pplock. The
38  *   location parameter can be used for debugging, as it contains the
39  *   compiler-provided __location__ of the call.
40  *
41  * @param destroy_mutex
42  *   Destroy a mutex. This function should expect two parameters: plock and
43  *   location. It should destroy the mutex associated with the identifier
44  *   plock. The location parameter can be used for debugging, as it contains
45  *   the compiler-provided __location__ of the call.
46  *
47  * @param lock_mutex
48  *   Lock a mutex. This function should expect three parameters: plock,
49  *   lock_type, and location. The mutex aassociated with identifier plock
50  *   should be locked if lock_type is 1, and unlocked if lock_type is 2. The
51  *   location parameter can be used for debugging, as it contains the
52  *   compiler-provided __location__ of the call.
53  *
54  * @param create_tls
55  *   Create thread local storage. This function should expect three
56  *   parameters: keyname, ppkey, and location. It should allocate an
57  *   implementation-specific amount of memory and assign the pointer to that
58  *   allocated memory to *ppkey. The location parameter can be used for
59  *   debugging, as it contains the compiler-provided __location__ of the
60  *   call. This function should return 0 upon success, non-zero upon failure.
61  *
62  * @param destroy_tls
63  *   Destroy thread local storage. This function should expect two parameters:
64  *   ppkey and location. The ppkey parameter points to a variable containing a
65  *   thread local storage key previously provided by the create_tls
66  *   function. The location parameter can be used for debugging, as it
67  *   contains the compiler-provided __location__ of the call.
68  *
69  * @param set_tls
70  *   Set a thread local storage variable's value. This function should expect
71  *   three parameters: pkey, pval, and location. The pkey parameter is a
72  *   thread local storage key previously provided by the create_tls
73  *   function. The (void *) pval parameter contains the value to be placed in
74  *   the thread local storage variable identified by pkey. The location
75  *   parameter can be used for debugging, as it contains the compiler-provided
76  *   __location__ of the call. This function should return 0 upon success;
77  *   non-zero otherwise.
78  *
79  * @param get_tls
80  *   Retrieve a thread local storage variable's value. This function should
81  *   expect two parameters: pkey and location. The pkey parameter is a thread
82  *   local storage key previously provided by the create_tls function, and
83  *   which has previously been used in a call to the set_tls function to
84  *   initialize a thread local storage variable. The location parameter can be
85  *   used for debugging, as it contains the compiler-provided __location__ of
86  *   the call. This function should return the (void *) value stored in the
87  *   variable identified by pkey.
88  *
89  * @return {void}
90  */
91 void
92 smbc_thread_impl(
93         /* Mutex functions. */
94         int (*create_mutex)(const char *lockname,
95                             void **pplock,
96                             const char *location),
97         void (*destroy_mutex)(void *plock,
98                               const char *location),
99         int (*lock_mutex)(void *plock,
100                           int lock_type,
101                           const char *location),
102     
103         /* Thread local storage. */
104         int (*create_tls)(const char *keyname,
105                           void **ppkey,
106                           const char *location),
107         void (*destroy_tls)(void **ppkey,
108                             const char *location),
109         int (*set_tls)(void *pkey,
110                        const void *pval,
111                        const char *location),
112         void *(*get_tls)(void *pkey,
113                          const char *location)
114         )
115 {
116         static struct smb_thread_functions tf;
117
118         tf.create_mutex  = create_mutex;
119         tf.destroy_mutex = destroy_mutex;
120         tf.lock_mutex    = lock_mutex;
121         tf.create_tls    = create_tls;
122         tf.destroy_tls   = destroy_tls;
123         tf.set_tls       = set_tls;
124         tf.get_tls       = get_tls;
125
126         smb_thread_set_functions(&tf);
127 }