GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-mutex.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2008-2013 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "oct-mutex.h"
28 #include "lo-error.h"
29 
30 #if defined (__WIN32__) && ! defined (__CYGWIN__)
31 #include <windows.h>
32 #elif defined (HAVE_PTHREAD_H)
33 #include <pthread.h>
34 #endif
35 
36 void
38 {
39  (*current_liboctave_error_handler) ("mutex not supported on this platform");
40 }
41 
42 void
44 {
45  (*current_liboctave_error_handler) ("mutex not supported on this platform");
46 }
47 
48 bool
50 {
51  (*current_liboctave_error_handler) ("mutex not supported on this platform");
52 
53  return false;
54 }
55 
56 #if defined (__WIN32__) && ! defined (__CYGWIN__)
57 
58 class
59 octave_w32_mutex : public octave_base_mutex
60 {
61 public:
62  octave_w32_mutex (void)
64  {
65  InitializeCriticalSection (&cs);
66  }
67 
68  ~octave_w32_mutex (void)
69  {
70  DeleteCriticalSection (&cs);
71  }
72 
73  void lock (void)
74  {
75  EnterCriticalSection (&cs);
76  }
77 
78  void unlock (void)
79  {
80  LeaveCriticalSection (&cs);
81  }
82 
83  bool try_lock (void)
84  {
85  return (TryEnterCriticalSection (&cs) != 0);
86  }
87 
88 private:
89  CRITICAL_SECTION cs;
90 };
91 
92 static DWORD octave_thread_id = 0;
93 
94 void
96 {
97  octave_thread_id = GetCurrentThreadId ();
98 }
99 
100 bool
102 {
103  return (GetCurrentThreadId () == octave_thread_id);
104 }
105 
106 #elif defined (HAVE_PTHREAD_H)
107 
108 class
109 octave_pthread_mutex : public octave_base_mutex
110 {
111 public:
112  octave_pthread_mutex (void)
113  : octave_base_mutex (), pm ()
114  {
115  pthread_mutexattr_t attr;
116 
117  pthread_mutexattr_init (&attr);
118  pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
119  pthread_mutex_init (&pm, &attr);
120  pthread_mutexattr_destroy (&attr);
121  }
122 
123  ~octave_pthread_mutex (void)
124  {
125  pthread_mutex_destroy (&pm);
126  }
127 
128  void lock (void)
129  {
130  pthread_mutex_lock (&pm);
131  }
132 
133  void unlock (void)
134  {
135  pthread_mutex_unlock (&pm);
136  }
137 
138  bool try_lock (void)
139  {
140  return (pthread_mutex_trylock (&pm) == 0);
141  }
142 
143 private:
144  pthread_mutex_t pm;
145 };
146 
147 static pthread_t octave_thread_id = 0;
148 
149 void
150 octave_thread::init (void)
151 {
152  octave_thread_id = pthread_self ();
153 }
154 
155 bool
157 {
158  return (pthread_equal (octave_thread_id, pthread_self ()) != 0);
159 }
160 
161 #endif
162 
163 static octave_base_mutex *
164 init_rep (void)
165 {
166 #if defined (__WIN32__) && ! defined (__CYGWIN__)
167  return new octave_w32_mutex ();
168 #elif defined (HAVE_PTHREAD_H)
169  return new octave_pthread_mutex ();
170 #else
171  return new octave_base_mutex ();
172 #endif
173 }
174