~singpolyma/asterisk

ref: 51af79b42e4ae7d1831603633d73bf0df179720e asterisk/main/sem.c -rw-r--r-- 2.6 KiB
51af79b4Christopher Vollick Content Created By Initiator 2: Electric Boogaloo 9 months ago
                                                                                
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
/*
 * 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.
 */

/*! \file
 *
 * \brief Asterisk semaphore support.
 */

#include "asterisk.h"

#include "asterisk/sem.h"
#include "asterisk/utils.h"

#ifndef HAS_WORKING_SEMAPHORE

/* DIY semaphores! */

int ast_sem_init(struct ast_sem *sem, int pshared, unsigned int value)
{
	if (pshared) {
		/* Don't need it... yet */
		errno = ENOSYS;
		return -1;
	}

	/* Since value is unsigned, this will also catch attempts to init with
	 * a negative value */
	if (value > AST_SEM_VALUE_MAX) {
		errno = EINVAL;
		return -1;
	}

	sem->count = value;
	sem->waiters = 0;
	ast_mutex_init(&sem->mutex);
	ast_cond_init(&sem->cond, NULL);
	return 0;
}

int ast_sem_destroy(struct ast_sem *sem)
{
	ast_mutex_destroy(&sem->mutex);
	ast_cond_destroy(&sem->cond);
	return 0;
}

int ast_sem_post(struct ast_sem *sem)
{
	SCOPED_MUTEX(lock, &sem->mutex);

	ast_assert(sem->count >= 0);

	if (sem->count == AST_SEM_VALUE_MAX) {
		errno = EOVERFLOW;
		return -1;
	}

	/* Give it up! */
	++sem->count;

	/* Release a waiter, if needed */
	if (sem->waiters) {
		ast_cond_signal(&sem->cond);
	}

	return 0;
}

int ast_sem_wait(struct ast_sem *sem)
{
	int res;
	SCOPED_MUTEX(lock, &sem->mutex);

	ast_assert(sem->count >= 0);

	/* Wait for a non-zero count */
	++sem->waiters;
	while (sem->count == 0) {
		res = ast_cond_wait(&sem->cond, &sem->mutex);
		/* Give up on error */
		if (res != 0) {
			--sem->waiters;
			return res;
		}
	}
	--sem->waiters;

	/* Take it! */
	--sem->count;

	return 0;
}

int ast_sem_timedwait(struct ast_sem *sem, const struct timespec *abs_timeout)
{
	int res;
	SCOPED_MUTEX(lock, &sem->mutex);

	ast_assert(sem->count >= 0);

	/* Wait for a non-zero count */
	++sem->waiters;
	while (sem->count == 0) {
		res = ast_cond_timedwait(&sem->cond, &sem->mutex, abs_timeout);
		/* Give up on error */
		if (res != 0) {
			--sem->waiters;
			return res;
		}
	}
	--sem->waiters;

	/* Take it! */
	--sem->count;

	return 0;
}

int ast_sem_getvalue(struct ast_sem *sem, int *sval)
{
	SCOPED_MUTEX(lock, &sem->mutex);

	ast_assert(sem->count >= 0);

	*sval = sem->count;

	return 0;
}

#endif