]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/ipfilter/lib/mutex_emul.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / ipfilter / lib / mutex_emul.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 2003 by Darren Reed.
5  * 
6  * See the IPFILTER.LICENCE file for details on licencing.  
7  *   
8  * $Id: mutex_emul.c,v 1.2.4.1 2006/06/16 17:21:06 darrenr Exp $ 
9  */     
10
11 #include "ipf.h"
12
13 #define EMM_MAGIC       0x9d7adba3
14
15 void eMmutex_enter(mtx, file, line)
16 eMmutex_t *mtx;
17 char *file;
18 int line;
19 {
20         if (mtx->eMm_magic != EMM_MAGIC) {
21                 fprintf(stderr, "%s:eMmutex_enter(%p): bad magic: %#x\n",
22                         mtx->eMm_owner, mtx, mtx->eMm_magic);
23                 abort();
24         }
25         if (mtx->eMm_held != 0) {
26                 fprintf(stderr, "%s:eMmutex_enter(%p): already locked: %d\n",
27                         mtx->eMm_owner, mtx, mtx->eMm_held);
28                 abort();
29         }
30         mtx->eMm_held++;
31         mtx->eMm_heldin = file;
32         mtx->eMm_heldat = line;
33 }
34
35
36 void eMmutex_exit(mtx)
37 eMmutex_t *mtx;
38 {
39         if (mtx->eMm_magic != EMM_MAGIC) {
40                 fprintf(stderr, "%s:eMmutex_exit(%p): bad magic: %#x\n",
41                         mtx->eMm_owner, mtx, mtx->eMm_magic);
42                 abort();
43         }
44         if (mtx->eMm_held != 1) {
45                 fprintf(stderr, "%s:eMmutex_exit(%p): not locked: %d\n",
46                         mtx->eMm_owner, mtx, mtx->eMm_held);
47                 abort();
48         }
49         mtx->eMm_held--;
50         mtx->eMm_heldin = NULL;
51         mtx->eMm_heldat = 0;
52 }
53
54
55 void eMmutex_init(mtx, who)
56 eMmutex_t *mtx;
57 char *who;
58 {
59         if (mtx->eMm_magic == EMM_MAGIC) {      /* safe bet ? */
60                 fprintf(stderr,
61                         "%s:eMmutex_init(%p): already initialised?: %#x\n",
62                         mtx->eMm_owner, mtx, mtx->eMm_magic);
63                 abort();
64         }
65         mtx->eMm_magic = EMM_MAGIC;
66         mtx->eMm_held = 0;
67         if (who != NULL)
68                 mtx->eMm_owner = strdup(who);
69         else
70                 mtx->eMm_owner = NULL;
71 }
72
73
74 void eMmutex_destroy(mtx)
75 eMmutex_t *mtx;
76 {
77         if (mtx->eMm_magic != EMM_MAGIC) {
78                 fprintf(stderr, "%s:eMmutex_destroy(%p): bad magic: %#x\n",
79                         mtx->eMm_owner, mtx, mtx->eMm_magic);
80                 abort();
81         }
82         if (mtx->eMm_held != 0) {
83                 fprintf(stderr, "%s:eMmutex_enter(%p): still locked: %d\n",
84                         mtx->eMm_owner, mtx, mtx->eMm_held);
85                 abort();
86         }
87         memset(mtx, 0xa5, sizeof(*mtx));
88 }