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.cc
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 (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include "oct-mutex.h"
31#include "lo-error.h"
32
33#if defined (OCTAVE_USE_WINDOWS_API)
34# include <windows.h>
35#elif defined (HAVE_PTHREAD_H)
36# include <pthread.h>
37#endif
38
40
41void
43{
44 (*current_liboctave_error_handler) ("mutex not supported on this platform");
45}
46
47void
49{
50 (*current_liboctave_error_handler) ("mutex not supported on this platform");
51}
52
53bool
55{
56 (*current_liboctave_error_handler) ("mutex not supported on this platform");
57
58 return false;
59}
60
61#if defined (OCTAVE_USE_WINDOWS_API)
62
63class w32_mutex : public base_mutex
64{
65public:
66 w32_mutex ()
67 : base_mutex ()
68 {
69 InitializeCriticalSection (&cs);
70 }
71
72 ~w32_mutex ()
73 {
74 DeleteCriticalSection (&cs);
75 }
76
77 void lock ()
78 {
79 EnterCriticalSection (&cs);
80 }
81
82 void unlock ()
83 {
84 LeaveCriticalSection (&cs);
85 }
86
87 bool try_lock ()
88 {
89 return (TryEnterCriticalSection (&cs) != 0);
90 }
91
92private:
93 CRITICAL_SECTION cs;
94};
95
96static DWORD thread_id = 0;
97
98void
100{
101 thread_id = GetCurrentThreadId ();
102}
103
104bool
106{
107 return (GetCurrentThreadId () == thread_id);
108}
109
110#elif defined (HAVE_PTHREAD_H)
111
112class pthread_mutex : public base_mutex
113{
114public:
115
116 pthread_mutex ()
117 : base_mutex (), m_pm ()
118 {
119 pthread_mutexattr_t attr;
120
121 pthread_mutexattr_init (&attr);
122 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
123 pthread_mutex_init (&m_pm, &attr);
124 pthread_mutexattr_destroy (&attr);
125 }
126
127 OCTAVE_DISABLE_COPY_MOVE (pthread_mutex)
128
129 ~pthread_mutex ()
130 {
131 pthread_mutex_destroy (&m_pm);
132 }
133
134 void lock ()
135 {
136 pthread_mutex_lock (&m_pm);
137 }
138
139 void unlock ()
140 {
141 pthread_mutex_unlock (&m_pm);
142 }
143
144 bool try_lock ()
145 {
146 return (pthread_mutex_trylock (&m_pm) == 0);
147 }
148
149private:
150 pthread_mutex_t m_pm;
151};
152
153static pthread_t thread_id = 0;
154
155void
157{
158 thread_id = pthread_self ();
159}
160
161bool
163{
164 return (pthread_equal (thread_id, pthread_self ()) != 0);
165}
166
167#endif
168
169static base_mutex *
170init_rep ()
171{
172#if defined (OCTAVE_USE_WINDOWS_API)
173 return new w32_mutex ();
174#elif defined (HAVE_PTHREAD_H)
175 return new pthread_mutex ();
176#else
177 return new base_mutex ();
178#endif
179}
180
181mutex::mutex () : m_rep (init_rep ()) { }
182
183OCTAVE_END_NAMESPACE(octave)
virtual void lock()
Definition oct-mutex.cc:42
virtual bool try_lock()
Definition oct-mutex.cc:54
virtual void unlock()
Definition oct-mutex.cc:48
static void init()
static bool is_thread()
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn