xref: /freebsd/sbin/ipf/libipf/rwlock_emul.c (revision b3d14eaccc5f606690d99b1998bfdf32a22404f6)
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	0x97dd8b3a
14 
15 void eMrwlock_read_enter(rw, file, line)
16 	eMrwlock_t *rw;
17 	char *file;
18 	int line;
19 {
20 	if (rw->eMrw_magic != EMM_MAGIC) {
21 		fprintf(stderr, "%s:eMrwlock_read_enter(%p): bad magic: %#x\n",
22 			rw->eMrw_owner, rw, rw->eMrw_magic);
23 		abort();
24 	}
25 	if (rw->eMrw_read != 0 || rw->eMrw_write != 0) {
26 		fprintf(stderr,
27 			"%s:eMrwlock_read_enter(%p): already locked: %d/%d\n",
28 			rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write);
29 		abort();
30 	}
31 	rw->eMrw_read++;
32 	rw->eMrw_heldin = file;
33 	rw->eMrw_heldat = line;
34 }
35 
36 
37 void eMrwlock_write_enter(rw, file, line)
38 	eMrwlock_t *rw;
39 	char *file;
40 	int line;
41 {
42 	if (rw->eMrw_magic != EMM_MAGIC) {
43 		fprintf(stderr, "%s:eMrwlock_write_enter(%p): bad magic: %#x\n",
44 			rw->eMrw_owner, rw, rw->eMrw_magic);
45 		abort();
46 	}
47 	if (rw->eMrw_read != 0 || rw->eMrw_write != 0) {
48 		fprintf(stderr,
49 			"%s:eMrwlock_write_enter(%p): already locked: %d/%d\n",
50 			rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write);
51 		abort();
52 	}
53 	rw->eMrw_write++;
54 	rw->eMrw_heldin = file;
55 	rw->eMrw_heldat = line;
56 }
57 
58 
59 void eMrwlock_try_upgrade(rw, file, line)
60 	eMrwlock_t *rw;
61 	char *file;
62 	int line;
63 {
64 	if (rw->eMrw_magic != EMM_MAGIC) {
65 		fprintf(stderr, "%s:eMrwlock_write_enter(%p): bad magic: %#x\n",
66 			rw->eMrw_owner, rw, rw->eMrw_magic);
67 		abort();
68 	}
69 	if (rw->eMrw_read != 0 || rw->eMrw_write != 0) {
70 		fprintf(stderr,
71 			"%s:eMrwlock_try_upgrade(%p): already locked: %d/%d\n",
72 			rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write);
73 		abort();
74 	}
75 	rw->eMrw_write++;
76 	rw->eMrw_heldin = file;
77 	rw->eMrw_heldat = line;
78 }
79 
80 void eMrwlock_downgrade(rw, file, line)
81 	eMrwlock_t *rw;
82 	char *file;
83 	int line;
84 {
85 	if (rw->eMrw_magic != EMM_MAGIC) {
86 		fprintf(stderr, "%s:eMrwlock_write_enter(%p): bad magic: %#x\n",
87 			rw->eMrw_owner, rw, rw->eMrw_magic);
88 		abort();
89 	}
90 	if (rw->eMrw_read != 0 || rw->eMrw_write != 1) {
91 		fprintf(stderr,
92 			"%s:eMrwlock_write_enter(%p): already locked: %d/%d\n",
93 			rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write);
94 		abort();
95 	}
96 	rw->eMrw_write--;
97 	rw->eMrw_read++;
98 	rw->eMrw_heldin = file;
99 	rw->eMrw_heldat = line;
100 }
101 
102 
103 void eMrwlock_exit(rw)
104 	eMrwlock_t *rw;
105 {
106 	if (rw->eMrw_magic != EMM_MAGIC) {
107 		fprintf(stderr, "%s:eMrwlock_exit(%p): bad magic: %#x\n",
108 			rw->eMrw_owner, rw, rw->eMrw_magic);
109 		abort();
110 	}
111 	if (rw->eMrw_read != 1 && rw->eMrw_write != 1) {
112 		fprintf(stderr, "%s:eMrwlock_exit(%p): not locked: %d/%d\n",
113 			rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write);
114 		abort();
115 	}
116 	if (rw->eMrw_read == 1)
117 		rw->eMrw_read--;
118 	else if (rw->eMrw_write == 1)
119 		rw->eMrw_write--;
120 	rw->eMrw_heldin = NULL;
121 	rw->eMrw_heldat = 0;
122 }
123 
124 
125 static int initcount = 0;
126 
127 void eMrwlock_init(rw, who)
128 	eMrwlock_t *rw;
129 	char *who;
130 {
131 	if (rw->eMrw_magic == EMM_MAGIC) {	/* safe bet ? */
132 		fprintf(stderr,
133 			"%s:eMrwlock_init(%p): already initialised?: %#x\n",
134 			rw->eMrw_owner, rw, rw->eMrw_magic);
135 		abort();
136 	}
137 	rw->eMrw_magic = EMM_MAGIC;
138 	rw->eMrw_read = 0;
139 	rw->eMrw_write = 0;
140 	if (who != NULL)
141 		rw->eMrw_owner = strdup(who);
142 	else
143 		rw->eMrw_owner = NULL;
144 	initcount++;
145 }
146 
147 
148 void eMrwlock_destroy(rw)
149 	eMrwlock_t *rw;
150 {
151 	if (rw->eMrw_magic != EMM_MAGIC) {
152 		fprintf(stderr, "%s:eMrwlock_destroy(%p): bad magic: %#x\n",
153 			rw->eMrw_owner, rw, rw->eMrw_magic);
154 		abort();
155 	}
156 	if (rw->eMrw_owner != NULL)
157 		free(rw->eMrw_owner);
158 	memset(rw, 0xa5, sizeof(*rw));
159 	initcount--;
160 }
161 
162 void ipf_rwlock_clean()
163 {
164 	if (initcount != 0)
165 		abort();
166 }
167