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.
30 #define _WINSOCKAPI_ // make windows.h happy about winsock.
33 // #define FD_SETSIZE 1000
34 // if you don't set this, you can only select on a default of 64 sockets.
42 mutex::mutex() { construct(); }
44 mutex::~mutex() { destruct(); }
46 void mutex::establish_lock() { lock(); }
48 void mutex::repeal_lock() { unlock(); }
50 void mutex::construct()
53 c_os_mutex = (CRITICAL_SECTION *)malloc(sizeof(CRITICAL_SECTION));
54 InitializeCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
55 #elif defined(__UNIX__)
56 pthread_mutexattr_t attr;
57 pthread_mutexattr_init(&attr);
60 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
62 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
65 printf("failed to initialize mutex attributes!\n"); fflush(NULL_POINTER);
67 c_os_mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
68 pthread_mutex_init((pthread_mutex_t *)c_os_mutex, &attr);
69 pthread_mutexattr_destroy(&attr);
71 #pragma error("no implementation of mutexes for this OS yet!")
75 void mutex::destruct()
82 if (!c_os_mutex) return; // already defunct.
84 DeleteCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
86 #elif defined(__UNIX__)
87 pthread_mutex_destroy((pthread_mutex_t *)c_os_mutex);
90 #pragma error("no implementation of mutexes for this OS yet!")
97 if (!c_os_mutex) return;
99 EnterCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
100 #elif defined(__UNIX__)
101 pthread_mutex_lock((pthread_mutex_t *)c_os_mutex);
103 #pragma error("no implementation of mutexes for this OS yet!")
109 if (!c_os_mutex) return;
111 LeaveCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
112 #elif defined(__UNIX__)
113 pthread_mutex_unlock((pthread_mutex_t *)c_os_mutex);
115 #pragma error("no implementation of mutexes for this OS yet!")