4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
13 #define EMM_MAGIC 0x9d7adba3
15 static int mutex_debug = 0;
16 static FILE *mutex_file = NULL;
17 static int initcount = 0;
20 eMmutex_enter(mtx, file, line)
26 fprintf(mutex_file, "%s:%d:eMmutex_enter(%s)\n", file, line,
28 if (mtx->eMm_magic != EMM_MAGIC) {
29 fprintf(stderr, "%s:eMmutex_enter(%p): bad magic: %#x\n",
30 mtx->eMm_owner, mtx, mtx->eMm_magic);
33 if (mtx->eMm_held != 0) {
34 fprintf(stderr, "%s:eMmutex_enter(%p): already locked: %d\n",
35 mtx->eMm_owner, mtx, mtx->eMm_held);
39 mtx->eMm_heldin = file;
40 mtx->eMm_heldat = line;
45 eMmutex_exit(mtx, file, line)
51 fprintf(mutex_file, "%s:%d:eMmutex_exit(%s)\n", file, line,
53 if (mtx->eMm_magic != EMM_MAGIC) {
54 fprintf(stderr, "%s:eMmutex_exit(%p): bad magic: %#x\n",
55 mtx->eMm_owner, mtx, mtx->eMm_magic);
58 if (mtx->eMm_held != 1) {
59 fprintf(stderr, "%s:eMmutex_exit(%p): not locked: %d\n",
60 mtx->eMm_owner, mtx, mtx->eMm_held);
64 mtx->eMm_heldin = NULL;
70 eMmutex_init(mtx, who, file, line)
76 if (mutex_file == NULL && mutex_debug)
77 mutex_file = fopen("ipf_mutex_log", "w");
79 fprintf(mutex_file, "%s:%d:eMmutex_init(%p,%s)\n",
80 file, line, mtx, who);
81 if (mtx->eMm_magic == EMM_MAGIC) { /* safe bet ? */
83 "%s:eMmutex_init(%p): already initialised?: %#x\n",
84 mtx->eMm_owner, mtx, mtx->eMm_magic);
87 mtx->eMm_magic = EMM_MAGIC;
90 mtx->eMm_owner = strdup(who);
92 mtx->eMm_owner = NULL;
98 eMmutex_destroy(mtx, file, line)
105 "%s:%d:eMmutex_destroy(%p,%s)\n", file, line,
106 mtx, mtx->eMm_owner);
107 if (mtx->eMm_magic != EMM_MAGIC) {
108 fprintf(stderr, "%s:eMmutex_destroy(%p): bad magic: %#x\n",
109 mtx->eMm_owner, mtx, mtx->eMm_magic);
112 if (mtx->eMm_held != 0) {
114 "%s:eMmutex_enter(%p): still locked: %d\n",
115 mtx->eMm_owner, mtx, mtx->eMm_held);
118 if (mtx->eMm_owner != NULL)
119 free(mtx->eMm_owner);
120 memset(mtx, 0xa5, sizeof(*mtx));
128 if (initcount != 0) {
130 fprintf(mutex_file, "initcount %d\n", initcount);