]> code.delx.au - pulseaudio/blob - src/pulsecore/once-win32.c
Corrected a bogus comment.
[pulseaudio] / src / pulsecore / once-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 <assert.h>
29 #include <stdio.h>
30
31 #include <windows.h>
32
33 #include <pulsecore/mutex.h>
34
35 #include "once.h"
36
37 void pa_once(pa_once_t *control, pa_once_func_t func) {
38 HANDLE mutex;
39 char name[64];
40
41 assert(control);
42 assert(func);
43
44 /* Create the global mutex */
45 sprintf(name, "pulse%d", (int)GetCurrentProcessId());
46
47 mutex = CreateMutex(NULL, FALSE, name);
48 assert(mutex);
49
50 /* Create the local mutex */
51 WaitForSingleObject(mutex, INFINITE);
52 if (!control->mutex)
53 control->mutex = pa_mutex_new(1);
54 ReleaseMutex(mutex);
55
56 CloseHandle(mutex);
57
58 /* Execute function */
59 pa_mutex_lock(control->mutex);
60 if (!control->once_value) {
61 control->once_value = 1;
62 func();
63 }
64 pa_mutex_unlock(control->mutex);
65
66 /* Caveat: We have to make sure that the once func has completed
67 * before returning, even if the once func is not actually
68 * executed by us. Hence the awkward locking. */
69 }