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