GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-mutex.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2008-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_oct_mutex_h)
24 #define octave_oct_mutex_h 1
25 
26 #include "octave-config.h"
27 
28 #include "oct-refcount.h"
29 
30 namespace octave
31 {
32  class mutex;
33 
34  class
36  {
37  public:
38  friend class mutex;
39 
40  base_mutex (void) : count (1) { }
41 
42  virtual ~base_mutex (void) = default;
43 
44  virtual void lock (void);
45 
46  virtual void unlock (void);
47 
48  virtual bool try_lock (void);
49 
50  private:
52  };
53 
54  class
55  OCTAVE_API
56  mutex
57  {
58  public:
59  mutex (void);
60 
61  mutex (const mutex& m)
62  : rep (m.rep)
63  {
64  rep->count++;
65  }
66 
67  ~mutex (void)
68  {
69  if (--rep->count == 0)
70  delete rep;
71  }
72 
73  mutex& operator = (const mutex& m)
74  {
75  if (rep != m.rep)
76  {
77  if (--rep->count == 0)
78  delete rep;
79 
80  rep = m.rep;
81  rep->count++;
82  }
83 
84  return *this;
85  }
86 
87  void lock (void)
88  {
89  rep->lock ();
90  }
91 
92  void unlock (void)
93  {
94  rep->unlock ();
95  }
96 
97  bool try_lock (void)
98  {
99  return rep->try_lock ();
100  }
101 
102  protected:
104  };
105 
106  class
107  autolock
108  {
109  public:
110  autolock (const mutex& m, bool block = true)
111  : m_mutex (m), m_lock_result (false)
112  {
113  if (block)
114  {
115  m_mutex.lock ();
116  m_lock_result = true;
117  }
118  else
119  m_lock_result = m_mutex.try_lock ();
120  }
121 
122  // No copying.
123 
124  autolock (const autolock&) = delete;
125 
126  autolock& operator = (const autolock&) = delete;
127 
128  ~autolock (void)
129  {
130  if (m_lock_result)
131  m_mutex.unlock ();
132  }
133 
134  bool ok (void) const { return m_lock_result; }
135 
136  operator bool (void) const { return ok (); }
137 
138  private:
139 
141 
143  };
144 
145 
146  class
147  OCTAVE_API
148  thread
149  {
150  public:
151 
152  static void init (void);
153 
154  static bool is_thread (void);
155 
156  OCTAVE_DEPRECATED (4.4, "use 'is_thread' instead")
157  static bool is_octave_thread (void) { return is_thread (); }
158  };
159 }
160 
161 #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)
162 
163 OCTAVE_DEPRECATED (4.4, "use 'octave::mutex' instead")
164 typedef octave::mutex octave_mutex;
165 
166 OCTAVE_DEPRECATED (4.4, "use 'octave::base_mutex' instead")
167 typedef octave::base_mutex octave_base_mutex;
168 
169 OCTAVE_DEPRECATED (4.4, "use 'octave::autolock' instead")
170 typedef octave::autolock octave_autolock;
171 
172 OCTAVE_DEPRECATED (4.4, "use 'octave::thread' instead")
173 typedef octave::thread octave_thread;
174 
175 #endif
176 
177 #endif
base_mutex * rep
Definition: oct-mutex.h:103
mutex(const mutex &m)
Definition: oct-mutex.h:61
void unlock(void)
Definition: oct-mutex.h:92
refcount< int > count
Definition: oct-mutex.h:51
autolock(const mutex &m, bool block=true)
Definition: oct-mutex.h:110
is false
Definition: cellfun.cc:400
bool try_lock(void)
Definition: oct-mutex.h:97
bool ok(void) const
Definition: oct-mutex.h:134
~mutex(void)
Definition: oct-mutex.h:67
void lock(void)
Definition: oct-mutex.h:87