bump product version to 4.1.6.2
[LibreOffice.git] / salhelper / source / condition.cxx
blobe330960ebe85635413060afa7facabda017e75cd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <salhelper/condition.hxx>
22 #include <osl/time.h>
25 using namespace salhelper;
28 /******************************************************************
29 * *
30 * Condition *
31 * *
32 ******************************************************************/
34 Condition::Condition(osl::Mutex& aMutex)
35 : m_aMutex(aMutex),
36 m_aCondition(osl_createCondition())
41 Condition::~Condition()
43 osl_destroyCondition(m_aCondition);
47 /******************************************************************
48 * *
49 * ConditionModifier *
50 * *
51 ******************************************************************/
53 ConditionModifier::ConditionModifier(Condition& aCond)
54 : m_aCond(aCond)
56 m_aCond.m_aMutex.acquire();
60 ConditionModifier::~ConditionModifier()
62 if(m_aCond.applies())
63 osl_setCondition(m_aCond.m_aCondition);
65 m_aCond.m_aMutex.release();
70 /******************************************************************
71 * *
72 * ConditionWaiter *
73 * *
74 ******************************************************************/
76 ConditionWaiter::timedout::timedout() {}
78 ConditionWaiter::timedout::timedout(timedout const &) {}
80 ConditionWaiter::timedout::~timedout() {}
82 ConditionWaiter::timedout &
83 ConditionWaiter::timedout::operator =(timedout const &) { return *this; }
85 ConditionWaiter::ConditionWaiter(Condition& aCond)
86 : m_aCond(aCond)
88 while(true) {
89 osl_waitCondition(m_aCond.m_aCondition,0);
90 m_aCond.m_aMutex.acquire();
92 if(m_aCond.applies())
93 break;
94 else {
95 osl_resetCondition(m_aCond.m_aCondition);
96 m_aCond.m_aMutex.release();
102 ConditionWaiter::ConditionWaiter(Condition& aCond,sal_uInt32 milliSec)
103 throw(
104 ConditionWaiter::timedout
106 : m_aCond(aCond)
108 TimeValue aTime;
109 aTime.Seconds = milliSec / 1000;
110 aTime.Nanosec = 1000000 * ( milliSec % 1000 );
112 while(true) {
113 if( osl_waitCondition(m_aCond.m_aCondition,&aTime) ==
114 osl_cond_result_timeout )
115 throw timedout();
117 m_aCond.m_aMutex.acquire();
119 if(m_aCond.applies())
120 break;
121 else {
122 osl_resetCondition(m_aCond.m_aCondition);
123 m_aCond.m_aMutex.release();
129 ConditionWaiter::~ConditionWaiter()
131 if(! m_aCond.applies())
132 osl_resetCondition(m_aCond.m_aCondition);
133 m_aCond.m_aMutex.release();
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */