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