1*7c478bd9Sstevel@tonic-gate #include "ipf.h"
2*7c478bd9Sstevel@tonic-gate 
3*7c478bd9Sstevel@tonic-gate #define	EMM_MAGIC	0x97dd8b3a
4*7c478bd9Sstevel@tonic-gate 
eMrwlock_read_enter(rw,file,line)5*7c478bd9Sstevel@tonic-gate void eMrwlock_read_enter(rw, file, line)
6*7c478bd9Sstevel@tonic-gate eMrwlock_t *rw;
7*7c478bd9Sstevel@tonic-gate char *file;
8*7c478bd9Sstevel@tonic-gate int line;
9*7c478bd9Sstevel@tonic-gate {
10*7c478bd9Sstevel@tonic-gate 	if (rw->eMrw_magic != EMM_MAGIC) {
11*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s:eMrwlock_read_enter(%p): bad magic: %#x\n",
12*7c478bd9Sstevel@tonic-gate 			rw->eMrw_owner, rw, rw->eMrw_magic);
13*7c478bd9Sstevel@tonic-gate 		abort();
14*7c478bd9Sstevel@tonic-gate 	}
15*7c478bd9Sstevel@tonic-gate 	if (rw->eMrw_read != 0 || rw->eMrw_write != 0) {
16*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
17*7c478bd9Sstevel@tonic-gate 			"%s:eMrwlock_read_enter(%p): already locked: %d/%d\n",
18*7c478bd9Sstevel@tonic-gate 			rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write);
19*7c478bd9Sstevel@tonic-gate 		abort();
20*7c478bd9Sstevel@tonic-gate 	}
21*7c478bd9Sstevel@tonic-gate 	rw->eMrw_read++;
22*7c478bd9Sstevel@tonic-gate 	rw->eMrw_heldin = file;
23*7c478bd9Sstevel@tonic-gate 	rw->eMrw_heldat = line;
24*7c478bd9Sstevel@tonic-gate }
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate 
eMrwlock_write_enter(rw,file,line)27*7c478bd9Sstevel@tonic-gate void eMrwlock_write_enter(rw, file, line)
28*7c478bd9Sstevel@tonic-gate eMrwlock_t *rw;
29*7c478bd9Sstevel@tonic-gate char *file;
30*7c478bd9Sstevel@tonic-gate int line;
31*7c478bd9Sstevel@tonic-gate {
32*7c478bd9Sstevel@tonic-gate 	if (rw->eMrw_magic != EMM_MAGIC) {
33*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s:eMrwlock_write_enter(%p): bad magic: %#x\n",
34*7c478bd9Sstevel@tonic-gate 			rw->eMrw_owner, rw, rw->eMrw_magic);
35*7c478bd9Sstevel@tonic-gate 		abort();
36*7c478bd9Sstevel@tonic-gate 	}
37*7c478bd9Sstevel@tonic-gate 	if (rw->eMrw_read != 0 || rw->eMrw_write != 0) {
38*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
39*7c478bd9Sstevel@tonic-gate 			"%s:eMrwlock_write_enter(%p): already locked: %d/%d\n",
40*7c478bd9Sstevel@tonic-gate 			rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write);
41*7c478bd9Sstevel@tonic-gate 		abort();
42*7c478bd9Sstevel@tonic-gate 	}
43*7c478bd9Sstevel@tonic-gate 	rw->eMrw_write++;
44*7c478bd9Sstevel@tonic-gate 	rw->eMrw_heldin = file;
45*7c478bd9Sstevel@tonic-gate 	rw->eMrw_heldat = line;
46*7c478bd9Sstevel@tonic-gate }
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 
eMrwlock_downgrade(rw,file,line)49*7c478bd9Sstevel@tonic-gate void eMrwlock_downgrade(rw, file, line)
50*7c478bd9Sstevel@tonic-gate eMrwlock_t *rw;
51*7c478bd9Sstevel@tonic-gate char *file;
52*7c478bd9Sstevel@tonic-gate int line;
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate 	if (rw->eMrw_magic != EMM_MAGIC) {
55*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s:eMrwlock_write_enter(%p): bad magic: %#x\n",
56*7c478bd9Sstevel@tonic-gate 			rw->eMrw_owner, rw, rw->eMrw_magic);
57*7c478bd9Sstevel@tonic-gate 		abort();
58*7c478bd9Sstevel@tonic-gate 	}
59*7c478bd9Sstevel@tonic-gate 	if (rw->eMrw_read != 0 || rw->eMrw_write != 1) {
60*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
61*7c478bd9Sstevel@tonic-gate 			"%s:eMrwlock_write_enter(%p): already locked: %d/%d\n",
62*7c478bd9Sstevel@tonic-gate 			rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write);
63*7c478bd9Sstevel@tonic-gate 		abort();
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate 	rw->eMrw_write--;
66*7c478bd9Sstevel@tonic-gate 	rw->eMrw_read++;
67*7c478bd9Sstevel@tonic-gate 	rw->eMrw_heldin = file;
68*7c478bd9Sstevel@tonic-gate 	rw->eMrw_heldat = line;
69*7c478bd9Sstevel@tonic-gate }
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 
eMrwlock_exit(rw)72*7c478bd9Sstevel@tonic-gate void eMrwlock_exit(rw)
73*7c478bd9Sstevel@tonic-gate eMrwlock_t *rw;
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	if (rw->eMrw_magic != EMM_MAGIC) {
76*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s:eMrwlock_exit(%p): bad magic: %#x\n",
77*7c478bd9Sstevel@tonic-gate 			rw->eMrw_owner, rw, rw->eMrw_magic);
78*7c478bd9Sstevel@tonic-gate 		abort();
79*7c478bd9Sstevel@tonic-gate 	}
80*7c478bd9Sstevel@tonic-gate 	if (rw->eMrw_read != 1 && rw->eMrw_write != 1) {
81*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s:eMrwlock_exit(%p): not locked: %d/%d\n",
82*7c478bd9Sstevel@tonic-gate 			rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write);
83*7c478bd9Sstevel@tonic-gate 		abort();
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 	if (rw->eMrw_read == 1)
86*7c478bd9Sstevel@tonic-gate 		rw->eMrw_read--;
87*7c478bd9Sstevel@tonic-gate 	else if (rw->eMrw_write == 1)
88*7c478bd9Sstevel@tonic-gate 		rw->eMrw_write--;
89*7c478bd9Sstevel@tonic-gate 	rw->eMrw_heldin = NULL;
90*7c478bd9Sstevel@tonic-gate 	rw->eMrw_heldat = 0;
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 
eMrwlock_init(rw,who)94*7c478bd9Sstevel@tonic-gate void eMrwlock_init(rw, who)
95*7c478bd9Sstevel@tonic-gate eMrwlock_t *rw;
96*7c478bd9Sstevel@tonic-gate char *who;
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	if (rw->eMrw_magic == EMM_MAGIC) {	/* safe bet ? */
99*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
100*7c478bd9Sstevel@tonic-gate 			"%s:eMrwlock_init(%p): already initialised?: %#x\n",
101*7c478bd9Sstevel@tonic-gate 			rw->eMrw_owner, rw, rw->eMrw_magic);
102*7c478bd9Sstevel@tonic-gate 		abort();
103*7c478bd9Sstevel@tonic-gate 	}
104*7c478bd9Sstevel@tonic-gate 	rw->eMrw_magic = EMM_MAGIC;
105*7c478bd9Sstevel@tonic-gate 	rw->eMrw_read = 0;
106*7c478bd9Sstevel@tonic-gate 	rw->eMrw_write = 0;
107*7c478bd9Sstevel@tonic-gate 	if (who != NULL)
108*7c478bd9Sstevel@tonic-gate 		rw->eMrw_owner = strdup(who);
109*7c478bd9Sstevel@tonic-gate 	else
110*7c478bd9Sstevel@tonic-gate 		rw->eMrw_owner = NULL;
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 
eMrwlock_destroy(rw)114*7c478bd9Sstevel@tonic-gate void eMrwlock_destroy(rw)
115*7c478bd9Sstevel@tonic-gate eMrwlock_t *rw;
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	if (rw->eMrw_magic != EMM_MAGIC) {
118*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s:eMrwlock_destroy(%p): bad magic: %#x\n",
119*7c478bd9Sstevel@tonic-gate 			rw->eMrw_owner, rw, rw->eMrw_magic);
120*7c478bd9Sstevel@tonic-gate 		abort();
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 	memset(rw, 0xa5, sizeof(*rw));
123*7c478bd9Sstevel@tonic-gate }
124