GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-mutex.cc
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 (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 
41 void
43 {
44  (*current_liboctave_error_handler) ("mutex not supported on this platform");
45 }
46 
47 void
49 {
50  (*current_liboctave_error_handler) ("mutex not supported on this platform");
51 }
52 
53 bool
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 
63 class
64 w32_mutex : public base_mutex
65 {
66 public:
67  w32_mutex ()
68  : base_mutex ()
69  {
70  InitializeCriticalSection (&cs);
71  }
72 
73  ~w32_mutex ()
74  {
75  DeleteCriticalSection (&cs);
76  }
77 
78  void lock ()
79  {
80  EnterCriticalSection (&cs);
81  }
82 
83  void unlock ()
84  {
85  LeaveCriticalSection (&cs);
86  }
87 
88  bool try_lock ()
89  {
90  return (TryEnterCriticalSection (&cs) != 0);
91  }
92 
93 private:
94  CRITICAL_SECTION cs;
95 };
96 
97 static DWORD thread_id = 0;
98 
99 void
100 thread::init ()
101 {
102  thread_id = GetCurrentThreadId ();
103 }
104 
105 bool
107 {
108  return (GetCurrentThreadId () == thread_id);
109 }
110 
111 #elif defined (HAVE_PTHREAD_H)
112 
113 class
114 pthread_mutex : public base_mutex
115 {
116 public:
117 
118  pthread_mutex ()
119  : base_mutex (), m_pm ()
120  {
121  pthread_mutexattr_t attr;
122 
123  pthread_mutexattr_init (&attr);
124  pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
125  pthread_mutex_init (&m_pm, &attr);
126  pthread_mutexattr_destroy (&attr);
127  }
128 
129  OCTAVE_DISABLE_COPY_MOVE (pthread_mutex)
130 
131  ~pthread_mutex ()
132  {
133  pthread_mutex_destroy (&m_pm);
134  }
135 
136  void lock ()
137  {
138  pthread_mutex_lock (&m_pm);
139  }
140 
141  void unlock ()
142  {
143  pthread_mutex_unlock (&m_pm);
144  }
145 
146  bool try_lock ()
147  {
148  return (pthread_mutex_trylock (&m_pm) == 0);
149  }
150 
151 private:
152  pthread_mutex_t m_pm;
153 };
154 
155 static pthread_t thread_id = 0;
156 
157 void
158 thread::init ()
159 {
160  thread_id = pthread_self ();
161 }
162 
163 bool
165 {
166  return (pthread_equal (thread_id, pthread_self ()) != 0);
167 }
168 
169 #endif
170 
171 static base_mutex *
172 init_rep ()
173 {
174 #if defined (OCTAVE_USE_WINDOWS_API)
175  return new w32_mutex ();
176 #elif defined (HAVE_PTHREAD_H)
177  return new pthread_mutex ();
178 #else
179  return new base_mutex ();
180 #endif
181 }
182 
183 mutex::mutex () : m_rep (init_rep ()) { }
184 
185 OCTAVE_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
mutex()
Definition: oct-mutex.cc:183
static void init()
static bool is_thread()
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn