This source file includes following definitions.
- glwthread_mutex_init
- glwthread_mutex_lock
- glwthread_mutex_trylock
- glwthread_mutex_unlock
- glwthread_mutex_destroy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21
22
23 #include "windows-mutex.h"
24
25 #include <errno.h>
26
27 void
28 glwthread_mutex_init (glwthread_mutex_t *mutex)
29 {
30 InitializeCriticalSection (&mutex->lock);
31 mutex->guard.done = 1;
32 }
33
34 int
35 glwthread_mutex_lock (glwthread_mutex_t *mutex)
36 {
37 if (!mutex->guard.done)
38 {
39 if (InterlockedIncrement (&mutex->guard.started) == 0)
40
41 glwthread_mutex_init (mutex);
42 else
43 {
44
45 InterlockedDecrement (&mutex->guard.started);
46
47
48 while (!mutex->guard.done)
49 Sleep (0);
50 }
51 }
52 EnterCriticalSection (&mutex->lock);
53 return 0;
54 }
55
56 int
57 glwthread_mutex_trylock (glwthread_mutex_t *mutex)
58 {
59 if (!mutex->guard.done)
60 {
61 if (InterlockedIncrement (&mutex->guard.started) == 0)
62
63 glwthread_mutex_init (mutex);
64 else
65 {
66
67 InterlockedDecrement (&mutex->guard.started);
68
69
70 return EBUSY;
71 }
72 }
73 if (!TryEnterCriticalSection (&mutex->lock))
74 return EBUSY;
75 return 0;
76 }
77
78 int
79 glwthread_mutex_unlock (glwthread_mutex_t *mutex)
80 {
81 if (!mutex->guard.done)
82 return EINVAL;
83 LeaveCriticalSection (&mutex->lock);
84 return 0;
85 }
86
87 int
88 glwthread_mutex_destroy (glwthread_mutex_t *mutex)
89 {
90 if (!mutex->guard.done)
91 return EINVAL;
92 DeleteCriticalSection (&mutex->lock);
93 mutex->guard.done = 0;
94 return 0;
95 }