xref: /freebsd/sbin/ipf/libipf/mutex_emul.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 
2 /*
3  * Copyright (C) 2012 by Darren Reed.
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  *
7  * $Id$
8  */
9 
10 #include "ipf.h"
11 
12 #define	EMM_MAGIC	0x9d7adba3
13 
14 static	int	mutex_debug = 0;
15 static	FILE	*mutex_file = NULL;
16 static	int	initcount = 0;
17 
18 void
19 eMmutex_enter(eMmutex_t *mtx, char *file, int line)
20 {
21 	if (mutex_debug & 2)
22 		fprintf(mutex_file, "%s:%d:eMmutex_enter(%s)\n", file, line,
23 		       mtx->eMm_owner);
24 	if (mtx->eMm_magic != EMM_MAGIC) {
25 		fprintf(stderr, "%s:eMmutex_enter(%p): bad magic: %#x\n",
26 			mtx->eMm_owner, mtx, mtx->eMm_magic);
27 		abort();
28 	}
29 	if (mtx->eMm_held != 0) {
30 		fprintf(stderr, "%s:eMmutex_enter(%p): already locked: %d\n",
31 			mtx->eMm_owner, mtx, mtx->eMm_held);
32 		abort();
33 	}
34 	mtx->eMm_held++;
35 	mtx->eMm_heldin = file;
36 	mtx->eMm_heldat = line;
37 }
38 
39 
40 void
41 eMmutex_exit(eMmutex_t *mtx, char *file, int line)
42 {
43 	if (mutex_debug & 2)
44 		fprintf(mutex_file, "%s:%d:eMmutex_exit(%s)\n", file, line,
45 		       mtx->eMm_owner);
46 	if (mtx->eMm_magic != EMM_MAGIC) {
47 		fprintf(stderr, "%s:eMmutex_exit(%p): bad magic: %#x\n",
48 			mtx->eMm_owner, mtx, mtx->eMm_magic);
49 		abort();
50 	}
51 	if (mtx->eMm_held != 1) {
52 		fprintf(stderr, "%s:eMmutex_exit(%p): not locked: %d\n",
53 			mtx->eMm_owner, mtx, mtx->eMm_held);
54 		abort();
55 	}
56 	mtx->eMm_held--;
57 	mtx->eMm_heldin = NULL;
58 	mtx->eMm_heldat = 0;
59 }
60 
61 
62 void
63 eMmutex_init(eMmutex_t *mtx, char *who, char *file, int line)
64 {
65 	if (mutex_file == NULL && mutex_debug)
66 		mutex_file = fopen("ipf_mutex_log", "w");
67 	if (mutex_debug & 1)
68 		fprintf(mutex_file, "%s:%d:eMmutex_init(%p,%s)\n",
69 			file, line, mtx, who);
70 	if (mtx->eMm_magic == EMM_MAGIC) {	/* safe bet ? */
71 		fprintf(stderr,
72 			"%s:eMmutex_init(%p): already initialised?: %#x\n",
73 			mtx->eMm_owner, mtx, mtx->eMm_magic);
74 		abort();
75 	}
76 	mtx->eMm_magic = EMM_MAGIC;
77 	mtx->eMm_held = 0;
78 	if (who != NULL)
79 		mtx->eMm_owner = strdup(who);
80 	else
81 		mtx->eMm_owner = NULL;
82 	initcount++;
83 }
84 
85 
86 void
87 eMmutex_destroy(eMmutex_t *mtx, char *file, int line)
88 {
89 	if (mutex_debug & 1)
90 		fprintf(mutex_file,
91 			"%s:%d:eMmutex_destroy(%p,%s)\n", file, line,
92 			mtx, mtx->eMm_owner);
93 	if (mtx->eMm_magic != EMM_MAGIC) {
94 		fprintf(stderr, "%s:eMmutex_destroy(%p): bad magic: %#x\n",
95 			mtx->eMm_owner, mtx, mtx->eMm_magic);
96 		abort();
97 	}
98 	if (mtx->eMm_held != 0) {
99 		fprintf(stderr,
100 			"%s:eMmutex_enter(%p): still locked: %d\n",
101 			mtx->eMm_owner, mtx, mtx->eMm_held);
102 		abort();
103 	}
104 	if (mtx->eMm_owner != NULL)
105 		free(mtx->eMm_owner);
106 	memset(mtx, 0xa5, sizeof(*mtx));
107 	initcount--;
108 }
109 
110 
111 void
112 ipf_mutex_clean(void)
113 {
114 	if (initcount != 0) {
115 		if (mutex_file)
116 			fprintf(mutex_file, "initcount %d\n", initcount);
117 		abort();
118 	}
119 }
120