Mercurial > hg > octave-avbm
changeset 7952:2c0a0edae596
reorganize octave_mutex class
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 19 Jul 2008 22:59:14 -0400 |
parents | 9d102940bdc7 |
children | efabe34cce70 |
files | liboctave/ChangeLog liboctave/oct-mutex.cc liboctave/oct-mutex.h |
diffstat | 3 files changed, 122 insertions(+), 99 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,16 @@ +2008-07-19 John W. Eaton <jwe@octave.org> + + * oct-mutex.h (class octave_base_mutex): New class. + (class octave_mutex): Don't use union for rep and count. + (octave_mutex::rep): Declare as octave_base_mutex. + (octave_mutex::count): Delete. + (octave_mutex::lock, octave_mutex::unlock): No longer virtual. + (octave_mutex::~octave_mutex): No need to check that rep is + valid or set rep to zero after deleting. + (octave_mutex::operator =): No need to check that rep is valid. + * oct-mutex.cc (octave_w32_mutex, octave_pthread_mutex): Derive + from octave_base_mutex, not octave_mutex. + 2008-07-18 John W. Eaton <jwe@octave.org> * oct-mutex.h (octave_mutex::octave_mutex (int)): Initialize rep
--- a/liboctave/oct-mutex.cc +++ b/liboctave/oct-mutex.cc @@ -35,85 +35,83 @@ #include <windows.h> #endif -class -octave_default_mutex : public octave_mutex +void +octave_base_mutex::lock (void) { -public: - octave_default_mutex (void) - : octave_mutex (-1) { } + error ("mutex not supported on this platform"); +} - void lock (void) - { error ("mutex not supported on this platform"); } - - void unlock(void) - { error ("mutex not supported on this platform"); } -}; +void +octave_base_mutex::unlock (void) +{ + error ("mutex not supported on this platform"); +} #if defined (__WIN32__) && ! defined (__CYGWIN__) class -octave_w32_mutex : public octave_mutex +octave_w32_mutex : public octave_base_mutex { public: - octave_w32_mutex (void) - : octave_mutex (-1) - { - InitializeCriticalSection (&cs); - } + octave_w32_mutex (void) + : octave_base_mutex () + { + InitializeCriticalSection (&cs); + } - ~octave_w32_mutex (void) - { - DeleteCriticalSection (&cs); - } + ~octave_w32_mutex (void) + { + DeleteCriticalSection (&cs); + } - void lock (void) - { - EnterCriticalSection (&cs); - } + void lock (void) + { + EnterCriticalSection (&cs); + } - void unlock (void) - { - LeaveCriticalSection (&cs); - } + void unlock (void) + { + LeaveCriticalSection (&cs); + } private: - CRITICAL_SECTION cs; + CRITICAL_SECTION cs; }; #elif defined (HAVE_PTHREAD_H) class -octave_pthread_mutex : public octave_mutex +octave_pthread_mutex : public octave_base_mutex { public: - octave_pthread_mutex (void) - : octave_mutex (-1) - { - pthread_mutexattr_t attr; + octave_pthread_mutex (void) + : octave_base_mutex () + { + pthread_mutexattr_t attr; - pthread_mutexattr_init (&attr); - pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init (&pm, &attr); - pthread_mutexattr_destroy (&attr); - } + pthread_mutexattr_init (&attr); + pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init (&pm, &attr); + pthread_mutexattr_destroy (&attr); + } - ~octave_pthread_mutex (void) - { - pthread_mutex_destroy (&pm); - } + ~octave_pthread_mutex (void) + { + pthread_mutex_destroy (&pm); + } - void lock (void) - { - pthread_mutex_lock (&pm); - } + void lock (void) + { + pthread_mutex_lock (&pm); + } - void unlock (void) - { - pthread_mutex_unlock (&pm); - } + void unlock (void) + { + pthread_mutex_unlock (&pm); + } private: - pthread_mutex_t pm; + pthread_mutex_t pm; }; #endif @@ -125,7 +123,6 @@ #elif defined (HAVE_PTHREAD_H) rep = new octave_pthread_mutex (); #else - rep = new octave_default_mutex (); + rep = new octave_base_mutex (); #endif - rep->count = 1; }
--- a/liboctave/oct-mutex.h +++ b/liboctave/oct-mutex.h @@ -23,58 +23,71 @@ #if !defined (octave_octave_mutex_h) #define octave_octave_mutex_h 1 +class octave_mutex; + +class +octave_base_mutex +{ +public: + friend class octave_mutex; + + octave_base_mutex (void) : count (-1) { } + + virtual ~octave_base_mutex (void) { } + + virtual void lock (void); + + virtual void unlock (void); + +private: + int count; +}; + class OCTAVE_API octave_mutex { public: - octave_mutex (void); + octave_mutex (void); + + octave_mutex (const octave_mutex& m) + { + rep = m.rep; + rep->count++; + } - octave_mutex (const octave_mutex& m) + ~octave_mutex (void) + { + if (--rep->count == 0) + delete rep; + } + + octave_mutex& operator = (const octave_mutex& m) + { + if (rep != m.rep) { + if (--rep->count == 0) + delete rep; + rep = m.rep; rep->count++; } - virtual ~octave_mutex (void) - { - if (rep && --rep->count == 0) - { - delete rep; - rep = 0; - } - } + return *this; + } - octave_mutex& operator = (const octave_mutex& m) - { - if (rep != m.rep) - { - if (rep && --rep->count == 0) - delete rep; + void lock (void) + { + rep->lock (); + } - rep = m.rep; - rep->count++; - } - - return *this; - } - - virtual void lock (void) - { rep->lock (); } - - virtual void unlock (void) - { rep->unlock (); } + void unlock (void) + { + rep->unlock (); + } protected: - explicit octave_mutex (int /* dummy */) - : rep (0) { } - -protected: - union - { - octave_mutex *rep; - int count; - }; + octave_base_mutex *rep; }; class @@ -82,15 +95,15 @@ { public: octave_autolock (const octave_mutex& m) - : mutex (m) - { - mutex.lock (); - } + : mutex (m) + { + mutex.lock (); + } ~octave_autolock (void) - { - mutex.unlock (); - } + { + mutex.unlock (); + } private: