oct-mutex.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2008-2012 Michael Goffioul
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include "oct-mutex.h"
00028 #include "lo-error.h"
00029 
00030 #if defined (__WIN32__) && ! defined (__CYGWIN__)
00031 #include <windows.h>
00032 #elif defined (HAVE_PTHREAD_H)
00033 #include <pthread.h>
00034 #endif
00035 
00036 void
00037 octave_base_mutex::lock (void)
00038 {
00039   (*current_liboctave_error_handler) ("mutex not supported on this platform");
00040 }
00041 
00042 void
00043 octave_base_mutex::unlock (void)
00044 {
00045   (*current_liboctave_error_handler) ("mutex not supported on this platform");
00046 }
00047 
00048 bool
00049 octave_base_mutex::try_lock (void)
00050 {
00051   (*current_liboctave_error_handler) ("mutex not supported on this platform");
00052 
00053   return false;
00054 }
00055 
00056 #if defined (__WIN32__) && ! defined (__CYGWIN__)
00057 
00058 class
00059 octave_w32_mutex : public octave_base_mutex
00060 {
00061 public:
00062   octave_w32_mutex (void)
00063     : octave_base_mutex ()
00064   {
00065     InitializeCriticalSection (&cs);
00066   }
00067 
00068   ~octave_w32_mutex (void)
00069   {
00070     DeleteCriticalSection (&cs);
00071   }
00072 
00073   void lock (void)
00074   {
00075     EnterCriticalSection (&cs);
00076   }
00077 
00078   void unlock (void)
00079   {
00080     LeaveCriticalSection (&cs);
00081   }
00082 
00083   bool try_lock (void)
00084   {
00085     return (TryEnterCriticalSection (&cs) != 0);
00086   }
00087 
00088 private:
00089   CRITICAL_SECTION cs;
00090 };
00091 
00092 static DWORD octave_thread_id = 0; 
00093 
00094 void
00095 octave_thread::init (void)
00096 {
00097   octave_thread_id = GetCurrentThreadId ();
00098 }
00099 
00100 bool
00101 octave_thread::is_octave_thread (void)
00102 {
00103   return (GetCurrentThreadId () == octave_thread_id);
00104 }
00105 
00106 #elif defined (HAVE_PTHREAD_H)
00107 
00108 class
00109 octave_pthread_mutex : public octave_base_mutex
00110 {
00111 public:
00112   octave_pthread_mutex (void)
00113     : octave_base_mutex (), pm ()
00114   {
00115     pthread_mutexattr_t attr;
00116 
00117     pthread_mutexattr_init (&attr);
00118     pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
00119     pthread_mutex_init (&pm, &attr);
00120     pthread_mutexattr_destroy (&attr);
00121   }
00122 
00123   ~octave_pthread_mutex (void)
00124   {
00125     pthread_mutex_destroy (&pm);
00126   }
00127 
00128   void lock (void)
00129   {
00130     pthread_mutex_lock (&pm);
00131   }
00132 
00133   void unlock (void)
00134   {
00135     pthread_mutex_unlock (&pm);
00136   }
00137 
00138   bool try_lock (void)
00139   {
00140     return (pthread_mutex_trylock (&pm) == 0);
00141   }
00142 
00143 private:
00144   pthread_mutex_t pm;
00145 };
00146 
00147 static pthread_t octave_thread_id = 0;
00148 
00149 void
00150 octave_thread::init (void)
00151 {
00152   octave_thread_id = pthread_self ();
00153 }
00154 
00155 bool
00156 octave_thread::is_octave_thread (void)
00157 {
00158   return (pthread_equal (octave_thread_id, pthread_self ()) != 0);
00159 }
00160 
00161 #endif
00162 
00163 static octave_base_mutex *
00164 init_rep (void)
00165 {
00166 #if defined (__WIN32__) && ! defined (__CYGWIN__)
00167   return new octave_w32_mutex ();
00168 #elif defined (HAVE_PTHREAD_H)
00169   return new octave_pthread_mutex ();
00170 #else
00171   return new octave_base_mutex ();
00172 #endif
00173 }
00174 
00175 octave_mutex::octave_mutex (void) : rep (init_rep ()) { }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines