# HG changeset patch # User John W. Eaton # Date 1216232778 14400 # Node ID 5a156ab94dd26489cb09aa0476f0b2c8ffa52893 # Parent 54e414cc106b044cd3b223fd4ff2f5b0c9bb8381 Add octave_mutex class diff -r 54e414cc106b -r 5a156ab94dd2 ChangeLog --- a/ChangeLog Wed Jul 16 14:21:24 2008 -0400 +++ b/ChangeLog Wed Jul 16 14:26:18 2008 -0400 @@ -1,3 +1,7 @@ +2008-07-16 Michael Goffioul + + * configure.in: Add check for pthread.h. + 2008-07-14 John W. Eaton * Makeconf.in (MAGICK_CONFIG): Substitute here. diff -r 54e414cc106b -r 5a156ab94dd2 configure.in --- a/configure.in Wed Jul 16 14:21:24 2008 -0400 +++ b/configure.in Wed Jul 16 14:26:18 2008 -0400 @@ -1379,7 +1379,7 @@ AC_CHECK_HEADERS(assert.h curses.h direct.h dlfcn.h fcntl.h float.h \ floatingpoint.h grp.h ieeefp.h inttypes.h limits.h locale.h memory.h nan.h \ - ncurses.h poll.h pwd.h stdint.h stdlib.h string.h sunmath.h sys/ioctl.h \ + ncurses.h poll.h pthread.h pwd.h stdint.h stdlib.h string.h sunmath.h sys/ioctl.h \ sys/param.h sys/poll.h sys/resource.h sys/select.h sys/stat.h \ sys/time.h sys/times.h sys/types.h sys/utsname.h sys/utime.h termcap.h \ unistd.h utime.h varargs.h) diff -r 54e414cc106b -r 5a156ab94dd2 liboctave/ChangeLog --- a/liboctave/ChangeLog Wed Jul 16 14:21:24 2008 -0400 +++ b/liboctave/ChangeLog Wed Jul 16 14:26:18 2008 -0400 @@ -1,3 +1,8 @@ +2008-07-15 Michael Goffioul + + * oct-mutex.h, oct-mutex.cc: New files. + * Makefile.in: Add them to appropriate lists. + 2008-07-15 John W. Eaton * oct-sort.cc, oct-sort.h (octave_sort::count_run): Declare diff -r 54e414cc106b -r 5a156ab94dd2 liboctave/Makefile.in --- a/liboctave/Makefile.in Wed Jul 16 14:21:24 2008 -0400 +++ b/liboctave/Makefile.in Wed Jul 16 14:26:18 2008 -0400 @@ -88,7 +88,7 @@ lo-ieee.h lo-mappers.h lo-math.h lo-specfun.h \ lo-sysdep.h lo-utils.h mach-info.h md5.h oct-alloc.h oct-cmplx.h \ oct-env.h oct-fftw.h oct-getopt.h oct-group.h oct-inttypes.h \ - oct-lookup.h oct-md5.h oct-passwd.h oct-rand.h oct-rl-edit.h \ + oct-lookup.h oct-md5.h oct-mutex.h oct-passwd.h oct-rand.h oct-rl-edit.h \ oct-rl-hist.h oct-shlib.h oct-sort.h oct-spparms.h oct-syscalls.h \ oct-sparse.h oct-time.h oct-uname.h \ pathlen.h pathsearch.h prog-args.h \ @@ -148,7 +148,7 @@ file-ops.cc file-stat.cc glob-match.cc idx-vector.cc \ lo-ieee.cc lo-mappers.cc lo-specfun.cc lo-sysdep.cc \ lo-utils.cc mach-info.cc oct-alloc.cc oct-env.cc \ - oct-fftw.cc oct-group.cc oct-md5.cc oct-passwd.cc oct-rand.cc \ + oct-fftw.cc oct-group.cc oct-mutex.cc oct-md5.cc oct-passwd.cc oct-rand.cc \ oct-shlib.cc oct-spparms.cc oct-syscalls.cc oct-time.cc oct-uname.cc \ prog-args.cc regex-match.cc \ sparse-sort.cc sparse-util.cc str-vec.cc \ diff -r 54e414cc106b -r 5a156ab94dd2 liboctave/oct-mutex.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/liboctave/oct-mutex.cc Wed Jul 16 14:26:18 2008 -0400 @@ -0,0 +1,131 @@ +/* + +Copyright (C) 2008 Michael Goffioul + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, see +. + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "oct-mutex.h" +#include "error.h" + +#if defined (HAVE_PTHREAD_H) +#include +#endif + +#if defined (__WIN32__) && ! defined (__CYGWIN__) +#include +#endif + +class +octave_default_mutex : public octave_mutex +{ +public: + octave_default_mutex (void) + : octave_mutex (-1) { } + + void lock (void) + { error ("mutex not supported on this platform"); } + + void unlock(void) + { error ("mutex not supported on this platform"); } +}; + +#if defined (__WIN32__) && ! defined (__CYGWIN__) + +class +octave_w32_mutex : public octave_mutex +{ +public: + octave_w32_mutex (void) + : octave_mutex (-1) + { + InitializeCriticalSection (&cs); + } + + ~octave_w32_mutex (void) + { + DeleteCriticalSection (&cs); + } + + void lock (void) + { + EnterCriticalSection (&cs); + } + + void unlock (void) + { + LeaveCriticalSection (&cs); + } + +private: + CRITICAL_SECTION cs; +}; + +#elif defined (HAVE_PTHREAD_H) + +class +octave_pthread_mutex : public octave_mutex +{ +public: + octave_pthread_mutex (void) + : octave_mutex (-1) + { + pthread_mutexattr_t 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); + } + + void lock (void) + { + pthread_mutex_lock (&pm); + } + + void unlock (void) + { + pthread_mutex_unlock (&pm); + } + +private: + pthread_mutex_t pm; +}; + +#endif + +octave_mutex::octave_mutex (void) +{ +#if defined (__WIN32__) && ! defined (__CYGWIN__) + rep = new octave_w32_mutex (); +#elif defined (HAVE_PTHREAD_H) + rep = new octave_pthread_mutex (); +#else + rep = new octave_default_mutex (); +#endif + rep->count++; +} diff -r 54e414cc106b -r 5a156ab94dd2 liboctave/oct-mutex.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/liboctave/oct-mutex.h Wed Jul 16 14:26:18 2008 -0400 @@ -0,0 +1,104 @@ +/* + +Copyright (C) 2008 Michael Goffioul + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, see +. + +*/ + +#if !defined (octave_octave_mutex_h) +#define octave_octave_mutex_h 1 + +class +OCTAVE_API +octave_mutex +{ +public: + octave_mutex (void); + + octave_mutex (const octave_mutex& m) + { + rep = m.rep; + rep->count++; + } + + virtual ~octave_mutex (void) + { + if (rep && --rep->count == 0) + { + delete rep; + rep = 0; + } + } + + octave_mutex& operator = (const octave_mutex& m) + { + if (rep != m.rep) + { + if (rep && --rep->count == 0) + delete rep; + + rep = m.rep; + rep->count++; + } + + return *this; + } + + virtual void lock (void) + { rep->lock (); } + + virtual void unlock (void) + { rep->unlock (); } + +protected: + explicit octave_mutex (int /* dummy */) + : count (0) { } + +protected: + union + { + octave_mutex *rep; + int count; + }; +}; + +class +octave_autolock +{ +public: + octave_autolock (const octave_mutex& m) + : mutex (m) + { + mutex.lock (); + } + + ~octave_autolock (void) + { + mutex.unlock (); + } + +private: + octave_autolock (void) { } + octave_autolock (const octave_autolock&) { } + octave_autolock& operator = (const octave_autolock&) { } + +private: + octave_mutex mutex; +}; + +#endif