1 |
/* w32-sema.c |
2 |
* Copyright (C) 2001 Werner Koch (dd9jn), g10 Code GmbH |
3 |
* |
4 |
* This file is part of MyGPGME. |
5 |
* |
6 |
* MyGPGME is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* MyGPGME is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
|
21 |
#include <stdio.h> |
22 |
#include <stdlib.h> |
23 |
#include <string.h> |
24 |
#include <assert.h> |
25 |
#include <errno.h> |
26 |
#include <sys/types.h> |
27 |
#include <windows.h> |
28 |
#include <process.h> |
29 |
#include <io.h> |
30 |
|
31 |
#include "util.h" |
32 |
#include "sema.h" |
33 |
|
34 |
static void |
35 |
sema_fatal (const char *text) |
36 |
{ |
37 |
fprintf (stderr, "sema.c: %s\n", text); |
38 |
abort (); |
39 |
} |
40 |
|
41 |
|
42 |
static void |
43 |
critsect_init (struct critsect_s * s) |
44 |
{ |
45 |
CRITICAL_SECTION * mp; |
46 |
static CRITICAL_SECTION init_lock; |
47 |
static int initialized; |
48 |
|
49 |
if (!initialized) { |
50 |
/* the very first time we call this function, we assume that only |
51 |
* one thread is running, so that we can bootstrap the semaphore code |
52 |
*/ |
53 |
InitializeCriticalSection (&init_lock); |
54 |
initialized = 1; |
55 |
} |
56 |
if (!s) |
57 |
return; /* we just want to initialize ourself */ |
58 |
|
59 |
/* first test whether it is really not initialized */ |
60 |
EnterCriticalSection (&init_lock); |
61 |
if ( s->private ) { |
62 |
LeaveCriticalSection (&init_lock); |
63 |
return; |
64 |
} |
65 |
/* now init it */ |
66 |
mp = malloc ( sizeof *mp ); |
67 |
if (!mp) { |
68 |
LeaveCriticalSection (&init_lock); |
69 |
sema_fatal ("out of core while creating critical section lock"); |
70 |
} |
71 |
InitializeCriticalSection (mp); |
72 |
s->private = mp; |
73 |
LeaveCriticalSection (&init_lock); |
74 |
} |
75 |
|
76 |
void |
77 |
_gpgme_sema_subsystem_init () |
78 |
{ |
79 |
/* fixme: we should check that there is only one thread running */ |
80 |
critsect_init (NULL); |
81 |
} |
82 |
|
83 |
|
84 |
void |
85 |
_gpgme_sema_cs_enter ( struct critsect_s *s ) |
86 |
{ |
87 |
if (!s->private) |
88 |
critsect_init (s); |
89 |
EnterCriticalSection ( (CRITICAL_SECTION*)s->private ); |
90 |
} |
91 |
|
92 |
void |
93 |
_gpgme_sema_cs_leave (struct critsect_s *s) |
94 |
{ |
95 |
if (!s->private) |
96 |
critsect_init (s); |
97 |
LeaveCriticalSection ( (CRITICAL_SECTION*)s->private ); |
98 |
} |
99 |
|
100 |
void |
101 |
_gpgme_sema_cs_destroy ( struct critsect_s *s ) |
102 |
{ |
103 |
if (s && s->private) { |
104 |
DeleteCriticalSection ((CRITICAL_SECTION*)s->private); |
105 |
safe_free (s->private); |
106 |
s->private = NULL; |
107 |
} |
108 |
} |