GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
oct-mutex.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2008-2025 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
35class mutex;
36
38{
39public:
40 friend class mutex;
41
42 OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE (base_mutex)
43
44 virtual ~base_mutex () = default;
45
46 virtual void lock ();
47
48 virtual void unlock ();
49
50 virtual bool try_lock ();
51};
52
54{
55public:
56 mutex ();
57
58 OCTAVE_DEFAULT_COPY_MOVE_DELETE (mutex)
59
60 void lock ()
61 {
62 m_rep->lock ();
63 }
64
65 void unlock ()
66 {
67 m_rep->unlock ();
68 }
69
70 bool try_lock ()
71 {
72 return m_rep->try_lock ();
73 }
74
75protected:
76 std::shared_ptr<base_mutex> m_rep;
77};
78
80{
81public:
82 autolock (const mutex& m, bool block = true)
83 : m_mutex (m), m_lock_result (false)
84 {
85 if (block)
86 {
87 m_mutex.lock ();
88 m_lock_result = true;
89 }
90 else
91 m_lock_result = m_mutex.try_lock ();
92 }
93
94 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (autolock)
95
97 {
98 if (m_lock_result)
99 m_mutex.unlock ();
100 }
101
102 bool ok () const { return m_lock_result; }
103
104 operator bool () const { return ok (); }
105
106private:
107
108 mutex m_mutex;
109
110 bool m_lock_result;
111};
112
113
115{
116public:
117
118 static void init ();
119
120 static bool is_thread ();
121};
122
123OCTAVE_END_NAMESPACE(octave)
124
125#endif
bool ok() const
Definition oct-mutex.h:102
~autolock()
Definition oct-mutex.h:96
autolock(const mutex &m, bool block=true)
Definition oct-mutex.h:82
void unlock()
Definition oct-mutex.h:65
bool try_lock()
Definition oct-mutex.h:70
std::shared_ptr< base_mutex > m_rep
Definition oct-mutex.h:76
static void init()
static bool is_thread()
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition main.in.cc:55