forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreads.c
174 lines (155 loc) · 4.81 KB
/
threads.c
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
/*****************************************************************************
* threads.c: LibVLC generic thread support
*****************************************************************************
* Copyright (C) 2009-2012 Rémi Denis-Courmont
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <vlc_common.h>
/*** Global locks ***/
void vlc_global_mutex (unsigned n, bool acquire)
{
static vlc_mutex_t locks[] = {
VLC_STATIC_MUTEX,
VLC_STATIC_MUTEX,
VLC_STATIC_MUTEX,
VLC_STATIC_MUTEX,
VLC_STATIC_MUTEX,
};
static_assert (VLC_MAX_MUTEX == (sizeof (locks) / sizeof (locks[0])),
"Wrong number of global mutexes");
assert (n < (sizeof (locks) / sizeof (locks[0])));
vlc_mutex_t *lock = locks + n;
if (acquire)
vlc_mutex_lock (lock);
else
vlc_mutex_unlock (lock);
}
#ifdef LIBVLC_NEED_RWLOCK
/*** Generic read/write locks ***/
#include <stdlib.h>
#include <limits.h>
/* NOTE:
* lock->state is a signed long integer:
* - The sign bit is set when the lock is held for writing.
* - The other bits code the number of times the lock is held for reading.
* Consequently:
* - The value is negative if and only if the lock is held for writing.
* - The value is zero if and only if the lock is not held at all.
*/
#define READER_MASK LONG_MAX
#define WRITER_BIT LONG_MIN
void vlc_rwlock_init (vlc_rwlock_t *lock)
{
vlc_mutex_init (&lock->mutex);
vlc_cond_init (&lock->wait);
lock->state = 0;
}
void vlc_rwlock_destroy (vlc_rwlock_t *lock)
{
vlc_cond_destroy (&lock->wait);
vlc_mutex_destroy (&lock->mutex);
}
void vlc_rwlock_rdlock (vlc_rwlock_t *lock)
{
vlc_mutex_lock (&lock->mutex);
/* Recursive read-locking is allowed.
* Ensure that there is no active writer. */
while (lock->state < 0)
{
assert (lock->state == WRITER_BIT);
mutex_cleanup_push (&lock->mutex);
vlc_cond_wait (&lock->wait, &lock->mutex);
vlc_cleanup_pop ();
}
if (unlikely(lock->state >= READER_MASK))
abort (); /* An overflow is certainly a recursion bug. */
lock->state++;
vlc_mutex_unlock (&lock->mutex);
}
void vlc_rwlock_wrlock (vlc_rwlock_t *lock)
{
vlc_mutex_lock (&lock->mutex);
/* Wait until nobody owns the lock in any way. */
while (lock->state != 0)
{
mutex_cleanup_push (&lock->mutex);
vlc_cond_wait (&lock->wait, &lock->mutex);
vlc_cleanup_pop ();
}
lock->state = WRITER_BIT;
vlc_mutex_unlock (&lock->mutex);
}
void vlc_rwlock_unlock (vlc_rwlock_t *lock)
{
vlc_mutex_lock (&lock->mutex);
if (lock->state < 0)
{ /* Write unlock */
assert (lock->state == WRITER_BIT);
/* Let reader and writer compete. OS scheduler decides who wins. */
lock->state = 0;
vlc_cond_broadcast (&lock->wait);
}
else
{ /* Read unlock */
assert (lock->state > 0);
/* If there are no readers left, wake up one pending writer. */
if (--lock->state == 0)
vlc_cond_signal (&lock->wait);
}
vlc_mutex_unlock (&lock->mutex);
}
#endif /* LIBVLC_NEED_RWLOCK */
#ifdef LIBVLC_NEED_SEMAPHORE
/*** Generic semaphores ***/
#include <limits.h>
#include <errno.h>
void vlc_sem_init (vlc_sem_t *sem, unsigned value)
{
vlc_mutex_init (&sem->lock);
vlc_cond_init (&sem->wait);
sem->value = value;
}
void vlc_sem_destroy (vlc_sem_t *sem)
{
vlc_cond_destroy (&sem->wait);
vlc_mutex_destroy (&sem->lock);
}
int vlc_sem_post (vlc_sem_t *sem)
{
int ret = 0;
vlc_mutex_lock (&sem->lock);
if (likely(sem->value != UINT_MAX))
sem->value++;
else
ret = EOVERFLOW;
vlc_mutex_unlock (&sem->lock);
vlc_cond_signal (&sem->wait);
return ret;
}
void vlc_sem_wait (vlc_sem_t *sem)
{
vlc_mutex_lock (&sem->lock);
mutex_cleanup_push (&sem->lock);
while (!sem->value)
vlc_cond_wait (&sem->wait, &sem->lock);
sem->value--;
vlc_cleanup_run ();
}
#endif /* LIBVLC_NEED_SEMAPHORE */