-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex_emul.c
131 lines (119 loc) · 2.65 KB
/
mutex_emul.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (C) 2003-2012 by Darren Reed.
*
* See the IPFILTER.LICENCE file for details on licencing.
*
* $Id$
*/
#include "ipf.h"
#define EMM_MAGIC 0x9d7adba3
static int mutex_debug = 0;
static FILE *mutex_file = NULL;
static int initcount = 0;
void
eMmutex_enter(mtx, file, line)
eMmutex_t *mtx;
char *file;
int line;
{
if (mutex_debug & 2)
fprintf(mutex_file, "%s:%d:eMmutex_enter(%s)\n", file, line,
mtx->eMm_owner);
if (mtx->eMm_magic != EMM_MAGIC) {
fprintf(stderr, "%s:eMmutex_enter(%p): bad magic: %#x\n",
mtx->eMm_owner, mtx, mtx->eMm_magic);
abort();
}
if (mtx->eMm_held != 0) {
fprintf(stderr, "%s:eMmutex_enter(%p): already locked: %d\n",
mtx->eMm_owner, mtx, mtx->eMm_held);
abort();
}
mtx->eMm_held++;
mtx->eMm_heldin = file;
mtx->eMm_heldat = line;
}
void
eMmutex_exit(mtx, file, line)
eMmutex_t *mtx;
char *file;
int line;
{
if (mutex_debug & 2)
fprintf(mutex_file, "%s:%d:eMmutex_exit(%s)\n", file, line,
mtx->eMm_owner);
if (mtx->eMm_magic != EMM_MAGIC) {
fprintf(stderr, "%s:eMmutex_exit(%p): bad magic: %#x\n",
mtx->eMm_owner, mtx, mtx->eMm_magic);
abort();
}
if (mtx->eMm_held != 1) {
fprintf(stderr, "%s:eMmutex_exit(%p): not locked: %d\n",
mtx->eMm_owner, mtx, mtx->eMm_held);
abort();
}
mtx->eMm_held--;
mtx->eMm_heldin = NULL;
mtx->eMm_heldat = 0;
}
void
eMmutex_init(mtx, who, file, line)
eMmutex_t *mtx;
char *who;
char *file;
int line;
{
if (mutex_file == NULL && mutex_debug)
mutex_file = fopen("ipf_mutex_log", "w");
if (mutex_debug & 1)
fprintf(mutex_file, "%s:%d:eMmutex_init(%p,%s)\n",
file, line, mtx, who);
if (mtx->eMm_magic == EMM_MAGIC) { /* safe bet ? */
fprintf(stderr,
"%s:eMmutex_init(%p): already initialised?: %#x\n",
mtx->eMm_owner, mtx, mtx->eMm_magic);
abort();
}
mtx->eMm_magic = EMM_MAGIC;
mtx->eMm_held = 0;
if (who != NULL)
mtx->eMm_owner = strdup(who);
else
mtx->eMm_owner = NULL;
initcount++;
}
void
eMmutex_destroy(mtx, file, line)
eMmutex_t *mtx;
char *file;
int line;
{
if (mutex_debug & 1)
fprintf(mutex_file,
"%s:%d:eMmutex_destroy(%p,%s)\n", file, line,
mtx, mtx->eMm_owner);
if (mtx->eMm_magic != EMM_MAGIC) {
fprintf(stderr, "%s:eMmutex_destroy(%p): bad magic: %#x\n",
mtx->eMm_owner, mtx, mtx->eMm_magic);
abort();
}
if (mtx->eMm_held != 0) {
fprintf(stderr,
"%s:eMmutex_enter(%p): still locked: %d\n",
mtx->eMm_owner, mtx, mtx->eMm_held);
abort();
}
if (mtx->eMm_owner != NULL)
free(mtx->eMm_owner);
memset(mtx, 0xa5, sizeof(*mtx));
initcount--;
}
void
ipf_mutex_clean(void)
{
if (initcount != 0) {
if (mutex_file)
fprintf(mutex_file, "initcount %d\n", initcount);
abort();
}
}