mutex.hh

00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2007, 2008, 2009 Ciaran McCreesh
00005  *
00006  * This file is part of the Paludis package manager. Paludis is free software;
00007  * you can redistribute it and/or modify it under the terms of the GNU General
00008  * Public License version 2, as published by the Free Software Foundation.
00009  *
00010  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_MUTEX_HH
00021 #define PALUDIS_GUARD_PALUDIS_UTIL_MUTEX_HH 1
00022 
00023 #include <paludis/util/mutex-fwd.hh>
00024 #include <paludis/util/attributes.hh>
00025 #include <pthread.h>
00026 
00027 /**
00028  * Declarations for Mutex, Lock and TryLock.
00029  *
00030  * \ingroup g_threads
00031  *
00032  * \section Examples
00033  *
00034  * - None at this time.
00035  */
00036 
00037 namespace paludis
00038 {
00039     /**
00040      * A simple mutex class, which can be locked using Lock and TryLock.
00041      *
00042      * \ingroup g_threads
00043      * \since 0.26
00044      */
00045     class PALUDIS_VISIBLE Mutex
00046     {
00047         private:
00048             Mutex(const Mutex &);
00049             Mutex & operator= (const Mutex &);
00050 
00051             pthread_mutex_t _mutex;
00052 
00053         public:
00054             ///\name Basic operations
00055             ///\{
00056 
00057             explicit Mutex();
00058             ~Mutex();
00059 
00060             ///\}
00061 
00062             pthread_mutex_t * posix_mutex() PALUDIS_ATTRIBUTE((warn_unused_result));
00063     };
00064 
00065     /**
00066      * A RAII lock for a Mutex.
00067      *
00068      * If threading is disabled, locking is a no-op.
00069      *
00070      * \ingroup g_threads
00071      * \nosubgrouping
00072      * \since 0.26
00073      */
00074     class PALUDIS_VISIBLE Lock
00075     {
00076         private:
00077             Lock(const Lock &);
00078             Lock & operator= (const Lock &);
00079 
00080             Mutex * _mutex;
00081 
00082         public:
00083             ///\name Basic operations
00084             ///\{
00085 
00086             explicit Lock(Mutex &);
00087             ~Lock();
00088 
00089             ///\}
00090 
00091             /**
00092              * Acquire a lock on the provided Mutex, and then release our
00093              * previously owned lock.
00094              *
00095              * Use with caution -- this is a good way of creating deadlocks.
00096              */
00097             void acquire_then_release_old(Mutex &);
00098     };
00099 
00100     /**
00101      * A RAII trylock for a Mutex.
00102      *
00103      * If threading is disabled, locking is a no-op and the try always succeeds.
00104      *
00105      * \ingroup g_threads
00106      * \since 0.26
00107      * \nosubgrouping
00108      */
00109     class PALUDIS_VISIBLE TryLock
00110     {
00111         private:
00112             TryLock(const TryLock &);
00113             TryLock & operator= (const TryLock &);
00114 
00115             Mutex * _mutex;
00116 
00117         public:
00118             ///\name Basic operations
00119             ///\{
00120 
00121             explicit TryLock(Mutex &);
00122             ~TryLock();
00123 
00124             ///\}
00125 
00126             /**
00127              * Did the lock succeed?
00128              */
00129             bool operator() () const PALUDIS_ATTRIBUTE((warn_unused_result));
00130     };
00131 }
00132 
00133 #endif

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4