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