2 This file is part of PulseAudio.
4 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
26 #include <pulse/xmalloc.h>
27 #include <pulsecore/hashmap.h>
32 CRITICAL_SECTION mutex;
36 pa_hashmap *wait_events;
39 pa_mutex* pa_mutex_new(bool recursive, bool inherit_priority) {
42 m = pa_xnew(pa_mutex, 1);
44 InitializeCriticalSection(&m->mutex);
49 void pa_mutex_free(pa_mutex *m) {
52 DeleteCriticalSection(&m->mutex);
56 void pa_mutex_lock(pa_mutex *m) {
59 EnterCriticalSection(&m->mutex);
62 void pa_mutex_unlock(pa_mutex *m) {
65 LeaveCriticalSection(&m->mutex);
68 pa_cond *pa_cond_new(void) {
71 c = pa_xnew(pa_cond, 1);
72 c->wait_events = pa_hashmap_new(NULL, NULL);
73 assert(c->wait_events);
78 void pa_cond_free(pa_cond *c) {
81 pa_hashmap_free(c->wait_events);
85 void pa_cond_signal(pa_cond *c, int broadcast) {
88 if (pa_hashmap_size(c->wait_events) == 0)
92 SetEvent(pa_hashmap_first(c->wait_events));
100 pa_hashmap_iterate(c->wait_events, &iter, &key);
103 event = (HANDLE)pa_hashmap_get(c->wait_events, key);
109 int pa_cond_wait(pa_cond *c, pa_mutex *m) {
115 event = CreateEvent(NULL, FALSE, FALSE, NULL);
118 pa_hashmap_put(c->wait_events, event, event);
122 WaitForSingleObject(event, INFINITE);
126 pa_hashmap_remove(c->wait_events, event);
134 int pa_cond_timedwait(pa_cond *c, pa_mutex *m, int ms_to_wait) {
141 event = CreateEvent(NULL, FALSE, FALSE, NULL);
144 pa_hashmap_put(c->wait_events, event, event);
148 ret = WaitForSingleObject(event, ms_to_wait);
152 pa_hashmap_remove(c->wait_events, event);
159 /* This is a copy of the function in mutex-posix.c */
160 pa_mutex* pa_static_mutex_get(pa_static_mutex *s, bool recursive, bool inherit_priority) {
165 /* First, check if already initialized and short cut */
166 if ((m = pa_atomic_ptr_load(&s->ptr)))
169 /* OK, not initialized, so let's allocate, and fill in */
170 m = pa_mutex_new(recursive, inherit_priority);
171 if ((pa_atomic_ptr_cmpxchg(&s->ptr, NULL, m)))
176 /* Him, filling in failed, so someone else must have filled in
178 pa_assert_se(m = pa_atomic_ptr_load(&s->ptr));