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