1 /*****************************************************************************\
4 * Author : Chris Koeritz *
6 *******************************************************************************
7 * Copyright (c) 1996-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
15 // NOTE: we are explicitly avoiding use of new and delete here because this
16 // class is needed by our memory allocation object, which would be
17 // providing the new and delete methods.
27 #define _WINSOCKAPI_ // make windows.h happy about winsock.
33 mutex::mutex() { construct(); }
35 mutex::~mutex() { destruct(); }
37 void mutex::establish_lock() { lock(); }
39 void mutex::repeal_lock() { unlock(); }
41 void mutex::construct()
44 c_os_mutex = (CRITICAL_SECTION *)malloc(sizeof(CRITICAL_SECTION));
45 InitializeCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
46 #elif defined(__UNIX__)
47 pthread_mutexattr_t attr;
48 pthread_mutexattr_init(&attr);
51 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
53 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
56 //printf("failed to initialize mutex attributes!\n"); fflush(NIL);
58 c_os_mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
59 pthread_mutex_init((pthread_mutex_t *)c_os_mutex, &attr);
60 pthread_mutexattr_destroy(&attr);
62 #pragma error("no implementation of mutexes for this OS yet!")
66 void mutex::destruct()
73 if (!c_os_mutex) return; // already defunct.
75 DeleteCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
77 #elif defined(__UNIX__)
78 pthread_mutex_destroy((pthread_mutex_t *)c_os_mutex);
81 #pragma error("no implementation of mutexes for this OS yet!")
88 if (!c_os_mutex) return;
90 EnterCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
91 #elif defined(__UNIX__)
92 pthread_mutex_lock((pthread_mutex_t *)c_os_mutex);
94 #pragma error("no implementation of mutexes for this OS yet!")
100 if (!c_os_mutex) return;
102 LeaveCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
103 #elif defined(__UNIX__)
104 pthread_mutex_unlock((pthread_mutex_t *)c_os_mutex);
106 #pragma error("no implementation of mutexes for this OS yet!")