1*7c478bd9Sstevel@tonic-gate #include "ipf.h"
2*7c478bd9Sstevel@tonic-gate 
3*7c478bd9Sstevel@tonic-gate #define	EMM_MAGIC	0x9d7adba3
4*7c478bd9Sstevel@tonic-gate 
eMmutex_enter(mtx,file,line)5*7c478bd9Sstevel@tonic-gate void eMmutex_enter(mtx, file, line)
6*7c478bd9Sstevel@tonic-gate eMmutex_t *mtx;
7*7c478bd9Sstevel@tonic-gate char *file;
8*7c478bd9Sstevel@tonic-gate int line;
9*7c478bd9Sstevel@tonic-gate {
10*7c478bd9Sstevel@tonic-gate 	if (mtx->eMm_magic != EMM_MAGIC) {
11*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s:eMmutex_enter(%p): bad magic: %#x\n",
12*7c478bd9Sstevel@tonic-gate 			mtx->eMm_owner, mtx, mtx->eMm_magic);
13*7c478bd9Sstevel@tonic-gate 		abort();
14*7c478bd9Sstevel@tonic-gate 	}
15*7c478bd9Sstevel@tonic-gate 	if (mtx->eMm_held != 0) {
16*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s:eMmutex_enter(%p): already locked: %d\n",
17*7c478bd9Sstevel@tonic-gate 			mtx->eMm_owner, mtx, mtx->eMm_held);
18*7c478bd9Sstevel@tonic-gate 		abort();
19*7c478bd9Sstevel@tonic-gate 	}
20*7c478bd9Sstevel@tonic-gate 	mtx->eMm_held++;
21*7c478bd9Sstevel@tonic-gate 	mtx->eMm_heldin = file;
22*7c478bd9Sstevel@tonic-gate 	mtx->eMm_heldat = line;
23*7c478bd9Sstevel@tonic-gate }
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
eMmutex_exit(mtx)26*7c478bd9Sstevel@tonic-gate void eMmutex_exit(mtx)
27*7c478bd9Sstevel@tonic-gate eMmutex_t *mtx;
28*7c478bd9Sstevel@tonic-gate {
29*7c478bd9Sstevel@tonic-gate 	if (mtx->eMm_magic != EMM_MAGIC) {
30*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s:eMmutex_exit(%p): bad magic: %#x\n",
31*7c478bd9Sstevel@tonic-gate 			mtx->eMm_owner, mtx, mtx->eMm_magic);
32*7c478bd9Sstevel@tonic-gate 		abort();
33*7c478bd9Sstevel@tonic-gate 	}
34*7c478bd9Sstevel@tonic-gate 	if (mtx->eMm_held != 1) {
35*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s:eMmutex_exit(%p): not locked: %d\n",
36*7c478bd9Sstevel@tonic-gate 			mtx->eMm_owner, mtx, mtx->eMm_held);
37*7c478bd9Sstevel@tonic-gate 		abort();
38*7c478bd9Sstevel@tonic-gate 	}
39*7c478bd9Sstevel@tonic-gate 	mtx->eMm_held--;
40*7c478bd9Sstevel@tonic-gate 	mtx->eMm_heldin = NULL;
41*7c478bd9Sstevel@tonic-gate 	mtx->eMm_heldat = 0;
42*7c478bd9Sstevel@tonic-gate }
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 
eMmutex_init(mtx,who)45*7c478bd9Sstevel@tonic-gate void eMmutex_init(mtx, who)
46*7c478bd9Sstevel@tonic-gate eMmutex_t *mtx;
47*7c478bd9Sstevel@tonic-gate char *who;
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	if (mtx->eMm_magic == EMM_MAGIC) {	/* safe bet ? */
50*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
51*7c478bd9Sstevel@tonic-gate 			"%s:eMmutex_init(%p): already initialised?: %#x\n",
52*7c478bd9Sstevel@tonic-gate 			mtx->eMm_owner, mtx, mtx->eMm_magic);
53*7c478bd9Sstevel@tonic-gate 		abort();
54*7c478bd9Sstevel@tonic-gate 	}
55*7c478bd9Sstevel@tonic-gate 	mtx->eMm_magic = EMM_MAGIC;
56*7c478bd9Sstevel@tonic-gate 	mtx->eMm_held = 0;
57*7c478bd9Sstevel@tonic-gate 	if (who != NULL)
58*7c478bd9Sstevel@tonic-gate 		mtx->eMm_owner = strdup(who);
59*7c478bd9Sstevel@tonic-gate 	else
60*7c478bd9Sstevel@tonic-gate 		mtx->eMm_owner = NULL;
61*7c478bd9Sstevel@tonic-gate }
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 
eMmutex_destroy(mtx)64*7c478bd9Sstevel@tonic-gate void eMmutex_destroy(mtx)
65*7c478bd9Sstevel@tonic-gate eMmutex_t *mtx;
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	if (mtx->eMm_magic != EMM_MAGIC) {
68*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s:eMmutex_destroy(%p): bad magic: %#x\n",
69*7c478bd9Sstevel@tonic-gate 			mtx->eMm_owner, mtx, mtx->eMm_magic);
70*7c478bd9Sstevel@tonic-gate 		abort();
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 	if (mtx->eMm_held != 0) {
73*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s:eMmutex_enter(%p): still locked: %d\n",
74*7c478bd9Sstevel@tonic-gate 			mtx->eMm_owner, mtx, mtx->eMm_held);
75*7c478bd9Sstevel@tonic-gate 		abort();
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate 	memset(mtx, 0xa5, sizeof(*mtx));
78*7c478bd9Sstevel@tonic-gate }
79