xref: /freebsd/sbin/ipf/libipf/mutex_emul.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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(eMmutex_t *mtx, char *file, int line)
89 {
90 	if (mutex_debug & 1)
91 		fprintf(mutex_file,
92 			"%s:%d:eMmutex_destroy(%p,%s)\n", file, line,
93 			mtx, mtx->eMm_owner);
94 	if (mtx->eMm_magic != EMM_MAGIC) {
95 		fprintf(stderr, "%s:eMmutex_destroy(%p): bad magic: %#x\n",
96 			mtx->eMm_owner, mtx, mtx->eMm_magic);
97 		abort();
98 	}
99 	if (mtx->eMm_held != 0) {
100 		fprintf(stderr,
101 			"%s:eMmutex_enter(%p): still locked: %d\n",
102 			mtx->eMm_owner, mtx, mtx->eMm_held);
103 		abort();
104 	}
105 	if (mtx->eMm_owner != NULL)
106 		free(mtx->eMm_owner);
107 	memset(mtx, 0xa5, sizeof(*mtx));
108 	initcount--;
109 }
110 
111 
112 void
113 ipf_mutex_clean(void)
114 {
115 	if (initcount != 0) {
116 		if (mutex_file)
117 			fprintf(mutex_file, "initcount %d\n", initcount);
118 		abort();
119 	}
120 }
121