root/trunk/dep/include/zthread/FairReadWriteLock.h @ 18

Revision 2, 4.1 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (c) 2005, Eric Crahen
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is furnished
9 * to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22
23#ifndef __ZTFAIRREADWRITELOCK_H__
24#define __ZTFAIRREADWRITELOCK_H__
25
26#include "zthread/ReadWriteLock.h"
27#include "zthread/Condition.h"
28#include "zthread/Guard.h"
29#include "zthread/Mutex.h"
30
31namespace ZThread {
32
33  /**
34   * @class FairReadWriteLock
35   *
36   * @author Eric Crahen <http://www.code-foo.com>
37   * @date <2003-07-16T10:26:25-0400>
38   * @version 2.2.7
39   * 
40   * A FairReadWriteLock maintains a balance between the order read-only access
41   * and read-write access is allowed. Threads contending for the pair of Lockable
42   * objects this ReadWriteLock provides will gain access to the locks in FIFO order.
43   *
44   * @see ReadWriteLock
45   */
46  class FairReadWriteLock : public ReadWriteLock {
47
48    Mutex _lock;
49    Condition _cond;
50 
51    volatile int _readers;
52
53    //! @class ReadLock
54    class ReadLock : public Lockable {
55
56      FairReadWriteLock& _rwlock;
57
58    public:
59
60      ReadLock(FairReadWriteLock& rwlock) : _rwlock(rwlock) {}
61
62      virtual ~ReadLock() {}
63
64      virtual void acquire() {
65
66        Guard<Mutex> g(_rwlock._lock);
67        ++_rwlock._readers; 
68
69      }
70
71      virtual bool tryAcquire(unsigned long timeout) {
72     
73        if(!_rwlock._lock.tryAcquire(timeout))
74          return false;
75
76        ++_rwlock._readers; 
77        _rwlock._lock.release();
78
79        return true;
80      }
81
82      virtual void release() {
83
84        Guard<Mutex> g(_rwlock._lock);
85        --_rwlock._readers; 
86
87        if(_rwlock._readers == 0)
88          _rwlock._cond.signal();
89
90      }
91
92    };
93
94    //! @class WriteLock
95    class WriteLock : public Lockable {
96
97      FairReadWriteLock& _rwlock;
98
99    public:
100
101      WriteLock(FairReadWriteLock& rwlock) : _rwlock(rwlock) {}
102
103      virtual ~WriteLock() {}
104
105      virtual void acquire() {
106
107        _rwlock._lock.acquire();
108
109        try {
110
111          while(_rwlock._readers > 0)
112            _rwlock._cond.wait();
113
114        } catch(...) {
115
116          _rwlock._lock.release();
117          throw;
118
119        }
120
121      }
122
123      virtual bool tryAcquire(unsigned long timeout) {
124     
125        if(!_rwlock._lock.tryAcquire(timeout))
126          return false;
127
128        try {
129
130          while(_rwlock._readers > 0)
131            _rwlock._cond.wait(timeout);
132
133        } catch(...) {
134
135          _rwlock._lock.release();
136          throw;
137
138        }
139
140        return true;
141
142      }
143
144      virtual void release() {
145        _rwlock._lock.release();
146      }
147
148    };
149
150    friend class ReadLock;
151    friend class WriteLock;
152
153    ReadLock _rlock;
154    WriteLock _wlock;
155
156  public:
157 
158    /**
159     * Create a BiasedReadWriteLock
160     *
161     * @exception Initialization_Exception thrown if resources could not be
162     *            allocated for this object.
163     */
164    FairReadWriteLock() : _cond(_lock), _readers(0), _rlock(*this), _wlock(*this) {}
165
166    //! Destroy this ReadWriteLock
167    virtual ~FairReadWriteLock() {}
168
169    /**
170     * @see ReadWriteLock::getReadLock()
171     */
172    virtual Lockable& getReadLock() { return _rlock; }
173
174    /**
175     * @see ReadWriteLock::getWriteLock()
176     */
177    virtual Lockable& getWriteLock() { return _wlock; }
178 
179  };
180
181}; // __ZTFAIRREADWRITELOCK_H__
182
183#endif
Note: See TracBrowser for help on using the browser.