mutex_emul.c revision 285830
1/* $FreeBSD: releng/10.2/contrib/ipfilter/lib/mutex_emul.c 255332 2013-09-06 23:11:19Z cy $ */ 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 15static int mutex_debug = 0; 16static FILE *mutex_file = NULL; 17static int initcount = 0; 18 19void 20eMmutex_enter(mtx, file, line) 21 eMmutex_t *mtx; 22 char *file; 23 int line; 24{ 25 if (mutex_debug & 2) 26 fprintf(mutex_file, "%s:%d:eMmutex_enter(%s)\n", file, line, 27 mtx->eMm_owner); 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); 31 abort(); 32 } 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); 36 abort(); 37 } 38 mtx->eMm_held++; 39 mtx->eMm_heldin = file; 40 mtx->eMm_heldat = line; 41} 42 43 44void 45eMmutex_exit(mtx, file, line) 46 eMmutex_t *mtx; 47 char *file; 48 int line; 49{ 50 if (mutex_debug & 2) 51 fprintf(mutex_file, "%s:%d:eMmutex_exit(%s)\n", file, line, 52 mtx->eMm_owner); 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); 56 abort(); 57 } 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); 61 abort(); 62 } 63 mtx->eMm_held--; 64 mtx->eMm_heldin = NULL; 65 mtx->eMm_heldat = 0; 66} 67 68 69void 70eMmutex_init(mtx, who, file, line) 71 eMmutex_t *mtx; 72 char *who; 73 char *file; 74 int line; 75{ 76 if (mutex_file == NULL && mutex_debug) 77 mutex_file = fopen("ipf_mutex_log", "w"); 78 if (mutex_debug & 1) 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 ? */ 82 fprintf(stderr, 83 "%s:eMmutex_init(%p): already initialised?: %#x\n", 84 mtx->eMm_owner, mtx, mtx->eMm_magic); 85 abort(); 86 } 87 mtx->eMm_magic = EMM_MAGIC; 88 mtx->eMm_held = 0; 89 if (who != NULL) 90 mtx->eMm_owner = strdup(who); 91 else 92 mtx->eMm_owner = NULL; 93 initcount++; 94} 95 96 97void 98eMmutex_destroy(mtx, file, line) 99 eMmutex_t *mtx; 100 char *file; 101 int line; 102{ 103 if (mutex_debug & 1) 104 fprintf(mutex_file, 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); 110 abort(); 111 } 112 if (mtx->eMm_held != 0) { 113 fprintf(stderr, 114 "%s:eMmutex_enter(%p): still locked: %d\n", 115 mtx->eMm_owner, mtx, mtx->eMm_held); 116 abort(); 117 } 118 if (mtx->eMm_owner != NULL) 119 free(mtx->eMm_owner); 120 memset(mtx, 0xa5, sizeof(*mtx)); 121 initcount--; 122} 123 124 125void 126ipf_mutex_clean() 127{ 128 if (initcount != 0) { 129 if (mutex_file) 130 fprintf(mutex_file, "initcount %d\n", initcount); 131 abort(); 132 } 133} 134