1 /* $FreeBSD$ */ 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 0x97dd8b3a 14 15 void eMrwlock_read_enter(eMrwlock_t *rw, char *file, int line) 16 { 17 if (rw->eMrw_magic != EMM_MAGIC) { 18 fprintf(stderr, "%s:eMrwlock_read_enter(%p): bad magic: %#x\n", 19 rw->eMrw_owner, rw, rw->eMrw_magic); 20 abort(); 21 } 22 if (rw->eMrw_read != 0 || rw->eMrw_write != 0) { 23 fprintf(stderr, 24 "%s:eMrwlock_read_enter(%p): already locked: %d/%d\n", 25 rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write); 26 abort(); 27 } 28 rw->eMrw_read++; 29 rw->eMrw_heldin = file; 30 rw->eMrw_heldat = line; 31 } 32 33 34 void eMrwlock_write_enter(eMrwlock_t *rw, char *file, int line) 35 { 36 if (rw->eMrw_magic != EMM_MAGIC) { 37 fprintf(stderr, "%s:eMrwlock_write_enter(%p): bad magic: %#x\n", 38 rw->eMrw_owner, rw, rw->eMrw_magic); 39 abort(); 40 } 41 if (rw->eMrw_read != 0 || rw->eMrw_write != 0) { 42 fprintf(stderr, 43 "%s:eMrwlock_write_enter(%p): already locked: %d/%d\n", 44 rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write); 45 abort(); 46 } 47 rw->eMrw_write++; 48 rw->eMrw_heldin = file; 49 rw->eMrw_heldat = line; 50 } 51 52 53 void eMrwlock_try_upgrade(eMrwlock_t *rw, char *file, int line) 54 { 55 if (rw->eMrw_magic != EMM_MAGIC) { 56 fprintf(stderr, "%s:eMrwlock_write_enter(%p): bad magic: %#x\n", 57 rw->eMrw_owner, rw, rw->eMrw_magic); 58 abort(); 59 } 60 if (rw->eMrw_read != 0 || rw->eMrw_write != 0) { 61 fprintf(stderr, 62 "%s:eMrwlock_try_upgrade(%p): already locked: %d/%d\n", 63 rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write); 64 abort(); 65 } 66 rw->eMrw_write++; 67 rw->eMrw_heldin = file; 68 rw->eMrw_heldat = line; 69 } 70 71 void eMrwlock_downgrade(eMrwlock_t *rw, char *file, int line) 72 { 73 if (rw->eMrw_magic != EMM_MAGIC) { 74 fprintf(stderr, "%s:eMrwlock_write_enter(%p): bad magic: %#x\n", 75 rw->eMrw_owner, rw, rw->eMrw_magic); 76 abort(); 77 } 78 if (rw->eMrw_read != 0 || rw->eMrw_write != 1) { 79 fprintf(stderr, 80 "%s:eMrwlock_write_enter(%p): already locked: %d/%d\n", 81 rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write); 82 abort(); 83 } 84 rw->eMrw_write--; 85 rw->eMrw_read++; 86 rw->eMrw_heldin = file; 87 rw->eMrw_heldat = line; 88 } 89 90 91 void eMrwlock_exit(eMrwlock_t *rw) 92 { 93 if (rw->eMrw_magic != EMM_MAGIC) { 94 fprintf(stderr, "%s:eMrwlock_exit(%p): bad magic: %#x\n", 95 rw->eMrw_owner, rw, rw->eMrw_magic); 96 abort(); 97 } 98 if (rw->eMrw_read != 1 && rw->eMrw_write != 1) { 99 fprintf(stderr, "%s:eMrwlock_exit(%p): not locked: %d/%d\n", 100 rw->eMrw_owner, rw, rw->eMrw_read, rw->eMrw_write); 101 abort(); 102 } 103 if (rw->eMrw_read == 1) 104 rw->eMrw_read--; 105 else if (rw->eMrw_write == 1) 106 rw->eMrw_write--; 107 rw->eMrw_heldin = NULL; 108 rw->eMrw_heldat = 0; 109 } 110 111 112 static int initcount = 0; 113 114 void eMrwlock_init(eMrwlock_t *rw, char *who) 115 { 116 if (rw->eMrw_magic == EMM_MAGIC) { /* safe bet ? */ 117 fprintf(stderr, 118 "%s:eMrwlock_init(%p): already initialised?: %#x\n", 119 rw->eMrw_owner, rw, rw->eMrw_magic); 120 abort(); 121 } 122 rw->eMrw_magic = EMM_MAGIC; 123 rw->eMrw_read = 0; 124 rw->eMrw_write = 0; 125 if (who != NULL) 126 rw->eMrw_owner = strdup(who); 127 else 128 rw->eMrw_owner = NULL; 129 initcount++; 130 } 131 132 133 void eMrwlock_destroy(eMrwlock_t *rw) 134 { 135 if (rw->eMrw_magic != EMM_MAGIC) { 136 fprintf(stderr, "%s:eMrwlock_destroy(%p): bad magic: %#x\n", 137 rw->eMrw_owner, rw, rw->eMrw_magic); 138 abort(); 139 } 140 if (rw->eMrw_owner != NULL) 141 free(rw->eMrw_owner); 142 memset(rw, 0xa5, sizeof(*rw)); 143 initcount--; 144 } 145 146 void ipf_rwlock_clean(void) 147 { 148 if (initcount != 0) 149 abort(); 150 } 151