| 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 = malloc(sizeof(*m)); |
| 40 |
if (!m) |
| 41 |
i_fatal(3, "Cannot allocate mutex object"); |
| 42 |
InitializeCriticalSection(&(m->section)); |
| 43 |
|
| 44 |
return m; |
| 45 |
} |
| 46 |
|
| 47 |
/* |
| 48 |
=item i_mutex_destroy(m) |
| 49 |
=category Mutex functions |
| 50 |
=synopsis i_mutex_destroy(m); |
| 51 |
|
| 52 |
Destroy a mutex. |
| 53 |
|
| 54 |
=cut |
| 55 |
*/ |
| 56 |
|
| 57 |
void |
| 58 |
i_mutex_destroy(i_mutex_t m) { |
| 59 |
DeleteCriticalSection(&(m->section)); |
| 60 |
free(m); |
| 61 |
} |
| 62 |
|
| 63 |
/* |
| 64 |
=item i_mutex_lock(m) |
| 65 |
=category Mutex functions |
| 66 |
=synopsis i_mutex_lock(m); |
| 67 |
|
| 68 |
Lock the mutex, waiting if another thread has the mutex locked. |
| 69 |
|
| 70 |
=cut |
| 71 |
*/ |
| 72 |
|
| 73 |
void |
| 74 |
i_mutex_lock(i_mutex_t m) { |
| 75 |
EnterCriticalSection(&(m->section)); |
| 76 |
} |
| 77 |
|
| 78 |
/* |
| 79 |
=item i_mutex_unlock(m) |
| 80 |
=category Mutex functions |
| 81 |
=synopsis i_mutex_unlock(m); |
| 82 |
|
| 83 |
Release the mutex. |
| 84 |
|
| 85 |
The behavior of releasing a mutex you don't hold is unspecified. |
| 86 |
|
| 87 |
=cut |
| 88 |
*/ |
| 89 |
|
| 90 |
void |
| 91 |
i_mutex_unlock(i_mutex_t m) { |
| 92 |
LeaveCriticalSection(&(m->section)); |
| 93 |
} |
| 94 |
|