1*13885a66Sdarrenr /* $NetBSD: mutex_emul.c,v 1.2 2012/07/22 14:27:36 darrenr Exp $ */
2bc4097aaSchristos
3bc4097aaSchristos /*
4bc4097aaSchristos * Copyright (C) 2012 by Darren Reed.
5bc4097aaSchristos *
6bc4097aaSchristos * See the IPFILTER.LICENCE file for details on licencing.
7bc4097aaSchristos *
8*13885a66Sdarrenr * Id: mutex_emul.c,v 1.1.1.2 2012/07/22 13:44:39 darrenr Exp $
9bc4097aaSchristos */
10bc4097aaSchristos
11bc4097aaSchristos #include "ipf.h"
12bc4097aaSchristos
13bc4097aaSchristos #define EMM_MAGIC 0x9d7adba3
14bc4097aaSchristos
15bc4097aaSchristos static int mutex_debug = 0;
16c9d5dc6cSdarrenr static FILE *mutex_file = NULL;
17c9d5dc6cSdarrenr static int initcount = 0;
18bc4097aaSchristos
19c9d5dc6cSdarrenr void
eMmutex_enter(mtx,file,line)20c9d5dc6cSdarrenr eMmutex_enter(mtx, file, line)
21bc4097aaSchristos eMmutex_t *mtx;
22bc4097aaSchristos char *file;
23bc4097aaSchristos int line;
24bc4097aaSchristos {
25bc4097aaSchristos if (mutex_debug & 2)
26c9d5dc6cSdarrenr fprintf(mutex_file, "%s:%d:eMmutex_enter(%s)\n", file, line,
27bc4097aaSchristos mtx->eMm_owner);
28bc4097aaSchristos if (mtx->eMm_magic != EMM_MAGIC) {
29bc4097aaSchristos fprintf(stderr, "%s:eMmutex_enter(%p): bad magic: %#x\n",
30bc4097aaSchristos mtx->eMm_owner, mtx, mtx->eMm_magic);
31bc4097aaSchristos abort();
32bc4097aaSchristos }
33bc4097aaSchristos if (mtx->eMm_held != 0) {
34bc4097aaSchristos fprintf(stderr, "%s:eMmutex_enter(%p): already locked: %d\n",
35bc4097aaSchristos mtx->eMm_owner, mtx, mtx->eMm_held);
36bc4097aaSchristos abort();
37bc4097aaSchristos }
38bc4097aaSchristos mtx->eMm_held++;
39bc4097aaSchristos mtx->eMm_heldin = file;
40bc4097aaSchristos mtx->eMm_heldat = line;
41bc4097aaSchristos }
42bc4097aaSchristos
43bc4097aaSchristos
44c9d5dc6cSdarrenr void
eMmutex_exit(mtx,file,line)45c9d5dc6cSdarrenr eMmutex_exit(mtx, file, line)
46bc4097aaSchristos eMmutex_t *mtx;
47bc4097aaSchristos char *file;
48bc4097aaSchristos int line;
49bc4097aaSchristos {
50bc4097aaSchristos if (mutex_debug & 2)
51c9d5dc6cSdarrenr fprintf(mutex_file, "%s:%d:eMmutex_exit(%s)\n", file, line,
52bc4097aaSchristos mtx->eMm_owner);
53bc4097aaSchristos if (mtx->eMm_magic != EMM_MAGIC) {
54bc4097aaSchristos fprintf(stderr, "%s:eMmutex_exit(%p): bad magic: %#x\n",
55bc4097aaSchristos mtx->eMm_owner, mtx, mtx->eMm_magic);
56bc4097aaSchristos abort();
57bc4097aaSchristos }
58bc4097aaSchristos if (mtx->eMm_held != 1) {
59bc4097aaSchristos fprintf(stderr, "%s:eMmutex_exit(%p): not locked: %d\n",
60bc4097aaSchristos mtx->eMm_owner, mtx, mtx->eMm_held);
61bc4097aaSchristos abort();
62bc4097aaSchristos }
63bc4097aaSchristos mtx->eMm_held--;
64bc4097aaSchristos mtx->eMm_heldin = NULL;
65bc4097aaSchristos mtx->eMm_heldat = 0;
66bc4097aaSchristos }
67bc4097aaSchristos
68bc4097aaSchristos
69c9d5dc6cSdarrenr void
eMmutex_init(mtx,who,file,line)70c9d5dc6cSdarrenr eMmutex_init(mtx, who, file, line)
71bc4097aaSchristos eMmutex_t *mtx;
72bc4097aaSchristos char *who;
73bc4097aaSchristos char *file;
74bc4097aaSchristos int line;
75bc4097aaSchristos {
76c9d5dc6cSdarrenr if (mutex_file == NULL && mutex_debug)
77c9d5dc6cSdarrenr mutex_file = fopen("ipf_mutex_log", "w");
78bc4097aaSchristos if (mutex_debug & 1)
79c9d5dc6cSdarrenr fprintf(mutex_file, "%s:%d:eMmutex_init(%p,%s)\n",
80bc4097aaSchristos file, line, mtx, who);
81bc4097aaSchristos if (mtx->eMm_magic == EMM_MAGIC) { /* safe bet ? */
82bc4097aaSchristos fprintf(stderr,
83bc4097aaSchristos "%s:eMmutex_init(%p): already initialised?: %#x\n",
84bc4097aaSchristos mtx->eMm_owner, mtx, mtx->eMm_magic);
85bc4097aaSchristos abort();
86bc4097aaSchristos }
87bc4097aaSchristos mtx->eMm_magic = EMM_MAGIC;
88bc4097aaSchristos mtx->eMm_held = 0;
89bc4097aaSchristos if (who != NULL)
90bc4097aaSchristos mtx->eMm_owner = strdup(who);
91bc4097aaSchristos else
92bc4097aaSchristos mtx->eMm_owner = NULL;
93bc4097aaSchristos initcount++;
94bc4097aaSchristos }
95bc4097aaSchristos
96bc4097aaSchristos
97c9d5dc6cSdarrenr void
eMmutex_destroy(mtx,file,line)98c9d5dc6cSdarrenr eMmutex_destroy(mtx, file, line)
99bc4097aaSchristos eMmutex_t *mtx;
100bc4097aaSchristos char *file;
101bc4097aaSchristos int line;
102bc4097aaSchristos {
103bc4097aaSchristos if (mutex_debug & 1)
104c9d5dc6cSdarrenr fprintf(mutex_file,
105c9d5dc6cSdarrenr "%s:%d:eMmutex_destroy(%p,%s)\n", file, line,
106bc4097aaSchristos mtx, mtx->eMm_owner);
107bc4097aaSchristos if (mtx->eMm_magic != EMM_MAGIC) {
108bc4097aaSchristos fprintf(stderr, "%s:eMmutex_destroy(%p): bad magic: %#x\n",
109bc4097aaSchristos mtx->eMm_owner, mtx, mtx->eMm_magic);
110bc4097aaSchristos abort();
111bc4097aaSchristos }
112bc4097aaSchristos if (mtx->eMm_held != 0) {
113c9d5dc6cSdarrenr fprintf(stderr,
114c9d5dc6cSdarrenr "%s:eMmutex_enter(%p): still locked: %d\n",
115bc4097aaSchristos mtx->eMm_owner, mtx, mtx->eMm_held);
116bc4097aaSchristos abort();
117bc4097aaSchristos }
118bc4097aaSchristos if (mtx->eMm_owner != NULL)
119bc4097aaSchristos free(mtx->eMm_owner);
120bc4097aaSchristos memset(mtx, 0xa5, sizeof(*mtx));
121bc4097aaSchristos initcount--;
122bc4097aaSchristos }
123bc4097aaSchristos
124bc4097aaSchristos
125c9d5dc6cSdarrenr void
ipf_mutex_clean()126c9d5dc6cSdarrenr ipf_mutex_clean()
127bc4097aaSchristos {
128c9d5dc6cSdarrenr if (initcount != 0) {
129c9d5dc6cSdarrenr if (mutex_file)
130c9d5dc6cSdarrenr fprintf(mutex_file, "initcount %d\n", initcount);
131bc4097aaSchristos abort();
132bc4097aaSchristos }
133c9d5dc6cSdarrenr }
134