GNU Octave 7.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-2022 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
33namespace octave
34{
35 class mutex;
36
37 class
39 {
40 public:
41 friend class mutex;
42
43 base_mutex (void) = default;
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
54 class
56 mutex
57 {
58 public:
59 mutex (void);
60
61 mutex (const mutex& m) = default;
62
63 ~mutex (void) = default;
64
65 mutex& operator = (const mutex& m) = default;
66
67 void lock (void)
68 {
69 m_rep->lock ();
70 }
71
72 void unlock (void)
73 {
74 m_rep->unlock ();
75 }
76
77 bool try_lock (void)
78 {
79 return m_rep->try_lock ();
80 }
81
82 protected:
83 std::shared_ptr<base_mutex> m_rep;
84 };
85
86 class
88 {
89 public:
90 autolock (const mutex& m, bool block = true)
91 : m_mutex (m), m_lock_result (false)
92 {
93 if (block)
94 {
95 m_mutex.lock ();
96 m_lock_result = true;
97 }
98 else
99 m_lock_result = m_mutex.try_lock ();
100 }
101
102 // No copying.
103
104 autolock (const autolock&) = delete;
105
106 autolock& operator = (const autolock&) = delete;
107
109 {
110 if (m_lock_result)
111 m_mutex.unlock ();
112 }
113
114 bool ok (void) const { return m_lock_result; }
115
116 operator bool (void) const { return ok (); }
117
118 private:
119
121
123 };
124
125
126 class
128 thread
129 {
130 public:
131
132 static void init (void);
133
134 static bool is_thread (void);
135 };
136}
137
138#endif
autolock(const autolock &)=delete
autolock(const mutex &m, bool block=true)
Definition: oct-mutex.h:90
bool ok(void) const
Definition: oct-mutex.h:114
base_mutex(void)=default
virtual ~base_mutex(void)=default
~mutex(void)=default
mutex(const mutex &m)=default
std::shared_ptr< base_mutex > m_rep
Definition: oct-mutex.h:83
void unlock(void)
Definition: oct-mutex.h:72
bool try_lock(void)
Definition: oct-mutex.h:77
void lock(void)
Definition: oct-mutex.h:67
static bool is_thread(void)
static void init(void)
#define OCTAVE_API
Definition: main.in.cc:55