summaryrefslogtreecommitdiff
path: root/lib/util/smb_threads.c
blob: 8cddbf01762df98b167ba1cf73d22b4142fcf0ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
   Unix SMB/CIFS implementation.
   SMB client library implementation (thread interface functions).
   Copyright (C) Jeremy Allison, 2009.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * This code is based in the ideas in openssl
 * but somewhat simpler and expended to include
 * thread local storage.
 */

#include "includes.h"
#include "smb_threads.h"

/*********************************************************
 Functions to vector the locking primitives used internally
 by libsmbclient.
*********************************************************/

const struct smb_thread_functions *global_tfp;

/*********************************************************
 Dynamic lock array.
*********************************************************/

void **global_lock_array;

/*********************************************************
 Mutex used for our internal "once" function
*********************************************************/

void *once_mutex = NULL;


/*********************************************************
 Function to set the locking primitives used by libsmbclient.
*********************************************************/

int smb_thread_set_functions(const struct smb_thread_functions *tf)
{
	int i;

	global_tfp = tf;

#if defined(PARANOID_MALLOC_CHECKER)
#ifdef malloc
#undef malloc
#endif
#endif

	/* Here we initialize any static locks we're using. */
	global_lock_array = (void **)malloc(sizeof(void *) *NUM_GLOBAL_LOCKS);

#if defined(PARANOID_MALLOC_CHECKER)
#define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
#endif

	if (global_lock_array == NULL) {
		return ENOMEM;
	}

	for (i = 0; i < NUM_GLOBAL_LOCKS; i++) {
		char *name = NULL;
		if (asprintf(&name, "global_lock_%d", i) == -1) {
			SAFE_FREE(global_lock_array);
			return ENOMEM;
		}
		if (global_tfp->create_mutex(name,
				&global_lock_array[i],
				__location__)) {
			smb_panic("smb_thread_set_functions: create mutexes failed");
		}
		SAFE_FREE(name);
	}

        /* Create the mutex we'll use for our "once" function */
	if (SMB_THREAD_CREATE_MUTEX("smb_once", once_mutex) != 0) {
		smb_panic("smb_thread_set_functions: failed to create 'once' mutex");
	}

	return 0;
}

/*******************************************************************
 Call a function only once. We implement this ourselves
 using our own mutex rather than using the thread implementation's
 *_once() function because each implementation has its own
 type for the variable which keeps track of whether the function
 has been called, and there's no easy way to allocate the correct
 size variable in code internal to Samba without knowing the
 implementation's "once" type.
********************************************************************/
void smb_thread_once(smb_thread_once_t *ponce, void (*init_fn)(void))
{
        int ret;
        int need_func_call;

        /* Lock our "once" mutex in order to test and initialize ponce */
	if ((ret = SMB_THREAD_LOCK(once_mutex, SMB_THREAD_LOCK)) != 0) {
		DEBUG(0, ("error locking 'once': %d\n", ret));
	}

        /* Store whether we're going to need to issue the function call */
        need_func_call = ! *ponce;

        /*
         * See if another thread got here after we tested it initially but
         * before we got our lock.
         */
        if (need_func_call) {
                /*
                 * Nope, we still need to issue the call. Set the "once"
                 * variable to true now so we can unlock the mutex. (We don't
                 * want to leave it locked during the call to the
                 * initialization function in case there's yet another "once"
                 * function needed to be called from therein.)
                 */
                *ponce = true;
        }

        /* Unlock the mutex */
	if ((ret = SMB_THREAD_LOCK(once_mutex, SMB_THREAD_UNLOCK)) != 0) {
		DEBUG(0, ("error unlocking 'once': %d\n", ret));
	}

        /* Finally, if we need to call the user-provided function, ... */
        if (need_func_call) {
                /* ... then do so now. */
                (*init_fn)();
        }
}


#if 0
/* Test. - pthread implementations. */
#include <pthread.h>

#ifdef malloc
#undef malloc
#endif

SMB_THREADS_DEF_PTHREAD_IMPLEMENTATION(tf);

static smb_thread_once_t ot = SMB_THREAD_ONCE_INIT;
void *pkey = NULL;

static void init_fn(void)
{
	int ret;

	if (!global_tfp) {
		/* Non-thread safe init case. */
		if (ot) {
			return;
		}
		ot = true;
	}

	if ((ret = SMB_THREAD_CREATE_TLS("test_tls", pkey)) != 0) {
		printf("Create tls once error: %d\n", ret);
	}
}

/* Test function. */
int test_threads(void)
{
	int ret;
	void *plock = NULL;
	smb_thread_set_functions(&tf);

	SMB_THREAD_ONCE(&ot, init_fn);

	if ((ret = SMB_THREAD_CREATE_MUTEX("test", plock)) != 0) {
		printf("Create lock error: %d\n", ret);
	}
	if ((ret = SMB_THREAD_LOCK(plock, SMB_THREAD_LOCK)) != 0) {
		printf("lock error: %d\n", ret);
	}
	if ((ret = SMB_THREAD_LOCK(plock, SMB_THREAD_UNLOCK)) != 0) {
		printf("unlock error: %d\n", ret);
	}
	SMB_THREAD_DESTROY_MUTEX(plock);
	SMB_THREAD_DESTROY_TLS(pkey);

	return 0;
}
#endif