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