-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeneric_mutex.c
79 lines (62 loc) · 1.58 KB
/
generic_mutex.c
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
//============
// my_mutex.c
//============
// To not always use macro "KERNAUX_PROTECTED_FIELD" around the names of
// structure fields you may define "KERNAUX_ACCESS_PROTECTED" before including
// any other headers, but ONLY in the file where you implement a generic type.
//
#define KERNAUX_ACCESS_PROTECTED
//============
// my_mutex.h
//============
#include <kernaux/generic/mutex.h>
#include <pthread.h>
typedef struct MyMutex {
struct KernAux_Mutex mutex;
pthread_mutex_t pthread_mutex;
} *MyMutex;
struct MyMutex MyMytex_create();
//============
// my_mutex.c
//============
#include <kernaux/generic/mutex.h>
#include <pthread.h>
#include <stdlib.h>
static void MyMutex_lock (void *mutex);
static void MyMutex_unlock(void *mutex);
struct MyMutex MyMutex_create()
{
struct MyMutex my_mutex = {
.mutex = {
.lock = MyMutex_lock,
.unlock = MyMutex_unlock,
},
};
if (pthread_mutex_init(&my_mutex.pthread_mutex, NULL) != 0) abort();
return my_mutex;
}
void MyMutex_lock(void *const mutex)
{
const MyMutex my_mutex = mutex;
pthread_mutex_lock(&my_mutex->pthread_mutex);
}
void MyMutex_unlock(void *const mutex)
{
const MyMutex my_mutex = mutex;
pthread_mutex_unlock(&my_mutex->pthread_mutex);
}
//========
// main.c
//========
static int shared_counter = 0;
void example_main()
{
// Create mutex
struct MyMutex my_mutex = MyMutex_create();
// Lock mutex
KernAux_Mutex_lock(&my_mutex.mutex);
// Access shared data
++shared_counter;
// Unlock mutex
KernAux_Mutex_unlock(&my_mutex.mutex);
}