first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / basis / mutex.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : mutex                                                             *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
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 \*****************************************************************************/
14
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.
18
19 #include "mutex.h"
20
21 #include <stdlib.h>
22
23 #ifdef __UNIX__
24   #include <pthread.h>
25 #endif
26 #ifdef __WIN32__
27   #define _WINSOCKAPI_  // make windows.h happy about winsock.
28   #include <windows.h>
29 #endif
30
31 namespace basis {
32
33 mutex::mutex() { construct(); }
34
35 mutex::~mutex() { destruct(); }
36
37 void mutex::establish_lock() { lock(); }
38
39 void mutex::repeal_lock() { unlock(); }
40
41 void mutex::construct()
42 {
43 #ifdef __WIN32__
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);
49   int ret = -1;
50 #ifdef __APPLE__
51   ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
52 #else
53   ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
54 #endif
55   if (ret != 0) {
56 //printf("failed to initialize mutex attributes!\n"); fflush(NIL);
57   }
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);
61 #else
62   #pragma error("no implementation of mutexes for this OS yet!")
63 #endif
64 }
65
66 void mutex::destruct()
67 {
68   defang();
69 }
70
71 void mutex::defang()
72 {
73   if (!c_os_mutex) return;  // already defunct.
74 #ifdef __WIN32__
75   DeleteCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
76   free(c_os_mutex);
77 #elif defined(__UNIX__)
78   pthread_mutex_destroy((pthread_mutex_t *)c_os_mutex);
79   free(c_os_mutex);
80 #else
81   #pragma error("no implementation of mutexes for this OS yet!")
82 #endif
83   c_os_mutex = 0;
84 }
85
86 void mutex::lock()
87 {
88   if (!c_os_mutex) return;
89 #ifdef __WIN32__
90   EnterCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
91 #elif defined(__UNIX__)
92   pthread_mutex_lock((pthread_mutex_t *)c_os_mutex);
93 #else
94   #pragma error("no implementation of mutexes for this OS yet!")
95 #endif
96 }
97
98 void mutex::unlock()
99 {
100   if (!c_os_mutex) return;
101 #ifdef __WIN32__
102   LeaveCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
103 #elif defined(__UNIX__)
104   pthread_mutex_unlock((pthread_mutex_t *)c_os_mutex);
105 #else
106   #pragma error("no implementation of mutexes for this OS yet!")
107 #endif
108 }
109
110 } //namespace.
111