GNU Octave  4.2.1
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-2017 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 "octave-config.h"
27 
28 #include "oct-refcount.h"
29 
30 class octave_mutex;
31 
32 class
34 {
35 public:
36  friend class octave_mutex;
37 
38  octave_base_mutex (void) : count (1) { }
39 
40  virtual ~octave_base_mutex (void) { }
41 
42  virtual void lock (void);
43 
44  virtual void unlock (void);
45 
46  virtual bool try_lock (void);
47 
48 private:
50 };
51 
52 class
53 OCTAVE_API
55 {
56 public:
57  octave_mutex (void);
58 
60  : rep (m.rep)
61  {
62  rep->count++;
63  }
64 
66  {
67  if (--rep->count == 0)
68  delete rep;
69  }
70 
72  {
73  if (rep != m.rep)
74  {
75  if (--rep->count == 0)
76  delete rep;
77 
78  rep = m.rep;
79  rep->count++;
80  }
81 
82  return *this;
83  }
84 
85  void lock (void)
86  {
87  rep->lock ();
88  }
89 
90  void unlock (void)
91  {
92  rep->unlock ();
93  }
94 
95  bool try_lock (void)
96  {
97  return rep->try_lock ();
98  }
99 
100 protected:
102 };
103 
104 class
106 {
107 public:
108  octave_autolock (const octave_mutex& m, bool block = true)
109  : mutex (m), lock_result (false)
110  {
111  if (block)
112  {
113  mutex.lock ();
114  lock_result = true;
115  }
116  else
117  lock_result = mutex.try_lock ();
118  }
119 
121  {
122  if (lock_result)
123  mutex.unlock ();
124  }
125 
126  bool ok (void) const { return lock_result; }
127 
128  operator bool (void) const { return ok (); }
129 
130 private:
131 
132  // No copying or default constructor!
133  octave_autolock (void);
136 
137 private:
140 };
141 
142 class
143 OCTAVE_API
145 {
146 public:
147  static void init (void);
148 
149  static bool is_octave_thread (void);
150 };
151 
152 #endif
~octave_autolock(void)
Definition: oct-mutex.h:120
virtual void unlock(void)
Definition: oct-mutex.cc:43
octave_mutex mutex
Definition: oct-mutex.h:138
octave_base_mutex(void)
Definition: oct-mutex.h:38
octave_mutex(void)
Definition: oct-mutex.cc:175
bool try_lock(void)
Definition: oct-mutex.h:95
virtual void lock(void)
Definition: oct-mutex.cc:37
octave_refcount< int > count
Definition: oct-mutex.h:49
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
octave_mutex & operator=(const octave_mutex &m)
Definition: oct-mutex.h:71
octave_mutex(const octave_mutex &m)
Definition: oct-mutex.h:59
octave_base_mutex * rep
Definition: oct-mutex.h:101
is false
Definition: cellfun.cc:398
virtual bool try_lock(void)
Definition: oct-mutex.cc:49
~octave_mutex(void)
Definition: oct-mutex.h:65
octave_autolock(const octave_mutex &m, bool block=true)
Definition: oct-mutex.h:108
bool ok(void) const
Definition: oct-mutex.h:126
virtual ~octave_base_mutex(void)
Definition: oct-mutex.h:40
void lock(void)
Definition: oct-mutex.h:85
void unlock(void)
Definition: oct-mutex.h:90