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