feisty meow concerns codebase  2.140
mutex.cpp
Go to the documentation of this file.
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 /*
16  NOTE: we are explicitly avoiding use of new and delete here because this
17  class is needed by our memory allocation object, which would be providing
18  the new and delete methods.
19 
20  this class also does not participate in the FUNCDEF macros or anything that
21  would trigger activity in the callstack_tracker, because that object also
22  needs to use mutexes.
23 */
24 
25 #include "mutex.h"
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #ifdef __UNIX__
31  #include <pthread.h>
32 #endif
33 #ifdef __WIN32__
34  #include <synchapi.h>
35 /*
36  #define _WINSOCKAPI_ // make windows.h happy about winsock.
37  // winsock support...
38 // #undef FD_SETSIZE
39 // #define FD_SETSIZE 1000
40  // if you don't set this, you can only select on a default of 64 sockets.
41  #include <winsock2.h>
42  #include <windows.h>
43  */
44 #endif
45 
46 namespace basis {
47 
49 
51 
53 
55 
57 {
58 #ifdef __WIN32__
59  c_os_mutex = (CRITICAL_SECTION *)malloc(sizeof(CRITICAL_SECTION));
60  InitializeCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
61 #elif defined(__UNIX__)
62  pthread_mutexattr_t attr;
63  pthread_mutexattr_init(&attr);
64  int ret = -1;
65 #ifdef __APPLE__
66  ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
67 #else
68  ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
69 #endif
70  if (ret != 0) {
71  printf("failed to initialize mutex attributes!\n"); fflush(NULL_POINTER);
72  }
73  c_os_mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
74  pthread_mutex_init((pthread_mutex_t *)c_os_mutex, &attr);
75  pthread_mutexattr_destroy(&attr);
76 #else
77  #pragma error("no implementation of mutexes for this OS yet!")
78 #endif
79 }
80 
82 {
83  defang();
84 }
85 
86 void mutex::defang()
87 {
88  if (!c_os_mutex) return; // already defunct.
89 #ifdef __WIN32__
90  DeleteCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
91  free(c_os_mutex);
92 #elif defined(__UNIX__)
93  pthread_mutex_destroy((pthread_mutex_t *)c_os_mutex);
94  free(c_os_mutex);
95 #else
96  #pragma error("no implementation of mutexes for this OS yet!")
97 #endif
98  c_os_mutex = 0;
99 }
100 
102 {
103  if (!c_os_mutex) return;
104 #ifdef __WIN32__
105  EnterCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
106 #elif defined(__UNIX__)
107  pthread_mutex_lock((pthread_mutex_t *)c_os_mutex);
108 #else
109  #pragma error("no implementation of mutexes for this OS yet!")
110 #endif
111 }
112 
114 {
115  if (!c_os_mutex) return;
116 #ifdef __WIN32__
117  LeaveCriticalSection((LPCRITICAL_SECTION)c_os_mutex);
118 #elif defined(__UNIX__)
119  pthread_mutex_unlock((pthread_mutex_t *)c_os_mutex);
120 #else
121  #pragma error("no implementation of mutexes for this OS yet!")
122 #endif
123 }
124 
125 } //namespace.
126 
virtual ~mutex()
Destroys the mutex. It should not be locked upon destruction.
Definition: mutex.cpp:50
void construct()
Constructor for use with malloc/free instead of new/delete.
Definition: mutex.cpp:56
void destruct()
Destructor for use with malloc/free instead of new/delete.
Definition: mutex.cpp:81
void lock()
Clamps down on the mutex, if possible.
Definition: mutex.cpp:101
virtual void establish_lock()
Satisfies base class requirements for locking.
Definition: mutex.cpp:52
void unlock()
Gives up the possession of the mutex.
Definition: mutex.cpp:113
virtual void repeal_lock()
Satisfies base class requirements for unlocking.
Definition: mutex.cpp:54
mutex()
Constructs a new mutex.
Definition: mutex.cpp:48
#define NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30