GNU Octave  8.1.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-2023 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 <memory>
32 
34 
35 class mutex;
36 
37 class
40 {
41 public:
42  friend class mutex;
43 
44  base_mutex (void) = default;
45 
46  virtual ~base_mutex (void) = default;
47 
48  virtual void lock (void);
49 
50  virtual void unlock (void);
51 
52  virtual bool try_lock (void);
53 };
54 
55 class
57 mutex
58 {
59 public:
60  mutex (void);
61 
62  mutex (const mutex& m) = default;
63 
64  ~mutex (void) = default;
65 
66  mutex& operator = (const mutex& m) = default;
67 
68  void lock (void)
69  {
70  m_rep->lock ();
71  }
72 
73  void unlock (void)
74  {
75  m_rep->unlock ();
76  }
77 
78  bool try_lock (void)
79  {
80  return m_rep->try_lock ();
81  }
82 
83 protected:
84  std::shared_ptr<base_mutex> m_rep;
85 };
86 
87 class
90 {
91 public:
92  autolock (const mutex& m, bool block = true)
93  : m_mutex (m), m_lock_result (false)
94  {
95  if (block)
96  {
97  m_mutex.lock ();
98  m_lock_result = true;
99  }
100  else
101  m_lock_result = m_mutex.try_lock ();
102  }
103 
104  // No copying.
105 
106  autolock (const autolock&) = delete;
107 
108  autolock& operator = (const autolock&) = delete;
109 
110  ~autolock (void)
111  {
112  if (m_lock_result)
113  m_mutex.unlock ();
114  }
115 
116  bool ok (void) const { return m_lock_result; }
117 
118  operator bool (void) const { return ok (); }
119 
120 private:
121 
123 
125 };
126 
127 
128 class
130 thread
131 {
132 public:
133 
134  static void init (void);
135 
136  static bool is_thread (void);
137 };
138 
140 
141 #endif
OCTAVE_END_NAMESPACE(octave)
mutex m_mutex
Definition: oct-mutex.h:122
autolock(const autolock &)=delete
~autolock(void)
Definition: oct-mutex.h:110
bool ok(void) const
Definition: oct-mutex.h:116
bool m_lock_result
Definition: oct-mutex.h:124
autolock(const mutex &m, bool block=true)
Definition: oct-mutex.h:92
base_mutex(void)=default
virtual ~base_mutex(void)=default
void unlock(void)
Definition: oct-mutex.h:73
mutex & operator=(const mutex &m)=default
void lock(void)
Definition: oct-mutex.h:68
~mutex(void)=default
mutex(const mutex &m)=default
std::shared_ptr< base_mutex > m_rep
Definition: oct-mutex.h:84
bool try_lock(void)
Definition: oct-mutex.h:78
mutex(void)
Definition: oct-mutex.cc:180
static void init(void)
static bool is_thread(void)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition: main.in.cc:55
T octave_idx_type m
Definition: mx-inlines.cc:773