]> code.delx.au - pulseaudio/blob - src/pulsecore/mutex-win32.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / mutex-win32.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <windows.h>
29
30 #include <pulse/xmalloc.h>
31 #include <pulsecore/hashmap.h>
32
33 #include "mutex.h"
34
35 struct pa_mutex {
36 CRITICAL_SECTION mutex;
37 };
38
39 struct pa_cond {
40 pa_hashmap *wait_events;
41 };
42
43 pa_mutex* pa_mutex_new(pa_bool_t recursive, pa_bool_t inherit_priority) {
44 pa_mutex *m;
45
46 m = pa_xnew(pa_mutex, 1);
47
48 InitializeCriticalSection(&m->mutex);
49
50 return m;
51 }
52
53 void pa_mutex_free(pa_mutex *m) {
54 assert(m);
55
56 DeleteCriticalSection(&m->mutex);
57 pa_xfree(m);
58 }
59
60 void pa_mutex_lock(pa_mutex *m) {
61 assert(m);
62
63 EnterCriticalSection(&m->mutex);
64 }
65
66 void pa_mutex_unlock(pa_mutex *m) {
67 assert(m);
68
69 LeaveCriticalSection(&m->mutex);
70 }
71
72 pa_cond *pa_cond_new(void) {
73 pa_cond *c;
74
75 c = pa_xnew(pa_cond, 1);
76 c->wait_events = pa_hashmap_new(NULL, NULL);
77 assert(c->wait_events);
78
79 return c;
80 }
81
82 void pa_cond_free(pa_cond *c) {
83 assert(c);
84
85 pa_hashmap_free(c->wait_events, NULL, NULL);
86 pa_xfree(c);
87 }
88
89 void pa_cond_signal(pa_cond *c, int broadcast) {
90 assert(c);
91
92 if (pa_hashmap_size(c->wait_events) == 0)
93 return;
94
95 if (broadcast)
96 SetEvent(pa_hashmap_get_first(c->wait_events));
97 else {
98 void *iter;
99 const void *key;
100 HANDLE event;
101
102 iter = NULL;
103 while (1) {
104 pa_hashmap_iterate(c->wait_events, &iter, &key);
105 if (key == NULL)
106 break;
107 event = (HANDLE)pa_hashmap_get(c->wait_events, key);
108 SetEvent(event);
109 }
110 }
111 }
112
113 int pa_cond_wait(pa_cond *c, pa_mutex *m) {
114 HANDLE event;
115
116 assert(c);
117 assert(m);
118
119 event = CreateEvent(NULL, FALSE, FALSE, NULL);
120 assert(event);
121
122 pa_hashmap_put(c->wait_events, event, event);
123
124 pa_mutex_unlock(m);
125
126 WaitForSingleObject(event, INFINITE);
127
128 pa_mutex_lock(m);
129
130 pa_hashmap_remove(c->wait_events, event);
131
132 CloseHandle(event);
133
134 return 0;
135 }