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