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.
28 #define _WINSOCKAPI_ // make windows.h happy about winsock.
34 mutex::mutex() { construct(); }
36 mutex::~mutex() { destruct(); }
38 void mutex::establish_lock() { lock(); }
40 void mutex::repeal_lock() { unlock(); }
42 void mutex::construct()
45 c_os_mutex = (CRITICAL_SECTION *)malloc(sizeof(CRITICAL_SECTION));
46 InitializeCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
47 #elif defined(__UNIX__)
48 pthread_mutexattr_t attr;
49 pthread_mutexattr_init(&attr);
52 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
54 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
57 printf("failed to initialize mutex attributes!\n"); fflush(NIL);
59 c_os_mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
60 pthread_mutex_init((pthread_mutex_t *)c_os_mutex, &attr);
61 pthread_mutexattr_destroy(&attr);
63 #pragma error("no implementation of mutexes for this OS yet!")
67 void mutex::destruct()
74 if (!c_os_mutex) return; // already defunct.
76 DeleteCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
78 #elif defined(__UNIX__)
79 pthread_mutex_destroy((pthread_mutex_t *)c_os_mutex);
82 #pragma error("no implementation of mutexes for this OS yet!")
89 if (!c_os_mutex) return;
91 EnterCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
92 #elif defined(__UNIX__)
93 pthread_mutex_lock((pthread_mutex_t *)c_os_mutex);
95 #pragma error("no implementation of mutexes for this OS yet!")
101 if (!c_os_mutex) return;
103 LeaveCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
104 #elif defined(__UNIX__)
105 pthread_mutex_unlock((pthread_mutex_t *)c_os_mutex);
107 #pragma error("no implementation of mutexes for this OS yet!")