GNU Octave  6.2.0
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-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_oct_mutex_h)
27 #define octave_oct_mutex_h 1
28 
29 #include "octave-config.h"
30 
31 #include "oct-refcount.h"
32 
33 namespace octave
34 {
35  class mutex;
36 
37  class
39  {
40  public:
41  friend class mutex;
42 
43  base_mutex (void) : m_count (1) { }
44 
45  virtual ~base_mutex (void) = default;
46 
47  virtual void lock (void);
48 
49  virtual void unlock (void);
50 
51  virtual bool try_lock (void);
52 
53  private:
55  };
56 
57  class
58  OCTAVE_API
59  mutex
60  {
61  public:
62  mutex (void);
63 
64  mutex (const mutex& m)
65  : m_rep (m.m_rep)
66  {
67  m_rep->m_count++;
68  }
69 
70  ~mutex (void)
71  {
72  if (--m_rep->m_count == 0)
73  delete m_rep;
74  }
75 
76  mutex& operator = (const mutex& m)
77  {
78  if (m_rep != m.m_rep)
79  {
80  if (--m_rep->m_count == 0)
81  delete m_rep;
82 
83  m_rep = m.m_rep;
84  m_rep->m_count++;
85  }
86 
87  return *this;
88  }
89 
90  void lock (void)
91  {
92  m_rep->lock ();
93  }
94 
95  void unlock (void)
96  {
97  m_rep->unlock ();
98  }
99 
100  bool try_lock (void)
101  {
102  return m_rep->try_lock ();
103  }
104 
105  protected:
107  };
108 
109  class
110  autolock
111  {
112  public:
113  autolock (const mutex& m, bool block = true)
114  : m_mutex (m), m_lock_result (false)
115  {
116  if (block)
117  {
118  m_mutex.lock ();
119  m_lock_result = true;
120  }
121  else
122  m_lock_result = m_mutex.try_lock ();
123  }
124 
125  // No copying.
126 
127  autolock (const autolock&) = delete;
128 
129  autolock& operator = (const autolock&) = delete;
130 
131  ~autolock (void)
132  {
133  if (m_lock_result)
134  m_mutex.unlock ();
135  }
136 
137  bool ok (void) const { return m_lock_result; }
138 
139  operator bool (void) const { return ok (); }
140 
141  private:
142 
144 
146  };
147 
148 
149  class
150  OCTAVE_API
151  thread
152  {
153  public:
154 
155  static void init (void);
156 
157  static bool is_thread (void);
158  };
159 }
160 
161 #endif
autolock(const autolock &)=delete
autolock(const mutex &m, bool block=true)
Definition: oct-mutex.h:113
bool ok(void) const
Definition: oct-mutex.h:137
refcount< octave_idx_type > m_count
Definition: oct-mutex.h:54
virtual ~base_mutex(void)=default
~mutex(void)
Definition: oct-mutex.h:70
base_mutex * m_rep
Definition: oct-mutex.h:106
mutex(const mutex &m)
Definition: oct-mutex.h:64
void unlock(void)
Definition: oct-mutex.h:95
bool try_lock(void)
Definition: oct-mutex.h:100
void lock(void)
Definition: oct-mutex.h:90
static bool is_thread(void)
static void init(void)
T octave_idx_type m
Definition: mx-inlines.cc:773