Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : :
21 : : #include <salhelper/condition.hxx>
22 : : #include <osl/time.h>
23 : :
24 : :
25 : : using namespace salhelper;
26 : :
27 : :
28 : : /******************************************************************
29 : : * *
30 : : * Condition *
31 : : * *
32 : : ******************************************************************/
33 : :
34 : 0 : Condition::Condition(osl::Mutex& aMutex)
35 : : : m_aMutex(aMutex),
36 : 0 : m_aCondition(osl_createCondition())
37 : : {
38 : 0 : }
39 : :
40 : :
41 : 0 : Condition::~Condition()
42 : : {
43 : 0 : osl_destroyCondition(m_aCondition);
44 [ # # ]: 0 : }
45 : :
46 : :
47 : : /******************************************************************
48 : : * *
49 : : * ConditionModifier *
50 : : * *
51 : : ******************************************************************/
52 : :
53 : 0 : ConditionModifier::ConditionModifier(Condition& aCond)
54 : 0 : : m_aCond(aCond)
55 : : {
56 : 0 : m_aCond.m_aMutex.acquire();
57 : 0 : }
58 : :
59 : :
60 : 0 : ConditionModifier::~ConditionModifier()
61 : : {
62 [ # # ]: 0 : if(m_aCond.applies())
63 : 0 : osl_setCondition(m_aCond.m_aCondition);
64 : :
65 : 0 : m_aCond.m_aMutex.release();
66 : 0 : }
67 : :
68 : :
69 : :
70 : : /******************************************************************
71 : : * *
72 : : * ConditionWaiter *
73 : : * *
74 : : ******************************************************************/
75 : :
76 : 0 : ConditionWaiter::timedout::timedout() {}
77 : :
78 : 0 : ConditionWaiter::timedout::timedout(timedout const &) {}
79 : :
80 [ # # ]: 0 : ConditionWaiter::timedout::~timedout() {}
81 : :
82 : : ConditionWaiter::timedout &
83 : 0 : ConditionWaiter::timedout::operator =(timedout const &) { return *this; }
84 : :
85 : 0 : ConditionWaiter::ConditionWaiter(Condition& aCond)
86 : 0 : : m_aCond(aCond)
87 : : {
88 : 0 : while(true) {
89 : 0 : osl_waitCondition(m_aCond.m_aCondition,0);
90 : 0 : m_aCond.m_aMutex.acquire();
91 : :
92 [ # # ]: 0 : if(m_aCond.applies())
93 : 0 : break;
94 : : else {
95 : 0 : osl_resetCondition(m_aCond.m_aCondition);
96 : 0 : m_aCond.m_aMutex.release();
97 : : }
98 : : }
99 : 0 : }
100 : :
101 : :
102 : 0 : ConditionWaiter::ConditionWaiter(Condition& aCond,sal_uInt32 milliSec)
103 : : throw(
104 : : ConditionWaiter::timedout
105 : : )
106 : 0 : : m_aCond(aCond)
107 : : {
108 : : TimeValue aTime;
109 : 0 : aTime.Seconds = milliSec / 1000;
110 : 0 : aTime.Nanosec = 1000000 * ( milliSec % 1000 );
111 : :
112 : 0 : while(true) {
113 [ # # ][ # # ]: 0 : if( osl_waitCondition(m_aCond.m_aCondition,&aTime) ==
114 : : osl_cond_result_timeout )
115 [ # # ]: 0 : throw timedout();
116 : :
117 [ # # ]: 0 : m_aCond.m_aMutex.acquire();
118 : :
119 [ # # ][ # # ]: 0 : if(m_aCond.applies())
120 : 0 : break;
121 : : else {
122 [ # # ]: 0 : osl_resetCondition(m_aCond.m_aCondition);
123 [ # # ]: 0 : m_aCond.m_aMutex.release();
124 : : }
125 : : }
126 : 0 : }
127 : :
128 : :
129 : 0 : ConditionWaiter::~ConditionWaiter()
130 : : {
131 [ # # ]: 0 : if(! m_aCond.applies())
132 : 0 : osl_resetCondition(m_aCond.m_aCondition);
133 : 0 : m_aCond.m_aMutex.release();
134 : 0 : }
135 : :
136 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|