File: optional_mutex.c

package info (click to toggle)
spiped 1.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,328 kB
  • sloc: ansic: 11,951; sh: 1,081; makefile: 629; perl: 121
file content (69 lines) | stat: -rw-r--r-- 1,708 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
#ifdef OPTIONAL_MUTEX_PTHREAD_YES
#include <pthread.h>
#endif

#include "optional_mutex.h"

/* Sanity check OPTIONAL_MUTEX_PTHREAD_* defines. */
#if defined(OPTIONAL_MUTEX_PTHREAD_YES) && defined(OPTIONAL_MUTEX_PTHREAD_NO)
#error "You must not define both OPTIONAL_MUTEX_PTHREAD_YES "	\
    "and OPTIONAL_MUTEX_PTHREAD_NO"
#elif !(defined(OPTIONAL_MUTEX_PTHREAD_YES) ||			\
    defined(OPTIONAL_MUTEX_PTHREAD_NO))
#error "You must define either OPTIONAL_MUTEX_PTHREAD_YES "	\
    "or OPTIONAL_MUTEX_PTHREAD_NO"
#endif

#ifdef OPTIONAL_MUTEX_PTHREAD_YES
/* Clang normally warns if you have a mutex held at the end of a function. */
#ifdef __clang__
_Pragma("clang diagnostic push")
_Pragma("clang diagnostic ignored \"-Wthread-safety-analysis\"")
#endif
#endif

/**
 * optional_mutex_lock(mutex):
 * If OPTIONAL_MUTEX_PTHREAD_YES is defined, call pthread_mutex_lock();
 * if OPTIONAL_MUTEX_PTHREAD_NO is defined, do nothing.
 */
int
optional_mutex_lock(pthread_mutex_t * mutex)
{

#ifdef OPTIONAL_MUTEX_PTHREAD_YES
	/* Mutexes are enabled; call the pthread function. */
	return (pthread_mutex_lock(mutex));
#else
	(void)mutex; /* UNUSED */

	/* Do nothing. */
	return (0);
#endif
}

/**
 * optional_mutex_unlock(mutex):
 * If OPTIONAL_MUTEX_PTHREAD_YES is defined, call pthread_mutex_unlock();
 * if OPTIONAL_MUTEX_PTHREAD_NO is defined, do nothing.
 */
int
optional_mutex_unlock(pthread_mutex_t * mutex)
{

#ifdef OPTIONAL_MUTEX_PTHREAD_YES
	/* Mutexes are enabled; call the pthread function. */
	return (pthread_mutex_unlock(mutex));
#else
	(void)mutex; /* UNUSED */

	/* Do nothing. */
	return (0);
#endif
}

#ifdef OPTIONAL_MUTEX_PTHREAD_YES
#ifdef __clang__
_Pragma("clang diagnostic pop")
#endif
#endif