summaryrefslogtreecommitdiff
path: root/lib/util/smb_threads.h
blob: 4443c3eae43b30dd2bc64aeb86bd843524dad638 (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
/*
	Unix SMB/CIFS implementation.
	SMB 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/>.
*/

#ifndef _smb_threads_h_
#define _smb_threads_h_

/* Data types needed for smb_thread_once call. */

#if defined(HAVE_PTHREAD_H)
#include <pthread.h>
#define smb_thread_once_t pthread_once_t
#define SMB_THREAD_ONCE_INIT PTHREAD_ONCE_INIT
#define SMB_THREAD_ONCE_IS_INITIALIZED(val) (true)
#define SMB_THREAD_ONCE_INITIALIZE(val)
#else
#define smb_thread_once_t bool
#define SMB_THREAD_ONCE_INIT false
#define SMB_THREAD_ONCE_IS_INITIALIZED(val) ((val) == true)
#define SMB_THREAD_ONCE_INITIALIZE(val) ((val) = true)
#endif

enum smb_thread_lock_type {
	SMB_THREAD_LOCK = 1,
	SMB_THREAD_UNLOCK
};

struct smb_thread_functions {
	/* Mutex and tls functions. */
	int (*create_mutex)(const char *lockname,
			void **pplock,
			const char *location);
	void (*destroy_mutex)(void *plock,
			const char *location);
	int (*lock_mutex)(void *plock, enum smb_thread_lock_type lock_type,
			const char *location);

	/* Once initialization. */
	int (*smb_thread_once)(smb_thread_once_t *p_once, void (*init_fn)(void));

	/* Thread local storage. */
	int (*create_tls)(const char *keyname,
			void **ppkey,
			const char *location);
	void (*destroy_tls)(void **pkey,
			const char *location);
	int (*set_tls)(void *pkey, const void *pval, const char *location);
	void *(*get_tls)(void *pkey, const char *location);
};

int smb_thread_set_functions(const struct smb_thread_functions *tf);

extern const struct smb_thread_functions *global_tfp;

/* Define the pthread version of the functions. */

#define SMB_THREADS_DEF_PTHREAD_IMPLEMENTATION(tf) \
 \
static int smb_create_mutex_pthread(const char *lockname, void **pplock, const char *location) \
{ \
	pthread_mutex_t *pmut = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); \
	if (!pmut) { \
		return ENOMEM; \
	} \
	pthread_mutex_init(pmut, NULL); \
	*pplock = (void *)pmut; \
	return 0; \
} \
 \
static void smb_destroy_mutex_pthread(void *plock, const char *location) \
{ \
	pthread_mutex_destroy((pthread_mutex_t *)plock); \
	free(plock); \
} \
 \
static int smb_lock_pthread(void *plock, enum smb_thread_lock_type lock_type, const char *location) \
{ \
	if (lock_type == SMB_THREAD_UNLOCK) { \
		return pthread_mutex_unlock((pthread_mutex_t *)plock); \
	} else { \
		return pthread_mutex_lock((pthread_mutex_t *)plock); \
	} \
} \
 \
static int smb_thread_once_pthread(smb_thread_once_t *p_once, void (*init_fn)(void)) \
{ \
	return pthread_once(p_once, init_fn); \
} \
 \
static int smb_create_tls_pthread(const char *keyname, void **ppkey, const char *location) \
{ \
	int ret; \
	pthread_key_t *pkey; \
	pkey = (pthread_key_t *)malloc(sizeof(pthread_key_t)); \
	if (!pkey) { \
		return ENOMEM; \
	} \
	ret = pthread_key_create(pkey, NULL); \
	if (ret) { \
		free(pkey); \
		return ret; \
	} \
	*ppkey = (void *)pkey; \
	return 0; \
} \
 \
static void smb_destroy_tls_pthread(void **ppkey, const char *location) \
{ \
	if (*ppkey) { \
		pthread_key_delete(*(pthread_key_t *)ppkey); \
		free(*ppkey); \
		*ppkey = NULL; \
	} \
} \
 \
static int smb_set_tls_pthread(void *pkey, const void *pval, const char *location) \
{ \
        return pthread_setspecific(*(pthread_key_t *)pkey, pval); \
} \
 \
static void *smb_get_tls_pthread(void *pkey, const char *location) \
{ \
        return pthread_getspecific(*(pthread_key_t *)pkey); \
} \
 \
static const struct smb_thread_functions (tf) = { \
			smb_create_mutex_pthread, \
			smb_destroy_mutex_pthread, \
			smb_lock_pthread, \
			smb_thread_once_pthread, \
			smb_create_tls_pthread, \
			smb_destroy_tls_pthread, \
			smb_set_tls_pthread, \
			smb_get_tls_pthread }

#endif