summaryrefslogtreecommitdiff
path: root/include/asterisk/sem.h
blob: fcab82a5e2872886466bda8253988f98b70205c9 (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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2013, Digium, Inc.
 *
 * David M. Lee, II <dlee@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

#ifndef ASTERISK_SEMAPHORE_H
#define ASTERISK_SEMAPHORE_H

/*!
 * \file
 *
 * \brief Asterisk semaphore API
 *
 * This API is a thin wrapper around the POSIX semaphore API (when available),
 * so see the POSIX documentation for further details.
 */

#ifdef HAS_WORKING_SEMAPHORE
/* Working semaphore implementation detected */

#include <semaphore.h>

struct ast_sem {
	sem_t real_sem;
};

#define AST_SEM_VALUE_MAX SEM_VALUE_MAX

/* These are thin wrappers; might as well inline them */

static force_inline int ast_sem_init(struct ast_sem *sem, int pshared, unsigned int value)
{
	return sem_init(&sem->real_sem, pshared, value);
}

static force_inline int ast_sem_destroy(struct ast_sem *sem)
{
	return sem_destroy(&sem->real_sem);
}

static force_inline int ast_sem_post(struct ast_sem *sem)
{
	return sem_post(&sem->real_sem);
}

static force_inline int ast_sem_wait(struct ast_sem *sem)
{
	return sem_wait(&sem->real_sem);
}

static force_inline int ast_sem_timedwait(struct ast_sem *sem, const struct timespec *abs_timeout)
{
	return sem_timedwait(&sem->real_sem, abs_timeout);
}

static force_inline int ast_sem_getvalue(struct ast_sem *sem, int *sval)
{
	return sem_getvalue(&sem->real_sem, sval);
}

#else
/* Unnamed semaphores don't work. Rolling our own, I guess... */

#include "asterisk/lock.h"

#include <limits.h>

struct ast_sem {
	/*! Current count of this semaphore */
	int count;
	/*! Number of threads currently waiting for this semaphore */
	int waiters;
	/*! Mutual exclusion */
	ast_mutex_t mutex;
	/*! Condition for singalling waiters */
	ast_cond_t cond;
};

#define AST_SEM_VALUE_MAX INT_MAX

/*!
 * \brief Initialize a semaphore.
 *
 * \param sem Semaphore to initialize.
 * \param pshared Pass true (nonzero) to share this thread between processes.
 *                Not be supported on all platforms, so be wary!
 *                But leave the parameter, to be compatible with the POSIX ABI
 *                in case we need to add support in the future.
 * \param value Initial value of the semaphore.
 *
 * \return 0 on success.
 * \return -1 on error, errno set to indicate error.
 */
int ast_sem_init(struct ast_sem *sem, int pshared, unsigned int value);

/*!
 * \brief Destroy a semaphore.
 *
 * Destroying a semaphore that other threads are currently blocked on produces
 * undefined behavior.
 *
 * \param sem Semaphore to destroy.
 *
 * \return 0 on success.
 * \return -1 on error, errno set to indicate error.
 */
int ast_sem_destroy(struct ast_sem *sem);

/*!
 * \brief Increments the semaphore, unblocking a waiter if necessary.
 *
 * \param sem Semaphore to increment.
 *
 * \return 0 on success.
 * \return -1 on error, errno set to indicate error.
 */
int ast_sem_post(struct ast_sem *sem);

/*!
 * \brief Decrements the semaphore.
 *
 * If the semaphore's current value is zero, this function blocks until another
 * thread posts (ast_sem_post()) to the semaphore (or is interrupted by a signal
 * handler, which sets errno to EINTR).
 *
 * \param sem Semaphore to decrement.
 *
 * \return 0 on success.
 * \return -1 on error, errno set to indicate error.
 */
int ast_sem_wait(struct ast_sem *sem);

/*!
 * \brief Decrements the semaphore, waiting until abs_timeout.
 *
 * If the semaphore's current value is zero, this function blocks until another
 * thread posts (ast_sem_post()) to the semaphore (or is interrupted by a signal
 * handler, which sets errno to EINTR).
 *
 * \param sem Semaphore to decrement.
 *
 * \return 0 on success.
 * \return -1 on error, errno set to indicate error.
 */
int ast_sem_timedwait(struct ast_sem *sem, const struct timespec *abs_timeout);

/*!
 * \brief Gets the current value of the semaphore.
 *
 * If threads are blocked on this semaphore, POSIX allows the return value to be
 * either 0 or a negative number whose absolute value is the number of threads
 * blocked. Don't assume that it will give you one or the other; Asterisk has
 * been ported to just about everything.
 *
 * \param sem Semaphore to query.
 * \param[out] sval Output value.
 *
 * \return 0 on success.
 * \return -1 on error, errno set to indicate error.
 */
int ast_sem_getvalue(struct ast_sem *sem, int *sval);

#endif

#endif /* ASTERISK_SEMAPHORE_H */