File: tt_pthread.h

package info (click to toggle)
tarantool-lts 1.5.5.37.g1687c02-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 15,124 kB
  • ctags: 18,819
  • sloc: ansic: 118,797; cpp: 22,178; sh: 18,496; xml: 10,692; python: 2,083; java: 1,759; perl: 972; makefile: 719; ruby: 84
file content (178 lines) | stat: -rw-r--r-- 5,111 bytes parent folder | download | duplicates (2)
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
#ifndef TARANTOOL_PTHREAD_H_INCLUDED
#define TARANTOOL_PTHREAD_H_INCLUDED
/*
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * 1. Redistributions of source code must retain the above
 *    copyright notice, this list of conditions and the
 *    following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "tarantool/config.h"

#include <pthread.h>
#include "tarantool/util.h"
#include <say.h>

/**
 * Assert on any pthread* error in debug mode. In release,
 * write into the log file where and what has failed.
 *
 * Still give the user an opportunity to manually
 * check for error, by returning the pthread_* 
 * function status.
 */

#define tt_pthread_error(e)			\
	if (e != 0)				\
		say_error("%s error %d", __func__, e);\
	assert(e == 0);				\
	e

/**
 * Debug/logging friendly wrappers around pthread
 * functions.
 */

#define tt_pthread_mutex_init(mutex, attr)	\
({	int e = pthread_mutex_init(mutex, attr);\
	tt_pthread_error(e);			\
})

#define tt_pthread_mutex_destroy(mutex)		\
({	int e = pthread_mutex_destroy(mutex);	\
	tt_pthread_error(e);			\
})

#define tt_pthread_mutex_lock(mutex)		\
({	int e = pthread_mutex_lock(mutex);	\
	say_debug("%s: locking %s", __func__, #mutex);\
	tt_pthread_error(e);\
})

#define tt_pthread_mutex_trylock(mutex)		\
({	int e = pthread_mutex_trylock(mutex);	\
	if (e != 0 && e != EBUSY)		\
		say_error("%s error %d at %s:%d", __func__, e, __FILE__, __LINE__);\
	assert(e == 0 || e == EBUSY);		\
	e					\
})

#define tt_pthread_mutex_unlock(mutex)		\
({	int e = pthread_mutex_unlock(mutex);	\
	say_debug("%s: unlocking %s", __func__, #mutex);\
	tt_pthread_error(e);			\
})

#define tt_pthread_mutex_destroy(mutex)		\
({	int e = pthread_mutex_destroy(mutex);	\
	tt_pthread_error(e);			\
})

#define tt_pthread_mutexattr_init(attr)		\
({	int e = pthread_mutexattr_init(attr);	\
	tt_pthread_error(e);			\
})

#define tt_pthread_mutexattr_destroy(attr)	\
({	int e = pthread_mutexattr_destroy(attr);\
	tt_pthread_error(e);			\
})

#define tt_pthread_mutexattr_gettype(attr, type)\
({	int e = pthread_mutexattr_gettype(attr, type);\
	tt_pthread_error(e);			\
})

#define tt_pthread_mutexattr_settype(attr, type)\
({	int e = pthread_mutexattr_settype(attr, type);\
	tt_pthread_error(e);			\
})

#define tt_pthread_condattr_init(attr)		\
({	int e = pthread_condattr_init(attr);	\
	tt_pthread_error(e);			\
})

#define tt_pthread_condattr_destroy(attr)	\
({ int e = pthread_condattr_destroy(attr);	\
	tt_pthread_error(e);			\
})

#define tt_pthread_cond_init(cond, attr)	\
({	int e = pthread_cond_init(cond, attr);	\
	tt_pthread_error(e);			\
})

#define tt_pthread_cond_destroy(cond)		\
({	int e = pthread_cond_destroy(cond);	\
	tt_pthread_error(e);			\
})

#define tt_pthread_cond_signal(cond)		\
({	int e = pthread_cond_signal(cond);	\
	tt_pthread_error(e);			\
})

#define tt_pthread_cond_wait(cond, mutex)	\
({	int e = pthread_cond_wait(cond, mutex);\
	tt_pthread_error(e);			\
})

#define tt_pthread_cond_timedwait(cond, mutex, timeout)	\
({	int e = pthread_cond_timedwait(cond, mutex, timeout);\
	if (ETIMEDOUT != e)                           \
		say_error("%s error %d", __func__, e);\
	assert(e == 0 || e == ETIMEDOUT);             \
	e                                             \
})

#define tt_pthread_once(control, function)	\
({	int e = pthread_once(control, function);\
	tt_pthread_error(e);			\
})

#define tt_pthread_atfork(prepare, parent, child)\
({	int e = pthread_atfork(prepare, parent, child);\
	tt_pthread_error(e);			\
})

/** Make sure the created thread blocks all signals,
 * they are handled in the main thread.
 */
#define tt_pthread_create(thread, attr, run, arg)	\
({	sigset_t set, oldset;				\
	sigfillset(&set);				\
	pthread_sigmask(SIG_BLOCK, &set, &oldset);	\
	int e = pthread_create(thread, attr, run, arg);	\
	pthread_sigmask(SIG_SETMASK, &oldset, NULL);	\
	tt_pthread_error(e);				\
})

#define tt_pthread_join(thread, ret)			\
({	int e = pthread_join(thread, ret);		\
	tt_pthread_error(e);				\
})

#endif /* TARANTOOL_PTHREAD_H_INCLUDED */