Branch data Line data Source code
1 : : /***
2 : : This file is part of PulseAudio.
3 : :
4 : : Copyright 2006 Lennart Poettering
5 : :
6 : : PulseAudio is free software; you can redistribute it and/or modify
7 : : it under the terms of the GNU Lesser General Public License as published
8 : : by the Free Software Foundation; either version 2.1 of the License,
9 : : or (at your option) any later version.
10 : :
11 : : PulseAudio is distributed in the hope that it will be useful, but
12 : : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : General Public License for more details.
15 : :
16 : : You should have received a copy of the GNU Lesser General Public License
17 : : along with PulseAudio; if not, write to the Free Software
18 : : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 : : USA.
20 : : ***/
21 : :
22 : : #ifdef HAVE_CONFIG_H
23 : : #include <config.h>
24 : : #endif
25 : :
26 : : #include <errno.h>
27 : : #include <pthread.h>
28 : : #include <semaphore.h>
29 : :
30 : : #include <pulse/xmalloc.h>
31 : : #include <pulsecore/macro.h>
32 : :
33 : : #include "semaphore.h"
34 : :
35 : : struct pa_semaphore {
36 : : sem_t sem;
37 : : };
38 : :
39 : 10 : pa_semaphore* pa_semaphore_new(unsigned value) {
40 : : pa_semaphore *s;
41 : :
42 : 10 : s = pa_xnew(pa_semaphore, 1);
43 [ - + ]: 10 : pa_assert_se(sem_init(&s->sem, 0, value) == 0);
44 : 10 : return s;
45 : : }
46 : :
47 : 6 : void pa_semaphore_free(pa_semaphore *s) {
48 [ - + ]: 6 : pa_assert(s);
49 [ - + ]: 6 : pa_assert_se(sem_destroy(&s->sem) == 0);
50 : 6 : pa_xfree(s);
51 : 6 : }
52 : :
53 : 2 : void pa_semaphore_post(pa_semaphore *s) {
54 [ - + ]: 2 : pa_assert(s);
55 [ - + ]: 2 : pa_assert_se(sem_post(&s->sem) == 0);
56 : 2 : }
57 : :
58 : 1 : void pa_semaphore_wait(pa_semaphore *s) {
59 : : int ret;
60 [ - + ]: 1 : pa_assert(s);
61 : :
62 : : do {
63 : 1 : ret = sem_wait(&s->sem);
64 [ - + ][ # # ]: 1 : } while (ret < 0 && errno == EINTR);
65 : :
66 [ - + ]: 1 : pa_assert(ret == 0);
67 : 1 : }
68 : :
69 : 0 : pa_semaphore* pa_static_semaphore_get(pa_static_semaphore *s, unsigned value) {
70 : : pa_semaphore *m;
71 : :
72 [ # # ]: 0 : pa_assert(s);
73 : :
74 : : /* First, check if already initialized and short cut */
75 [ # # ]: 0 : if ((m = pa_atomic_ptr_load(&s->ptr)))
76 : : return m;
77 : :
78 : : /* OK, not initialized, so let's allocate, and fill in */
79 : 0 : m = pa_semaphore_new(value);
80 [ # # ]: 0 : if ((pa_atomic_ptr_cmpxchg(&s->ptr, NULL, m)))
81 : : return m;
82 : :
83 : 0 : pa_semaphore_free(m);
84 : :
85 : : /* Him, filling in failed, so someone else must have filled in
86 : : * already */
87 [ # # ]: 0 : pa_assert_se(m = pa_atomic_ptr_load(&s->ptr));
88 : : return m;
89 : : }
|