document threads support
[imager.git] / mutexwin.c
CommitLineData
24c9233d
TC
1/*
2=head1 NAME
3
4mutex.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
17struct 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
27Create a mutex.
28
29If a critical section cannot be created for whatever reason, Imager
30will abort.
31
32=cut
33*/
34
35i_mutex_t
36i_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
50Destroy a mutex.
51
52=cut
53*/
54
55void
56i_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
66Lock the mutex, waiting if another thread has the mutex locked.
67
68=cut
69*/
70
71void
72i_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
81Release the mutex.
82
83The behavior of releasing a mutex you don't hold is unspecified.
84
85=cut
86*/
87
88void
89i_mutex_unlock(i_mutex_t m) {
90 LeaveCriticalSection(&(m->section));
91}
92