285f83a4b866656235cff950cb4b5c4d709dea00
[platform/upstream/libzypp.git] / zypp / thread / Mutex.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/thread/Mutex.h
10  */
11 #ifndef   ZYPP_THREAD_MUTEX_H
12 #define   ZYPP_THREAD_MUTEX_H
13
14 #include "zypp/base/NonCopyable.h"
15 #include "zypp/thread/MutexException.h"
16 #include <pthread.h>
17
18 //////////////////////////////////////////////////////////////////////
19 namespace zypp
20 { ////////////////////////////////////////////////////////////////////
21   ////////////////////////////////////////////////////////////////////
22   namespace thread
23   { //////////////////////////////////////////////////////////////////
24
25
26     typedef pthread_mutex_t RecursiveMutex_t;
27
28
29     ////////////////////////////////////////////////////////////////
30     //
31     // CLASS NAME : Mutex
32     //
33     /** A recursive Mutex.
34      */
35     class Mutex: public zypp::base::NonCopyable
36     {
37     public:
38       /** Create a new recursive Mutex object.
39        * \throws MutexException on initialization failure.
40        */
41       Mutex();
42
43       /** Destroys this Mutex object.
44        */
45       ~Mutex();
46
47       /** Acquire ownership of this Mutex object.
48        * This call will block if another thread has ownership of
49        * this Mutex. When it returns, the current thread is the
50        * owner of this Mutex object.
51        *
52        * In the same thread, this recursive mutex can be acquired
53        * multiple times.
54        *
55        * \throws MutexException if the maximum number of recursive
56        *         locks for mutex has been exceeded.
57        */
58       void lock();
59
60       /** Release ownership of this Mutex object.
61        * If another thread is waiting to acquire the ownership of
62        * this mutex it will stop blocking and acquire ownership
63        * when this call returns.
64        *
65        * \throws MutexException if the current thread does not
66        *         own the mutex.
67        */
68       void unlock();
69
70       /** Try to acquire ownership of this Mutex object.
71        * This call will return false if another thread has ownership
72        * of this Mutex or the maximum number of recursive locks for
73        * mutex has been exceeded.
74        * When it returns true, the current thread is the owner of
75        * this Mutex object.
76        *
77        * \return true, if ownership was acquired.
78        */
79       bool trylock();
80
81     private:
82       RecursiveMutex_t m_mutex;
83     };
84
85
86     //////////////////////////////////////////////////////////////////
87   } // namespace thread
88   ////////////////////////////////////////////////////////////////////
89   ////////////////////////////////////////////////////////////////////
90 } // namespace zypp
91 //////////////////////////////////////////////////////////////////////
92
93 #endif // ZYPP_THREAD_MUTEX_H
94 /*
95 ** vim: set ts=2 sts=2 sw=2 ai et:
96 */