source: XMLIO_V2/external/src/POCO/Foundation/RWLock_WIN32.hpp @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

File size: 5.2 KB
Line 
1//
2// RWLock_WIN32.cpp
3//
4// $Id: //poco/1.3/Foundation/src/RWLock_WIN32.cpp#5 $
5//
6// Library: Foundation
7// Package: Threading
8// Module:  RWLock
9//
10// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// Permission is hereby granted, free of charge, to any person or organization
14// obtaining a copy of the software and accompanying documentation covered by
15// this license (the "Software") to use, reproduce, display, distribute,
16// execute, and transmit the Software, and to prepare derivative works of the
17// Software, and to permit third-parties to whom the Software is furnished to
18// do so, all subject to the following:
19//
20// The copyright notices in the Software and this entire statement, including
21// the above license grant, this restriction and the following disclaimer,
22// must be included in all copies of the Software, in whole or in part, and
23// all derivative works of the Software, unless such copies or derivative
24// works are solely in the form of machine-executable object code generated by
25// a source language processor.
26//
27// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33// DEALINGS IN THE SOFTWARE.
34//
35
36
37#include <Poco/RWLock_WIN32.h>
38
39
40namespace Poco {
41
42
43RWLockImpl::RWLockImpl(): _readers(0), _writersWaiting(0), _writers(0)
44{
45        _mutex = CreateMutexW(NULL, FALSE, NULL);
46        if (_mutex == NULL)
47                throw SystemException("cannot create reader/writer lock");
48
49        _readEvent = CreateEventW(NULL, TRUE, TRUE, NULL);
50        if (_readEvent == NULL)
51                throw SystemException("cannot create reader/writer lock");
52
53        _writeEvent = CreateEventW(NULL, TRUE, TRUE, NULL);
54        if (_writeEvent == NULL)
55                throw SystemException("cannot create reader/writer lock");
56}
57
58
59RWLockImpl::~RWLockImpl()
60{
61        CloseHandle(_mutex);
62        CloseHandle(_readEvent);
63        CloseHandle(_writeEvent);
64}
65
66
67inline void RWLockImpl::addWriter()
68{
69        switch (WaitForSingleObject(_mutex, INFINITE))
70        {
71        case WAIT_OBJECT_0:
72                if (++_writersWaiting == 1) ResetEvent(_readEvent);
73                ReleaseMutex(_mutex);
74                break;
75        default:
76                throw SystemException("cannot lock reader/writer lock");
77        }
78}
79
80
81inline void RWLockImpl::removeWriter()
82{
83        switch (WaitForSingleObject(_mutex, INFINITE))
84        {
85        case WAIT_OBJECT_0:
86                if (--_writersWaiting == 0 && _writers == 0) SetEvent(_readEvent);
87                ReleaseMutex(_mutex);
88                break;
89        default:
90                throw SystemException("cannot lock reader/writer lock");
91        }
92}
93
94
95void RWLockImpl::readLockImpl()
96{
97        HANDLE h[2];
98        h[0] = _mutex;
99        h[1] = _readEvent;
100        switch (WaitForMultipleObjects(2, h, TRUE, INFINITE))
101        {
102        case WAIT_OBJECT_0:
103        case WAIT_OBJECT_0 + 1:
104                ++_readers;
105                ResetEvent(_writeEvent);
106                ReleaseMutex(_mutex);
107                poco_assert_dbg(_writers == 0);
108                break;
109        default:
110                throw SystemException("cannot lock reader/writer lock");
111        }
112}
113
114
115bool RWLockImpl::tryReadLockImpl()
116{
117        for (;;)
118        {
119                if (_writers != 0 || _writersWaiting != 0)
120                        return false;
121
122                DWORD result = tryReadLockOnce();
123                switch (result)
124                {
125                case WAIT_OBJECT_0:
126                case WAIT_OBJECT_0 + 1:
127                        return true;
128                case WAIT_TIMEOUT:
129                        continue; // try again
130                default:
131                        throw SystemException("cannot lock reader/writer lock");
132                }
133        }
134}
135
136
137void RWLockImpl::writeLockImpl()
138{
139        addWriter();
140        HANDLE h[2];
141        h[0] = _mutex;
142        h[1] = _writeEvent;
143        switch (WaitForMultipleObjects(2, h, TRUE, INFINITE))
144        {
145        case WAIT_OBJECT_0:
146        case WAIT_OBJECT_0 + 1:
147                --_writersWaiting;
148                ++_readers;
149                ++_writers;
150                ResetEvent(_readEvent);
151                ResetEvent(_writeEvent);
152                ReleaseMutex(_mutex);
153                poco_assert_dbg(_writers == 1);
154                break;
155        default:
156                removeWriter();
157                throw SystemException("cannot lock reader/writer lock");
158        }
159}
160
161
162bool RWLockImpl::tryWriteLockImpl()
163{
164        addWriter();
165        HANDLE h[2];
166        h[0] = _mutex;
167        h[1] = _writeEvent;
168        switch (WaitForMultipleObjects(2, h, TRUE, 1))
169        {
170        case WAIT_OBJECT_0:
171        case WAIT_OBJECT_0 + 1:
172                --_writersWaiting;
173                ++_readers;
174                ++_writers;
175                ResetEvent(_readEvent);
176                ResetEvent(_writeEvent);
177                ReleaseMutex(_mutex);
178                poco_assert_dbg(_writers == 1);
179                return true;
180        case WAIT_TIMEOUT:
181                removeWriter();
182                return false;
183        default:
184                removeWriter();
185                throw SystemException("cannot lock reader/writer lock");
186        }
187}
188
189
190void RWLockImpl::unlockImpl()
191{
192        switch (WaitForSingleObject(_mutex, INFINITE))
193        {
194        case WAIT_OBJECT_0:
195                _writers = 0;
196                if (_writersWaiting == 0) SetEvent(_readEvent);
197                if (--_readers == 0) SetEvent(_writeEvent);
198                ReleaseMutex(_mutex);
199                break;
200        default:
201                throw SystemException("cannot unlock reader/writer lock");
202        }
203}
204
205
206DWORD RWLockImpl::tryReadLockOnce()
207{
208        HANDLE h[2];
209        h[0] = _mutex;
210        h[1] = _readEvent;
211        DWORD result = WaitForMultipleObjects(2, h, TRUE, 1); 
212        switch (result)
213        {
214        case WAIT_OBJECT_0:
215        case WAIT_OBJECT_0 + 1:
216                ++_readers;
217                ResetEvent(_writeEvent);
218                ReleaseMutex(_mutex);
219                poco_assert_dbg(_writers == 0);
220                return result;
221        case WAIT_TIMEOUT:
222                return result;
223        default:
224                throw SystemException("cannot lock reader/writer lock");
225        }
226}
227
228
229} // namespace Poco
Note: See TracBrowser for help on using the repository browser.