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