Fix string hexdecoding
[platform/upstream/libzypp.git] / zypp / base / InterProcessMutex.h
1
2 #ifndef ZYPP_BASE_INTER_PROCESS_MUTEX_H
3 #define ZYPP_BASE_INTER_PROCESS_MUTEX_H
4
5 #include <string>
6 #include "zypp/base/Fd.h"
7 #include "zypp/base/Exception.h"
8 #include "zypp/base/NonCopyable.h"
9 #include "zypp/Pathname.h"
10
11 namespace zypp
12 {
13 namespace base
14 {
15
16 class ZYppLockedException : public Exception
17 {
18 public:
19     ZYppLockedException( const std::string & msg_r,
20                          const std::string &name,
21                          pid_t locker_pid );
22     virtual ~ZYppLockedException() throw();
23     pid_t locker_pid() const { return _locker_pid; }
24     std::string name() const { return _name; }
25 private:
26     pid_t _locker_pid;
27     std::string _name;
28 };
29
30 /**
31  *
32  * Inter process scoped lock implementation
33  *
34  * This mutex will allow only one writer process to
35  * reach a critical region protected by a mutex
36  * of the same name, if there are no readers
37  * at the same time.
38  *
39  * Multiple readers are allowed if there is no
40  * currently a writer.
41  *
42  */
43 class InterProcessMutex : private base::NonCopyable
44 {
45 public:
46    /**
47     * Processes can be of two types
48     * Reader or Writer
49     */
50     enum ConsumerType
51     {
52         Reader,
53         Writer
54     };
55
56    /**
57     * options to alter the mutex behavor
58     */
59    class Options
60    {
61    public:
62        /**
63         * Options for a mutex of type \ref ptype
64         * with a given name and timeout.
65         * Default is name "zypp" and no timeout
66         * (wait till resource is free)
67         *
68         * The mutex type, Writer or Reader must be
69         * given explictly.
70         *
71         * The mutex will be handled using a lock file
72         * located on default library path if the
73         * library is running as root, and in users home
74         * directory if not.
75         *
76         */
77        Options( ConsumerType ptype,
78                 const std::string &pname = "zypp",
79                 int ptimeout = -1 );
80
81        /**
82         * set the path where the lockfile is
83         * created.
84         */
85        void setPath( const Pathname &base );
86
87        std::string name;
88        int timeout;
89        ConsumerType type;
90        Pathname base;
91    };
92     
93    /**
94     * Creates a mutex with a name and a timeout.
95     *
96     * default timeout is -1 which means no timeout
97     * at all, and the mutex will wait forever if
98     * other process is accessing the critical region
99     * for a mutex in with the same name.
100     *
101     * If the timeout is 0, then if the lock is acquired
102     * an exception will be thrown inmediately.
103     *
104     * Otherwise, the timeout exception will come after
105     * the timeout is reached.
106     *
107     */
108     InterProcessMutex( const Options &poptions );
109
110     /**
111      * Destructor, gives up the lock on the named
112      * resource.
113      */
114     ~InterProcessMutex();
115
116 private:
117     bool isProcessRunning(pid_t pid_r);
118     Pathname lockFilePath() const;
119 private:
120     shared_ptr<Fd> _fd;
121     Options _options;
122 };
123
124
125 } }
126
127
128 #endif
129