2 #ifndef ZYPP_BASE_INTER_PROCESS_MUTEX_H
3 #define ZYPP_BASE_INTER_PROCESS_MUTEX_H
6 #include "zypp/base/Fd.h"
7 #include "zypp/base/Exception.h"
8 #include "zypp/base/NonCopyable.h"
9 #include "zypp/Pathname.h"
16 class ZYppLockedException : public Exception
19 ZYppLockedException( const std::string & msg_r,
20 const std::string &name,
22 virtual ~ZYppLockedException() throw();
23 pid_t locker_pid() const { return _locker_pid; }
24 std::string name() const { return _name; }
32 * Inter process scoped lock implementation
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
39 * Multiple readers are allowed if there is no
43 class InterProcessMutex : private base::NonCopyable
47 * Processes can be of two types
57 * options to alter the mutex behavor
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)
68 * The mutex type, Writer or Reader must be
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
77 Options( ConsumerType ptype,
78 const std::string &pname = "zypp",
82 * set the path where the lockfile is
85 void setPath( const Pathname &base );
94 * Creates a mutex with a name and a timeout.
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.
101 * If the timeout is 0, then if the lock is acquired
102 * an exception will be thrown inmediately.
104 * Otherwise, the timeout exception will come after
105 * the timeout is reached.
108 InterProcessMutex( const Options &poptions );
111 * Destructor, gives up the lock on the named
114 ~InterProcessMutex();
117 bool isProcessRunning(pid_t pid_r);
118 Pathname lockFilePath() const;