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.
31 // #define FD_SETSIZE 1000
32 // if you don't set this, you can only select on a default of 64 sockets.
39 mutex::mutex() { construct(); }
41 mutex::~mutex() { destruct(); }
43 void mutex::establish_lock() { lock(); }
45 void mutex::repeal_lock() { unlock(); }
47 void mutex::construct()
50 c_os_mutex = (CRITICAL_SECTION *)malloc(sizeof(CRITICAL_SECTION));
51 InitializeCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
52 #elif defined(__UNIX__)
53 pthread_mutexattr_t attr;
54 pthread_mutexattr_init(&attr);
57 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
59 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
62 printf("failed to initialize mutex attributes!\n"); fflush(NULL_POINTER);
64 c_os_mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
65 pthread_mutex_init((pthread_mutex_t *)c_os_mutex, &attr);
66 pthread_mutexattr_destroy(&attr);
68 #pragma error("no implementation of mutexes for this OS yet!")
72 void mutex::destruct()
79 if (!c_os_mutex) return; // already defunct.
81 DeleteCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
83 #elif defined(__UNIX__)
84 pthread_mutex_destroy((pthread_mutex_t *)c_os_mutex);
87 #pragma error("no implementation of mutexes for this OS yet!")
94 if (!c_os_mutex) return;
96 EnterCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
97 #elif defined(__UNIX__)
98 pthread_mutex_lock((pthread_mutex_t *)c_os_mutex);
100 #pragma error("no implementation of mutexes for this OS yet!")
106 if (!c_os_mutex) return;
108 LeaveCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
109 #elif defined(__UNIX__)
110 pthread_mutex_unlock((pthread_mutex_t *)c_os_mutex);
112 #pragma error("no implementation of mutexes for this OS yet!")