GNU Octave  9.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-2024 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  OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE (base_mutex)
45 
46  virtual ~base_mutex () = default;
47 
48  virtual void lock ();
49 
50  virtual void unlock ();
51 
52  virtual bool try_lock ();
53 };
54 
55 class
57 mutex
58 {
59 public:
60  mutex ();
61 
62  OCTAVE_DEFAULT_COPY_MOVE_DELETE (mutex)
63 
64  void lock ()
65  {
66  m_rep->lock ();
67  }
68 
69  void unlock ()
70  {
71  m_rep->unlock ();
72  }
73 
74  bool try_lock ()
75  {
76  return m_rep->try_lock ();
77  }
78 
79 protected:
80  std::shared_ptr<base_mutex> m_rep;
81 };
82 
83 class
86 {
87 public:
88  autolock (const mutex& m, bool block = true)
89  : m_mutex (m), m_lock_result (false)
90  {
91  if (block)
92  {
93  m_mutex.lock ();
94  m_lock_result = true;
95  }
96  else
97  m_lock_result = m_mutex.try_lock ();
98  }
99 
100  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (autolock)
101 
103  {
104  if (m_lock_result)
105  m_mutex.unlock ();
106  }
107 
108  bool ok () const { return m_lock_result; }
109 
110  operator bool () const { return ok (); }
111 
112 private:
113 
114  mutex m_mutex;
115 
116  bool m_lock_result;
117 };
118 
119 
120 class
122 thread
123 {
124 public:
125 
126  static void init ();
127 
128  static bool is_thread ();
129 };
130 
131 OCTAVE_END_NAMESPACE(octave)
132 
133 #endif
bool ok() const
Definition: oct-mutex.h:108
~autolock()
Definition: oct-mutex.h:102
autolock(const mutex &m, bool block=true)
Definition: oct-mutex.h:88
void unlock()
Definition: oct-mutex.h:69
bool try_lock()
Definition: oct-mutex.h:74
mutex()
Definition: oct-mutex.cc:183
void lock()
Definition: oct-mutex.h:64
std::shared_ptr< base_mutex > m_rep
Definition: oct-mutex.h:80
static void init()
static bool is_thread()
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition: main.cc:55
T octave_idx_type m
Definition: mx-inlines.cc:781