xref: /netbsd-src/external/bsd/ipf/dist/lib/rwlock_emul.c (revision 13885a665959c62f13a82b3caedf986eaa17aa31)
1*13885a66Sdarrenr /*	$NetBSD: rwlock_emul.c,v 1.2 2012/07/22 14:27:37 darrenr Exp $	*/
2bc4097aaSchristos 
3bc4097aaSchristos /*
4bc4097aaSchristos  * Copyright (C) 2012 by Darren Reed.
5bc4097aaSchristos  *
6bc4097aaSchristos  * See the IPFILTER.LICENCE file for details on licencing.
7bc4097aaSchristos  *
8*13885a66Sdarrenr  * Id: rwlock_emul.c,v 1.1.1.2 2012/07/22 13:44:42 darrenr Exp $
9bc4097aaSchristos  */
10bc4097aaSchristos 
11bc4097aaSchristos #include "ipf.h"
12bc4097aaSchristos 
13bc4097aaSchristos #define	EMM_MAGIC	0x97dd8b3a
14bc4097aaSchristos 
eMrwlock_read_enter(rw,file,line)15bc4097aaSchristos void eMrwlock_read_enter(rw, file, line)
16bc4097aaSchristos 	eMrwlock_t *rw;
17bc4097aaSchristos 	char *file;
18bc4097aaSchristos 	int line;
19bc4097aaSchristos {
20bc4097aaSchristos 	if (rw->eMrw_magic != EMM_MAGIC) {
21bc4097aaSchristos 		fprintf(stderr, "%s:eMrwlock_read_enter(%p): bad magic: %#x\n",
22bc4097aaSchristos 			rw->eMrw_owner, rw, rw->eMrw_magic);
23bc4097aaSchristos 		abort();
24bc4097aaSchristos 	}
25bc4097aaSchristos 	if (rw->eMrw_read != 0 || rw->eMrw_write != 0) {
26bc4097aaSchristos 		fprintf(stderr,
27bc4097aaSchristos 			"%s:eMrwlock_read_enter(%p): already locked: %d/%d\n",
28bc4097aaSchristos 			rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write);
29bc4097aaSchristos 		abort();
30bc4097aaSchristos 	}
31bc4097aaSchristos 	rw->eMrw_read++;
32bc4097aaSchristos 	rw->eMrw_heldin = file;
33bc4097aaSchristos 	rw->eMrw_heldat = line;
34bc4097aaSchristos }
35bc4097aaSchristos 
36bc4097aaSchristos 
eMrwlock_write_enter(rw,file,line)37bc4097aaSchristos void eMrwlock_write_enter(rw, file, line)
38bc4097aaSchristos 	eMrwlock_t *rw;
39bc4097aaSchristos 	char *file;
40bc4097aaSchristos 	int line;
41bc4097aaSchristos {
42bc4097aaSchristos 	if (rw->eMrw_magic != EMM_MAGIC) {
43bc4097aaSchristos 		fprintf(stderr, "%s:eMrwlock_write_enter(%p): bad magic: %#x\n",
44bc4097aaSchristos 			rw->eMrw_owner, rw, rw->eMrw_magic);
45bc4097aaSchristos 		abort();
46bc4097aaSchristos 	}
47bc4097aaSchristos 	if (rw->eMrw_read != 0 || rw->eMrw_write != 0) {
48bc4097aaSchristos 		fprintf(stderr,
49bc4097aaSchristos 			"%s:eMrwlock_write_enter(%p): already locked: %d/%d\n",
50bc4097aaSchristos 			rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write);
51bc4097aaSchristos 		abort();
52bc4097aaSchristos 	}
53bc4097aaSchristos 	rw->eMrw_write++;
54bc4097aaSchristos 	rw->eMrw_heldin = file;
55bc4097aaSchristos 	rw->eMrw_heldat = line;
56bc4097aaSchristos }
57bc4097aaSchristos 
58bc4097aaSchristos 
eMrwlock_downgrade(rw,file,line)59bc4097aaSchristos void eMrwlock_downgrade(rw, file, line)
60bc4097aaSchristos 	eMrwlock_t *rw;
61bc4097aaSchristos 	char *file;
62bc4097aaSchristos 	int line;
63bc4097aaSchristos {
64bc4097aaSchristos 	if (rw->eMrw_magic != EMM_MAGIC) {
65bc4097aaSchristos 		fprintf(stderr, "%s:eMrwlock_write_enter(%p): bad magic: %#x\n",
66bc4097aaSchristos 			rw->eMrw_owner, rw, rw->eMrw_magic);
67bc4097aaSchristos 		abort();
68bc4097aaSchristos 	}
69bc4097aaSchristos 	if (rw->eMrw_read != 0 || rw->eMrw_write != 1) {
70bc4097aaSchristos 		fprintf(stderr,
71bc4097aaSchristos 			"%s:eMrwlock_write_enter(%p): already locked: %d/%d\n",
72bc4097aaSchristos 			rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write);
73bc4097aaSchristos 		abort();
74bc4097aaSchristos 	}
75bc4097aaSchristos 	rw->eMrw_write--;
76bc4097aaSchristos 	rw->eMrw_read++;
77bc4097aaSchristos 	rw->eMrw_heldin = file;
78bc4097aaSchristos 	rw->eMrw_heldat = line;
79bc4097aaSchristos }
80bc4097aaSchristos 
81bc4097aaSchristos 
eMrwlock_exit(rw)82bc4097aaSchristos void eMrwlock_exit(rw)
83bc4097aaSchristos 	eMrwlock_t *rw;
84bc4097aaSchristos {
85bc4097aaSchristos 	if (rw->eMrw_magic != EMM_MAGIC) {
86bc4097aaSchristos 		fprintf(stderr, "%s:eMrwlock_exit(%p): bad magic: %#x\n",
87bc4097aaSchristos 			rw->eMrw_owner, rw, rw->eMrw_magic);
88bc4097aaSchristos 		abort();
89bc4097aaSchristos 	}
90bc4097aaSchristos 	if (rw->eMrw_read != 1 && rw->eMrw_write != 1) {
91bc4097aaSchristos 		fprintf(stderr, "%s:eMrwlock_exit(%p): not locked: %d/%d\n",
92bc4097aaSchristos 			rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write);
93bc4097aaSchristos 		abort();
94bc4097aaSchristos 	}
95bc4097aaSchristos 	if (rw->eMrw_read == 1)
96bc4097aaSchristos 		rw->eMrw_read--;
97bc4097aaSchristos 	else if (rw->eMrw_write == 1)
98bc4097aaSchristos 		rw->eMrw_write--;
99bc4097aaSchristos 	rw->eMrw_heldin = NULL;
100bc4097aaSchristos 	rw->eMrw_heldat = 0;
101bc4097aaSchristos }
102bc4097aaSchristos 
103bc4097aaSchristos 
104bc4097aaSchristos static int initcount = 0;
105bc4097aaSchristos 
eMrwlock_init(rw,who)106bc4097aaSchristos void eMrwlock_init(rw, who)
107bc4097aaSchristos 	eMrwlock_t *rw;
108bc4097aaSchristos 	char *who;
109bc4097aaSchristos {
110bc4097aaSchristos 	if (rw->eMrw_magic == EMM_MAGIC) {	/* safe bet ? */
111bc4097aaSchristos 		fprintf(stderr,
112bc4097aaSchristos 			"%s:eMrwlock_init(%p): already initialised?: %#x\n",
113bc4097aaSchristos 			rw->eMrw_owner, rw, rw->eMrw_magic);
114bc4097aaSchristos 		abort();
115bc4097aaSchristos 	}
116bc4097aaSchristos 	rw->eMrw_magic = EMM_MAGIC;
117bc4097aaSchristos 	rw->eMrw_read = 0;
118bc4097aaSchristos 	rw->eMrw_write = 0;
119bc4097aaSchristos 	if (who != NULL)
120bc4097aaSchristos 		rw->eMrw_owner = strdup(who);
121bc4097aaSchristos 	else
122bc4097aaSchristos 		rw->eMrw_owner = NULL;
123bc4097aaSchristos 	initcount++;
124bc4097aaSchristos }
125bc4097aaSchristos 
126bc4097aaSchristos 
eMrwlock_destroy(rw)127bc4097aaSchristos void eMrwlock_destroy(rw)
128bc4097aaSchristos 	eMrwlock_t *rw;
129bc4097aaSchristos {
130bc4097aaSchristos 	if (rw->eMrw_magic != EMM_MAGIC) {
131bc4097aaSchristos 		fprintf(stderr, "%s:eMrwlock_destroy(%p): bad magic: %#x\n",
132bc4097aaSchristos 			rw->eMrw_owner, rw, rw->eMrw_magic);
133bc4097aaSchristos 		abort();
134bc4097aaSchristos 	}
135bc4097aaSchristos 	if (rw->eMrw_owner != NULL)
136bc4097aaSchristos 		free(rw->eMrw_owner);
137bc4097aaSchristos 	memset(rw, 0xa5, sizeof(*rw));
138bc4097aaSchristos 	initcount--;
139bc4097aaSchristos }
140bc4097aaSchristos 
ipf_rwlock_clean()141bc4097aaSchristos void ipf_rwlock_clean()
142bc4097aaSchristos {
143bc4097aaSchristos 	if (initcount != 0)
144bc4097aaSchristos 		abort();
145bc4097aaSchristos }
146