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