Merge commit 'origin/master-tx'
[platform/upstream/pulseaudio.git] / src / pulsecore / mutex-win32.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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 <windows.h>
27
28 #include <pulse/xmalloc.h>
29 #include <pulsecore/hashmap.h>
30
31 #include "mutex.h"
32
33 struct pa_mutex {
34     CRITICAL_SECTION mutex;
35 };
36
37 struct pa_cond {
38     pa_hashmap *wait_events;
39 };
40
41 pa_mutex* pa_mutex_new(pa_bool_t recursive, pa_bool_t inherit_priority) {
42     pa_mutex *m;
43
44     m = pa_xnew(pa_mutex, 1);
45
46     InitializeCriticalSection(&m->mutex);
47
48     return m;
49 }
50
51 void pa_mutex_free(pa_mutex *m) {
52     assert(m);
53
54     DeleteCriticalSection(&m->mutex);
55     pa_xfree(m);
56 }
57
58 void pa_mutex_lock(pa_mutex *m) {
59     assert(m);
60
61     EnterCriticalSection(&m->mutex);
62 }
63
64 void pa_mutex_unlock(pa_mutex *m) {
65     assert(m);
66
67     LeaveCriticalSection(&m->mutex);
68 }
69
70 pa_cond *pa_cond_new(void) {
71     pa_cond *c;
72
73     c = pa_xnew(pa_cond, 1);
74     c->wait_events = pa_hashmap_new(NULL, NULL);
75     assert(c->wait_events);
76
77     return c;
78 }
79
80 void pa_cond_free(pa_cond *c) {
81     assert(c);
82
83     pa_hashmap_free(c->wait_events, NULL, NULL);
84     pa_xfree(c);
85 }
86
87 void pa_cond_signal(pa_cond *c, int broadcast) {
88     assert(c);
89
90     if (pa_hashmap_size(c->wait_events) == 0)
91         return;
92
93     if (broadcast)
94         SetEvent(pa_hashmap_get_first(c->wait_events));
95     else {
96         void *iter;
97         const void *key;
98         HANDLE event;
99
100         iter = NULL;
101         while (1) {
102             pa_hashmap_iterate(c->wait_events, &iter, &key);
103             if (key == NULL)
104                 break;
105             event = (HANDLE)pa_hashmap_get(c->wait_events, key);
106             SetEvent(event);
107         }
108     }
109 }
110
111 int pa_cond_wait(pa_cond *c, pa_mutex *m) {
112     HANDLE event;
113
114     assert(c);
115     assert(m);
116
117     event = CreateEvent(NULL, FALSE, FALSE, NULL);
118     assert(event);
119
120     pa_hashmap_put(c->wait_events, event, event);
121
122     pa_mutex_unlock(m);
123
124     WaitForSingleObject(event, INFINITE);
125
126     pa_mutex_lock(m);
127
128     pa_hashmap_remove(c->wait_events, event);
129
130     CloseHandle(event);
131
132     return 0;
133 }