interprocess_sync.h 3.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#ifndef __INTERPROCESS_SYNC_H__
#define __INTERPROCESS_SYNC_H__

#include <string>

namespace isc {
namespace util {

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class InterprocessSyncLocker; // forward declaration

/// \brief Interprocess Sync Class
///
/// This class specifies an interface for mutual exclusion among
/// co-operating processes. This is an abstract class and a real
/// implementation such as InterprocessSyncFile should be used
/// in code. Usage is as follows:
///
/// 1. Client instantiates a sync object of an implementation (such as
/// InterprocessSyncFile).
/// 2. Client then creates an automatic (stack) object of
/// InterprocessSyncLocker around the sync object. Such an object
/// destroys itself and releases any acquired lock when it goes out of extent.
/// 3. Client calls lock() method on the InterprocessSyncLocker.
/// 4. Client performs task that needs mutual exclusion.
/// 5. Client frees lock with unlock(), or simply returns from the basic
/// block which forms the scope for the InterprocessSyncLocker.
41
class InterprocessSync {
42
43
44
45
46
  // InterprocessSyncLocker is the only code outside this class that
  // should be allowed to call the lock(), tryLock() and unlock()
  // methods.
  friend class InterprocessSyncLocker;

47
48
49
public:
    /// \brief Constructor
    ///
50
    /// Creates an interprocess synchronization object
51
    ///
52
    /// \param task_name Name of the synchronization task. This has to be
53
54
55
56
    /// identical among the various processes that need to be
    /// synchronized for the same task.
    InterprocessSync(const std::string& task_name) :
        task_name_(task_name), is_locked_(false)
57
    {}
58
59
60
61
62

    /// \brief Destructor
    virtual ~InterprocessSync() {}

protected:
63
64
    /// \brief Acquire the lock (blocks if something else has acquired a
    /// lock on the same task name)
65
    virtual bool lock() = 0;
66
    /// \brief Try to acquire a lock (doesn't block)
67
    virtual bool tryLock() = 0;
68
    /// \brief Release the lock
69
70
    virtual bool unlock() = 0;

71
72
    const std::string task_name_; ///< The task name
    bool is_locked_;              ///< Is the lock taken?
73
74
};

75
76
77
78
79
80
/// \brief Interprocess Sync Locker Class
///
/// This class is used for making automatic stack objects to manage
/// locks that are released automatically when the block is exited
/// (RAII). It is meant to be used along with InterprocessSync objects. See
/// the description of InterprocessSync.
81
82
class InterprocessSyncLocker {
public:
83
84
85
    InterprocessSyncLocker(InterprocessSync& sync) :
        sync_(sync)
    {}
86
87

    /// \brief Destructor
88
89
90
91
    ~InterprocessSyncLocker() {
        unlock();
    }

92
93
    /// \brief Acquire the lock (blocks if something else has acquired a
    /// lock on the same task name)
94
    bool lock() {
95
        return (sync_.lock());
96
97
    }

98
    /// \brief Try to acquire a lock (doesn't block)
99
    bool tryLock() {
100
        return (sync_.tryLock());
101
102
    }

103
    /// \brief Release the lock
104
    bool unlock() {
105
        return (sync_.unlock());
106
    }
107
108

protected:
109
    InterprocessSync& sync_; ///< Ref to underlying sync object
110
111
112
113
114
115
};

} // namespace util
} // namespace isc

#endif // __INTERPROCESS_SYNC_H__