Mercurial > hg > octave-lyh
changeset 7934:5a156ab94dd2
Add octave_mutex class
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 16 Jul 2008 14:26:18 -0400 |
parents | 54e414cc106b |
children | 85d6296d51e1 |
files | ChangeLog configure.in liboctave/ChangeLog liboctave/Makefile.in liboctave/oct-mutex.cc liboctave/oct-mutex.h |
diffstat | 6 files changed, 247 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2008-07-16 Michael Goffioul <michael.goffioul@gmail.com> + + * configure.in: Add check for pthread.h. + 2008-07-14 John W. Eaton <jwe@octave.org> * Makeconf.in (MAGICK_CONFIG): Substitute here.
--- a/configure.in +++ b/configure.in @@ -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)
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,8 @@ +2008-07-15 Michael Goffioul <michael.goffioul@gmail.com> + + * oct-mutex.h, oct-mutex.cc: New files. + * Makefile.in: Add them to appropriate lists. + 2008-07-15 John W. Eaton <jwe@octave.org> * oct-sort.cc, oct-sort.h (octave_sort<T>::count_run): Declare
--- a/liboctave/Makefile.in +++ b/liboctave/Makefile.in @@ -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 \
new file mode 100644 --- /dev/null +++ b/liboctave/oct-mutex.cc @@ -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 +<http://www.gnu.org/licenses/>. + +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "oct-mutex.h" +#include "error.h" + +#if defined (HAVE_PTHREAD_H) +#include <pthread.h> +#endif + +#if defined (__WIN32__) && ! defined (__CYGWIN__) +#include <windows.h> +#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++; +}
new file mode 100644 --- /dev/null +++ b/liboctave/oct-mutex.h @@ -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 +<http://www.gnu.org/licenses/>. + +*/ + +#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