• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List

mutex.h

00001 /***************************************************************************
00002  *  include/stxxl/bits/common/mutex.h
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002 Roman Dementiev <[email protected]>
00007  *  Copyright (C) 2008 Andreas Beckmann <[email protected]>
00008  *
00009  *  Distributed under the Boost Software License, Version 1.0.
00010  *  (See accompanying file LICENSE_1_0.txt or copy at
00011  *  http://www.boost.org/LICENSE_1_0.txt)
00012  **************************************************************************/
00013 
00014 #ifndef STXXL_MUTEX_HEADER
00015 #define STXXL_MUTEX_HEADER
00016 
00017 #include <stxxl/bits/namespace.h>
00018 
00019 #ifdef STXXL_BOOST_THREADS
00020 
00021  #include <boost/thread/mutex.hpp>
00022 
00023 #else
00024 
00025  #include <pthread.h>
00026 
00027  #include <stxxl/bits/noncopyable.h>
00028  #include <stxxl/bits/common/utils.h>
00029 
00030 #endif
00031 
00032 
00033 __STXXL_BEGIN_NAMESPACE
00034 
00035 #ifdef STXXL_BOOST_THREADS
00036 
00037 typedef boost::mutex mutex;
00038 
00039 #else
00040 
00041 class mutex : private noncopyable
00042 {
00043     pthread_mutex_t _mutex;
00044 
00045 public:
00046     mutex()
00047     {
00048         check_pthread_call(pthread_mutex_init(&_mutex, NULL));
00049     }
00050 
00051     ~mutex()
00052     {
00053         int res = pthread_mutex_trylock(&_mutex);
00054 
00055         if (res == 0 || res == EBUSY) {
00056             check_pthread_call(pthread_mutex_unlock(&_mutex));
00057         } else
00058             stxxl_function_error(resource_error);
00059 
00060         check_pthread_call(pthread_mutex_destroy(&_mutex));
00061     }
00062     void lock()
00063     {
00064         check_pthread_call(pthread_mutex_lock(&_mutex));
00065     }
00066     void unlock()
00067     {
00068         check_pthread_call(pthread_mutex_unlock(&_mutex));
00069     }
00070 };
00071 
00072 #endif
00073 
00074 #ifdef STXXL_BOOST_THREADS
00075 
00076 typedef boost::mutex::scoped_lock scoped_mutex_lock;
00077 
00078 #else
00079 
00081 class scoped_mutex_lock
00082 {
00083     mutex & mtx;
00084     bool is_locked;
00085 
00086 public:
00087     scoped_mutex_lock(mutex & mtx_) : mtx(mtx_), is_locked(false)
00088     {
00089         lock();
00090     }
00091 
00092     ~scoped_mutex_lock()
00093     {
00094         unlock();
00095     }
00096 
00097     void lock()
00098     {
00099         if (!is_locked) {
00100             mtx.lock();
00101             is_locked = true;
00102         }
00103     }
00104 
00105     void unlock()
00106     {
00107         if (is_locked) {
00108             mtx.unlock();
00109             is_locked = false;
00110         }
00111     }
00112 };
00113 
00114 #endif
00115 
00116 __STXXL_END_NAMESPACE
00117 
00118 #endif // !STXXL_MUTEX_HEADER

Generated by  doxygen 1.7.1