mutex_emul.c revision 302408
1239281Sgonzo/* $FreeBSD: stable/11/contrib/ipfilter/lib/mutex_emul.c 255332 2013-09-06 23:11:19Z cy $ */ 2239281Sgonzo 3239281Sgonzo/* 4239281Sgonzo * Copyright (C) 2012 by Darren Reed. 5239281Sgonzo * 6239281Sgonzo * See the IPFILTER.LICENCE file for details on licencing. 7239281Sgonzo * 8239281Sgonzo * $Id$ 9239281Sgonzo */ 10239281Sgonzo 11239281Sgonzo#include "ipf.h" 12239281Sgonzo 13239281Sgonzo#define EMM_MAGIC 0x9d7adba3 14239281Sgonzo 15239281Sgonzostatic int mutex_debug = 0; 16239281Sgonzostatic FILE *mutex_file = NULL; 17239281Sgonzostatic int initcount = 0; 18239281Sgonzo 19239281Sgonzovoid 20239281SgonzoeMmutex_enter(mtx, file, line) 21239281Sgonzo eMmutex_t *mtx; 22239281Sgonzo char *file; 23239281Sgonzo int line; 24239281Sgonzo{ 25239281Sgonzo if (mutex_debug & 2) 26239281Sgonzo fprintf(mutex_file, "%s:%d:eMmutex_enter(%s)\n", file, line, 27239281Sgonzo mtx->eMm_owner); 28239281Sgonzo if (mtx->eMm_magic != EMM_MAGIC) { 29239281Sgonzo fprintf(stderr, "%s:eMmutex_enter(%p): bad magic: %#x\n", 30239281Sgonzo mtx->eMm_owner, mtx, mtx->eMm_magic); 31239281Sgonzo abort(); 32239281Sgonzo } 33239281Sgonzo if (mtx->eMm_held != 0) { 34239281Sgonzo fprintf(stderr, "%s:eMmutex_enter(%p): already locked: %d\n", 35239281Sgonzo mtx->eMm_owner, mtx, mtx->eMm_held); 36239281Sgonzo abort(); 37239281Sgonzo } 38239281Sgonzo mtx->eMm_held++; 39239281Sgonzo 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