File: mutexwin.c

package info (click to toggle)
libimager-perl 1.005%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,308 kB
  • ctags: 4,067
  • sloc: perl: 30,915; ansic: 27,680; makefile: 55; cpp: 4
file content (94 lines) | stat: -rw-r--r-- 1,259 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
=head1 NAME

mutex.c - Imager's mutex API.

=head1 FUNCTIONS

=over

=cut
*/

#include "imageri.h"

#include <windows.h>

struct i_mutex_tag {
  CRITICAL_SECTION section;
};

/*
=item i_mutex_new()
=category Mutex functions
=synopsis i_mutex_t m = i_mutex_new();
=order 10

Create a mutex.

If a critical section cannot be created for whatever reason, Imager
will abort.

=cut
*/

i_mutex_t
i_mutex_new(void) {
  i_mutex_t m;

  m = malloc(sizeof(*m));
  if (!m)
    i_fatal(3, "Cannot allocate mutex object");
  InitializeCriticalSection(&(m->section));

  return m;
}

/*
=item i_mutex_destroy(m)
=category Mutex functions
=synopsis i_mutex_destroy(m);

Destroy a mutex.

=cut
*/

void
i_mutex_destroy(i_mutex_t m) {
  DeleteCriticalSection(&(m->section));
  free(m);
}

/*
=item i_mutex_lock(m)
=category Mutex functions
=synopsis i_mutex_lock(m);

Lock the mutex, waiting if another thread has the mutex locked.

=cut
*/

void
i_mutex_lock(i_mutex_t m) {
  EnterCriticalSection(&(m->section));
}

/*
=item i_mutex_unlock(m)
=category Mutex functions
=synopsis i_mutex_unlock(m);

Release the mutex.

The behavior of releasing a mutex you don't hold is unspecified.

=cut
*/

void
i_mutex_unlock(i_mutex_t m) {
  LeaveCriticalSection(&(m->section));
}