GNU Octave  4.4.1
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-2018 Michael Goffioul
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "oct-mutex.h"
28 #include "lo-error.h"
29 
30 #if defined (OCTAVE_USE_WINDOWS_API)
31 # include <windows.h>
32 #elif defined (HAVE_PTHREAD_H)
33 # include <pthread.h>
34 #endif
35 
36 namespace octave
37 {
38  void
40  {
41  (*current_liboctave_error_handler) ("mutex not supported on this platform");
42  }
43 
44  void
46  {
47  (*current_liboctave_error_handler) ("mutex not supported on this platform");
48  }
49 
50  bool
52  {
53  (*current_liboctave_error_handler) ("mutex not supported on this platform");
54 
55  return false;
56  }
57 
58 #if defined (OCTAVE_USE_WINDOWS_API)
59 
60  class
61  w32_mutex : public base_mutex
62  {
63  public:
64  w32_mutex (void)
65  : base_mutex ()
66  {
67  InitializeCriticalSection (&cs);
68  }
69 
70  ~w32_mutex (void)
71  {
72  DeleteCriticalSection (&cs);
73  }
74 
75  void lock (void)
76  {
77  EnterCriticalSection (&cs);
78  }
79 
80  void unlock (void)
81  {
82  LeaveCriticalSection (&cs);
83  }
84 
85  bool try_lock (void)
86  {
87  return (TryEnterCriticalSection (&cs) != 0);
88  }
89 
90  private:
91  CRITICAL_SECTION cs;
92  };
93 
94  static DWORD thread_id = 0;
95 
96  void
97  thread::init (void)
98  {
99  thread_id = GetCurrentThreadId ();
100  }
101 
102  bool
103  thread::is_thread (void)
104  {
105  return (GetCurrentThreadId () == thread_id);
106  }
107 
108 #elif defined (HAVE_PTHREAD_H)
109 
110  class
111  pthread_mutex : public base_mutex
112  {
113  public:
114  pthread_mutex (void)
115  : base_mutex (), pm ()
116  {
117  pthread_mutexattr_t attr;
118 
119  pthread_mutexattr_init (&attr);
120  pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
121  pthread_mutex_init (&pm, &attr);
122  pthread_mutexattr_destroy (&attr);
123  }
124 
125  ~pthread_mutex (void)
126  {
127  pthread_mutex_destroy (&pm);
128  }
129 
130  void lock (void)
131  {
132  pthread_mutex_lock (&pm);
133  }
134 
135  void unlock (void)
136  {
137  pthread_mutex_unlock (&pm);
138  }
139 
140  bool try_lock (void)
141  {
142  return (pthread_mutex_trylock (&pm) == 0);
143  }
144 
145  private:
146  pthread_mutex_t pm;
147  };
148 
149  static pthread_t thread_id = 0;
150 
151  void
152  thread::init (void)
153  {
154  thread_id = pthread_self ();
155  }
156 
157  bool
158  thread::is_thread (void)
159  {
160  return (pthread_equal (thread_id, pthread_self ()) != 0);
161  }
162 
163 #endif
164 
165  static base_mutex *
166  init_rep (void)
167  {
168 #if defined (OCTAVE_USE_WINDOWS_API)
169  return new w32_mutex ();
170 #elif defined (HAVE_PTHREAD_H)
171  return new pthread_mutex ();
172 #else
173  return new base_mutex ();
174 #endif
175  }
176 
177  mutex::mutex (void) : rep (init_rep ()) { }
178 }
virtual void lock(void)
Definition: oct-mutex.cc:39
virtual void unlock(void)
Definition: oct-mutex.cc:45
virtual bool try_lock(void)
Definition: oct-mutex.cc:51
static void init(void)
octave::call_stack & cs
Definition: ov-class.cc:1752
static base_mutex * init_rep(void)
Definition: oct-mutex.cc:166
static bool is_thread(void)