GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-mutex.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2008-2013 Michael Goffioul
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_oct_mutex_h)
24 #define octave_oct_mutex_h 1
25 
26 #include "oct-refcount.h"
27 
28 class octave_mutex;
29 
30 class
32 {
33 public:
34  friend class octave_mutex;
35 
36  octave_base_mutex (void) : count (1) { }
37 
38  virtual ~octave_base_mutex (void) { }
39 
40  virtual void lock (void);
41 
42  virtual void unlock (void);
43 
44  virtual bool try_lock (void);
45 
46 private:
48 };
49 
50 class
51 OCTAVE_API
53 {
54 public:
55  octave_mutex (void);
56 
58  : rep (m.rep)
59  {
60  rep->count++;
61  }
62 
64  {
65  if (--rep->count == 0)
66  delete rep;
67  }
68 
70  {
71  if (rep != m.rep)
72  {
73  if (--rep->count == 0)
74  delete rep;
75 
76  rep = m.rep;
77  rep->count++;
78  }
79 
80  return *this;
81  }
82 
83  void lock (void)
84  {
85  rep->lock ();
86  }
87 
88  void unlock (void)
89  {
90  rep->unlock ();
91  }
92 
93  bool try_lock (void)
94  {
95  return rep->try_lock ();
96  }
97 
98 protected:
100 };
101 
102 class
104 {
105 public:
106  octave_autolock (const octave_mutex& m, bool block = true)
107  : mutex (m), lock_result (false)
108  {
109  if (block)
110  {
111  mutex.lock ();
112  lock_result = true;
113  }
114  else
115  lock_result = mutex.try_lock ();
116  }
117 
119  {
120  if (lock_result)
121  mutex.unlock ();
122  }
123 
124  bool ok (void) const { return lock_result; }
125 
126  operator bool (void) const { return ok (); }
127 
128 private:
129 
130  // No copying or default constructor!
131  octave_autolock (void);
134 
135 private:
138 };
139 
140 class
141 OCTAVE_API
143 {
144 public:
145  static void init (void);
146 
147  static bool is_octave_thread (void);
148 };
149 
150 #endif