feisty meow concerns codebase  2.140
mutex.h
Go to the documentation of this file.
1 #ifndef MUTEX_CLASS
2 #define MUTEX_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : mutex *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 1996-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include "contracts.h"
19 
21 
30 namespace basis {
31 
32 class mutex : public virtual base_synchronizer
33 {
34 public:
35  mutex();
36 
37  virtual ~mutex();
39 
41  void construct();
42 
44  void destruct();
45 
46  void lock();
48 
50  void unlock();
52 
53  virtual void establish_lock();
55  virtual void repeal_lock();
57 
58 private:
59  void *c_os_mutex;
60 
61  void defang();
63 
68  mutex(const mutex &);
69  mutex &operator =(const mutex &);
70 };
71 
73 
75 
113 {
114 public:
115  auto_synchronizer(base_synchronizer &locker) : _locker(locker)
116  { _locker.establish_lock(); }
118 
123  ~auto_synchronizer() { _locker.repeal_lock(); }
125 
126 private:
127  base_synchronizer &_locker;
128 
129  // disallowed.
130  auto_synchronizer(const auto_synchronizer &locker);
131  auto_synchronizer &operator =(const auto_synchronizer &locker);
132 };
133 
134 } //namespace.
135 
136 #endif
137 
auto_synchronizer simplifies concurrent code by automatically unlocking.
Definition: mutex.h:113
~auto_synchronizer()
Releases the lock as this object goes out of scope.
Definition: mutex.h:122
auto_synchronizer(base_synchronizer &locker)
Construction locks the "locker" object for the current program scope.
Definition: mutex.h:115
Interface for a simple form of synchronization.
Definition: contracts.h:98
virtual void establish_lock()=0
virtual void repeal_lock()=0
virtual ~mutex()
Destroys the mutex. It should not be locked upon destruction.
Definition: mutex.cpp:44
void construct()
Constructor for use with malloc/free instead of new/delete.
Definition: mutex.cpp:50
void destruct()
Destructor for use with malloc/free instead of new/delete.
Definition: mutex.cpp:75
void lock()
Clamps down on the mutex, if possible.
Definition: mutex.cpp:95
virtual void establish_lock()
Satisfies base class requirements for locking.
Definition: mutex.cpp:46
void unlock()
Gives up the possession of the mutex.
Definition: mutex.cpp:107
virtual void repeal_lock()
Satisfies base class requirements for unlocking.
Definition: mutex.cpp:48
mutex()
Constructs a new mutex.
Definition: mutex.cpp:42
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30