Commit | Line | Data |
---|---|---|
24c9233d TC |
1 | /* |
2 | =head1 NAME | |
3 | ||
4 | mutex.c - Imager's mutex API. | |
5 | ||
6 | =head1 FUNCTIONS | |
7 | ||
8 | =over | |
9 | ||
10 | =cut | |
11 | */ | |
12 | ||
13 | #include "imageri.h" | |
14 | ||
15 | #include <windows.h> | |
16 | ||
17 | struct i_mutex_tag { | |
18 | CRITICAL_SECTION section; | |
19 | }; | |
20 | ||
21 | /* | |
22 | =item i_mutex_new() | |
23 | =category Mutex functions | |
24 | =synopsis i_mutex_t m = i_mutex_new(); | |
25 | =order 10 | |
26 | ||
27 | Create a mutex. | |
28 | ||
29 | If a critical section cannot be created for whatever reason, Imager | |
30 | will abort. | |
31 | ||
32 | =cut | |
33 | */ | |
34 | ||
35 | i_mutex_t | |
36 | i_mutex_new(void) { | |
37 | i_mutex_t m; | |
38 | ||
39 | m = mymalloc(sizeof(*m)); | |
40 | InitializeCriticalSection(&(m->section)); | |
41 | ||
42 | return m; | |
43 | } | |
44 | ||
45 | /* | |
46 | =item i_mutex_destroy(m) | |
47 | =category Mutex functions | |
48 | =synopsis i_mutex_destroy(m); | |
49 | ||
50 | Destroy a mutex. | |
51 | ||
52 | =cut | |
53 | */ | |
54 | ||
55 | void | |
56 | i_mutex_destroy(i_mutex_t m) { | |
57 | DeleteCriticalSection(&(m->section)); | |
58 | myfree(m); | |
59 | } | |
60 | ||
61 | /* | |
62 | =item i_mutex_lock(m) | |
63 | =category Mutex functions | |
64 | =synopsis i_mutex_lock(m); | |
65 | ||
66 | Lock the mutex, waiting if another thread has the mutex locked. | |
67 | ||
68 | =cut | |
69 | */ | |
70 | ||
71 | void | |
72 | i_mutex_lock(i_mutex_t m) { | |
73 | EnterCriticalSection(&(m->section)); | |
74 | } | |
75 | ||
76 | /* | |
77 | =item i_mutex_unlock(m) | |
78 | =category Mutex functions | |
79 | =synopsis i_mutex_unlock(m); | |
80 | ||
81 | Release the mutex. | |
82 | ||
83 | The behavior of releasing a mutex you don't hold is unspecified. | |
84 | ||
85 | =cut | |
86 | */ | |
87 | ||
88 | void | |
89 | i_mutex_unlock(i_mutex_t m) { | |
90 | LeaveCriticalSection(&(m->section)); | |
91 | } | |
92 |