xref: /trunk/main/sal/inc/osl/conditn.hxx (revision 565d668c)
1*565d668cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*565d668cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*565d668cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*565d668cSAndrew Rist  * distributed with this work for additional information
6*565d668cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*565d668cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*565d668cSAndrew Rist  * "License"); you may not use this file except in compliance
9*565d668cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*565d668cSAndrew Rist  *
11*565d668cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*565d668cSAndrew Rist  *
13*565d668cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*565d668cSAndrew Rist  * software distributed under the License is distributed on an
15*565d668cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*565d668cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*565d668cSAndrew Rist  * specific language governing permissions and limitations
18*565d668cSAndrew Rist  * under the License.
19*565d668cSAndrew Rist  *
20*565d668cSAndrew Rist  *************************************************************/
21*565d668cSAndrew Rist 
22*565d668cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _OSL_CONDITN_HXX_
25cdf0e10cSrcweir #define _OSL_CONDITN_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifdef __cplusplus
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <osl/time.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <osl/conditn.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir 
34cdf0e10cSrcweir namespace osl
35cdf0e10cSrcweir {
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 	class Condition
38cdf0e10cSrcweir 	{
39cdf0e10cSrcweir 	public:
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 		enum Result
42cdf0e10cSrcweir 		{
43cdf0e10cSrcweir 			result_ok      = osl_cond_result_ok,
44cdf0e10cSrcweir 			result_error   = osl_cond_result_error,
45cdf0e10cSrcweir 			result_timeout = osl_cond_result_timeout
46cdf0e10cSrcweir 		};
47cdf0e10cSrcweir 
48cdf0e10cSrcweir         /* Create a condition.
49cdf0e10cSrcweir 		 */
Condition()50cdf0e10cSrcweir         Condition()
51cdf0e10cSrcweir 		{
52cdf0e10cSrcweir 			condition = osl_createCondition();
53cdf0e10cSrcweir 		}
54cdf0e10cSrcweir 
55cdf0e10cSrcweir         /* Release the OS-structures and free condition data-structure.
56cdf0e10cSrcweir          */
~Condition()57cdf0e10cSrcweir 		~Condition()
58cdf0e10cSrcweir 		{
59cdf0e10cSrcweir 			osl_destroyCondition(condition);
60cdf0e10cSrcweir 		}
61cdf0e10cSrcweir 
62cdf0e10cSrcweir         /* Release all waiting threads, check returns sal_True.
63cdf0e10cSrcweir          */
set()64cdf0e10cSrcweir         void set()
65cdf0e10cSrcweir 		{
66cdf0e10cSrcweir 			osl_setCondition(condition);
67cdf0e10cSrcweir 		}
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 		/* Reset condition to false: wait() will block, check() returns sal_False.
70cdf0e10cSrcweir          */
reset()71cdf0e10cSrcweir 		void reset() {
72cdf0e10cSrcweir 			osl_resetCondition(condition);
73cdf0e10cSrcweir 		}
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 		/** Blocks the calling thread until condition is set.
76cdf0e10cSrcweir 		 */
wait(const TimeValue * pTimeout=0)77cdf0e10cSrcweir 		Result wait(const TimeValue *pTimeout = 0)
78cdf0e10cSrcweir 		{
79cdf0e10cSrcweir 			return (Result) osl_waitCondition(condition, pTimeout);
80cdf0e10cSrcweir 		}
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 		/** Checks if the condition is set without blocking.
83cdf0e10cSrcweir 		 */
check()84cdf0e10cSrcweir 		sal_Bool check()
85cdf0e10cSrcweir 		{
86cdf0e10cSrcweir 			return osl_checkCondition(condition);
87cdf0e10cSrcweir 		}
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	private:
91cdf0e10cSrcweir 		oslCondition condition;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         /** The underlying oslCondition has no reference count.
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         Since the underlying oslCondition is not a reference counted object, copy
96cdf0e10cSrcweir         constructed Condition may work on an already destructed oslCondition object.
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         */
99cdf0e10cSrcweir         Condition(const Condition&);
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         /** The underlying oslCondition has no reference count.
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         When destructed, the Condition object destroys the undelying oslCondition,
104cdf0e10cSrcweir         which might cause severe problems in case it's a temporary object.
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         */
107cdf0e10cSrcweir         Condition(oslCondition condition);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         /** This assignment operator is private for the same reason as
110cdf0e10cSrcweir             the copy constructor.
111cdf0e10cSrcweir         */
112cdf0e10cSrcweir         Condition& operator= (const Condition&);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         /** This assignment operator is private for the same reason as
115cdf0e10cSrcweir             the constructor taking a oslCondition argument.
116cdf0e10cSrcweir         */
117cdf0e10cSrcweir         Condition& operator= (oslCondition);
118cdf0e10cSrcweir 	};
119cdf0e10cSrcweir 
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir #endif  /* __cplusplus */
123cdf0e10cSrcweir #endif	/* _OSL_CONDITN_HXX_ */
124cdf0e10cSrcweir 
125