annotate liboctave/util/oct-mutex.h @ 29565:7733ca1db419

use shared_ptr to manage memory for mutex object * oct-mutex.h (class base_mutex, class mutex): Use std::shared_ptr instead of custom reference counting.
author John W. Eaton <jwe@octave.org>
date Wed, 28 Apr 2021 08:18:43 -0400
parents 0a5b15007766
children 796f54d4ddbf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
29358
0a5b15007766 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
3 // Copyright (C) 2008-2021 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
7934
5a156ab94dd2 Add octave_mutex class
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25
20791
f7084eae3318 maint: Use Octave coding conventions for #if statements.
Rik <rik@octave.org>
parents: 19697
diff changeset
26 #if ! defined (octave_oct_mutex_h)
17822
ebb3ef964372 maint: Use common #define syntax "octave_filename_h" in h_files.
Rik <rik@octave.org>
parents: 17744
diff changeset
27 #define octave_oct_mutex_h 1
7934
5a156ab94dd2 Add octave_mutex class
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28
21244
1473547f50f5 include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents: 20791
diff changeset
29 #include "octave-config.h"
1473547f50f5 include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents: 20791
diff changeset
30
29565
7733ca1db419 use shared_ptr to manage memory for mutex object
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
31 #include <memory>
13985
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 13281
diff changeset
32
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
33 namespace octave
7952
2c0a0edae596 reorganize octave_mutex class
John W. Eaton <jwe@octave.org>
parents: 7943
diff changeset
34 {
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
35 class mutex;
7952
2c0a0edae596 reorganize octave_mutex class
John W. Eaton <jwe@octave.org>
parents: 7943
diff changeset
36
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
37 class
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
38 base_mutex
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
39 {
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
40 public:
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
41 friend class mutex;
7952
2c0a0edae596 reorganize octave_mutex class
John W. Eaton <jwe@octave.org>
parents: 7943
diff changeset
42
29565
7733ca1db419 use shared_ptr to manage memory for mutex object
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
43 base_mutex (void) = default;
13281
834f904a3dcb Add support for full asynchronous graphics toolkit running in a separate
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11523
diff changeset
44
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
45 virtual ~base_mutex (void) = default;
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
46
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
47 virtual void lock (void);
7952
2c0a0edae596 reorganize octave_mutex class
John W. Eaton <jwe@octave.org>
parents: 7943
diff changeset
48
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
49 virtual void unlock (void);
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
50
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
51 virtual bool try_lock (void);
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
52 };
7934
5a156ab94dd2 Add octave_mutex class
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
54 class
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
55 OCTAVE_API
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
56 mutex
7952
2c0a0edae596 reorganize octave_mutex class
John W. Eaton <jwe@octave.org>
parents: 7943
diff changeset
57 {
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
58 public:
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
59 mutex (void);
7952
2c0a0edae596 reorganize octave_mutex class
John W. Eaton <jwe@octave.org>
parents: 7943
diff changeset
60
29565
7733ca1db419 use shared_ptr to manage memory for mutex object
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
61 mutex (const mutex& m) = default;
7934
5a156ab94dd2 Add octave_mutex class
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62
29565
7733ca1db419 use shared_ptr to manage memory for mutex object
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
63 ~mutex (void) = default;
7934
5a156ab94dd2 Add octave_mutex class
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64
29565
7733ca1db419 use shared_ptr to manage memory for mutex object
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
65 mutex& operator = (const mutex& m) = default;
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
66
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
67 void lock (void)
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
68 {
27379
3db033e86376 use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents: 26377
diff changeset
69 m_rep->lock ();
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
70 }
7934
5a156ab94dd2 Add octave_mutex class
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
72 void unlock (void)
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
73 {
27379
3db033e86376 use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents: 26377
diff changeset
74 m_rep->unlock ();
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
75 }
7934
5a156ab94dd2 Add octave_mutex class
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
77 bool try_lock (void)
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
78 {
27379
3db033e86376 use m_ prefix for data members in most liboctave/util classes
John W. Eaton <jwe@octave.org>
parents: 26377
diff changeset
79 return m_rep->try_lock ();
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
80 }
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
81
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
82 protected:
29565
7733ca1db419 use shared_ptr to manage memory for mutex object
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
83 std::shared_ptr<base_mutex> m_rep;
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
84 };
22869
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22868
diff changeset
85
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
86 class
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
87 autolock
7952
2c0a0edae596 reorganize octave_mutex class
John W. Eaton <jwe@octave.org>
parents: 7943
diff changeset
88 {
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
89 public:
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
90 autolock (const mutex& m, bool block = true)
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
91 : m_mutex (m), m_lock_result (false)
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
92 {
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
93 if (block)
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
94 {
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
95 m_mutex.lock ();
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
96 m_lock_result = true;
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
97 }
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
98 else
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
99 m_lock_result = m_mutex.try_lock ();
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
100 }
7934
5a156ab94dd2 Add octave_mutex class
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
102 // No copying.
13281
834f904a3dcb Add support for full asynchronous graphics toolkit running in a separate
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11523
diff changeset
103
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
104 autolock (const autolock&) = delete;
13281
834f904a3dcb Add support for full asynchronous graphics toolkit running in a separate
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11523
diff changeset
105
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
106 autolock& operator = (const autolock&) = delete;
7936
78400fde223e Support for backend-to-octave event management
John W. Eaton <jwe@octave.org>
parents: 7934
diff changeset
107
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
108 ~autolock (void)
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
109 {
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
110 if (m_lock_result)
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
111 m_mutex.unlock ();
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
112 }
13281
834f904a3dcb Add support for full asynchronous graphics toolkit running in a separate
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11523
diff changeset
113
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
114 bool ok (void) const { return m_lock_result; }
7934
5a156ab94dd2 Add octave_mutex class
John W. Eaton <jwe@octave.org>
parents:
diff changeset
115
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
116 operator bool (void) const { return ok (); }
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
117
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
118 private:
22865
89756f2f085b use c++11 style for deleting default copy constructor and assignment operator
John W. Eaton <jwe@octave.org>
parents: 22755
diff changeset
119
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
120 mutex m_mutex;
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
121
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
122 bool m_lock_result;
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
123 };
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
124
13281
834f904a3dcb Add support for full asynchronous graphics toolkit running in a separate
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11523
diff changeset
125
23057
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
126 class
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
127 OCTAVE_API
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
128 thread
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
129 {
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
130 public:
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
131
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
132 static void init (void);
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
133
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
134 static bool is_thread (void);
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
135 };
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
136 }
bb7513d73673 move mutex classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23012
diff changeset
137
7934
5a156ab94dd2 Add octave_mutex class
John W. Eaton <jwe@octave.org>
parents:
diff changeset
138 #endif