source: XMLIO_V2/external/src/POCO/Foundation.save/Poco/Condition.h @ 80

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

ajout lib externe

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1//
2// Condition.h
3//
4// $Id: //poco/1.3/Foundation/include/Poco/Condition.h#1 $
5//
6// Library: Foundation
7// Package: Threading
8// Module:  Condition
9//
10// Definition of the Condition class template.
11//
12// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
13// and Contributors.
14//
15// Permission is hereby granted, free of charge, to any person or organization
16// obtaining a copy of the software and accompanying documentation covered by
17// this license (the "Software") to use, reproduce, display, distribute,
18// execute, and transmit the Software, and to prepare derivative works of the
19// Software, and to permit third-parties to whom the Software is furnished to
20// do so, all subject to the following:
21//
22// The copyright notices in the Software and this entire statement, including
23// the above license grant, this restriction and the following disclaimer,
24// must be included in all copies of the Software, in whole or in part, and
25// all derivative works of the Software, unless such copies or derivative
26// works are solely in the form of machine-executable object code generated by
27// a source language processor.
28//
29// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35// DEALINGS IN THE SOFTWARE.
36//
37
38
39#ifndef Foundation_Condition_INCLUDED
40#define Foundation_Condition_INCLUDED
41
42
43#include "Poco/Foundation.h"
44#include "Poco/Mutex.h"
45#include "Poco/ScopedUnlock.h"
46#include "Poco/Event.h"
47#include "Poco/Exception.h"
48#include <deque>
49
50
51namespace Poco {
52
53
54class Foundation_API Condition
55        /// A Condition is a synchronization object used to block a thread
56        /// until a particular condition is met.
57        /// A Condition object is always used in conjunction with
58        /// a Mutex (or FastMutex) object.
59        ///
60        /// Condition objects are similar to POSIX condition variables, which the
61        /// difference that Condition is not subject to spurious wakeups.
62        ///
63        /// Threads waiting on a Condition are resumed in FIFO order.
64{
65public:
66        Condition();
67                /// Creates the Condition.
68       
69        ~Condition();
70                /// Destroys the Condition.
71       
72        template <class Mtx>
73        void wait(Mtx& mutex)
74                /// Unlocks the mutex (which must be locked upon calling
75                /// wait()) and waits until the Condition is signalled.
76                ///
77                /// The given mutex will be locked again upon
78                /// leaving the function, even in case of an exception.
79        {
80                ScopedUnlock<Mtx> unlock(mutex, false);
81                Event event;
82                {
83                        FastMutex::ScopedLock lock(_mutex);
84                        mutex.unlock();
85                        enqueue(event);
86                }
87                event.wait();
88        }
89       
90        template <class Mtx>
91        void wait(Mtx& mutex, long milliseconds)
92                /// Unlocks the mutex (which must be locked upon calling
93                /// wait()) and waits for the given time until the Condition is signalled.
94                ///
95                /// The given mutex will be locked again upon successfully leaving the
96                /// function, even in case of an exception.
97                ///
98                /// Throws a TimeoutException if the Condition is not signalled
99                /// within the given time interval.
100        {
101                if (!tryWait(mutex, milliseconds))
102                        throw TimeoutException();
103        }
104       
105        template <class Mtx>
106        bool tryWait(Mtx& mutex, long milliseconds)
107                /// Unlocks the mutex (which must be locked upon calling
108                /// tryWait()) and waits for the given time until the Condition is signalled.
109                ///
110                /// The given mutex will be locked again upon leaving the
111                /// function, even in case of an exception.
112                ///
113                /// Returns true if the Condition has been signalled
114                /// within the given time interval, otherwise false.
115        {
116                ScopedUnlock<Mtx> unlock(mutex, false);
117                Event event;
118                {
119                        FastMutex::ScopedLock lock(_mutex);
120                        mutex.unlock();
121                        enqueue(event);
122                }
123                if (!event.tryWait(milliseconds))
124                {
125                        FastMutex::ScopedLock lock(_mutex);
126                        dequeue(event);
127                        return false;
128                }
129                return true;
130        }
131       
132        void signal();
133                /// Signals the Condition and allows one waiting thread
134                /// to continue execution.
135
136        void broadcast();
137                /// Signals the Condition and allows all waiting
138                /// threads to continue their execution.
139
140protected:
141        void enqueue(Event& event);
142        void dequeue();
143        void dequeue(Event& event);
144       
145private:
146        Condition(const Condition&);
147        Condition& operator = (const Condition&);
148       
149        typedef std::deque<Event*> WaitQueue;
150       
151        FastMutex _mutex;
152        WaitQueue _waitQueue;
153};
154
155
156} // namespace Poco
157
158
159#endif // Foundation_Condition_INCLUDED
Note: See TracBrowser for help on using the repository browser.