1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 29 #include <salhelper/condition.hxx> 30 #include <osl/time.h> 31 32 33 using namespace salhelper; 34 35 36 /****************************************************************** 37 * * 38 * Condition * 39 * * 40 ******************************************************************/ 41 42 Condition::Condition(osl::Mutex& aMutex) 43 : m_aMutex(aMutex), 44 m_aCondition(osl_createCondition()) 45 { 46 } 47 48 49 Condition::~Condition() 50 { 51 osl_destroyCondition(m_aCondition); 52 } 53 54 55 /****************************************************************** 56 * * 57 * ConditionModifier * 58 * * 59 ******************************************************************/ 60 61 ConditionModifier::ConditionModifier(Condition& aCond) 62 : m_aCond(aCond) 63 { 64 m_aCond.m_aMutex.acquire(); 65 } 66 67 68 ConditionModifier::~ConditionModifier() 69 { 70 if(m_aCond.applies()) 71 osl_setCondition(m_aCond.m_aCondition); 72 73 m_aCond.m_aMutex.release(); 74 } 75 76 77 78 /****************************************************************** 79 * * 80 * ConditionWaiter * 81 * * 82 ******************************************************************/ 83 84 ConditionWaiter::timedout::timedout() {} 85 86 ConditionWaiter::timedout::timedout(timedout const &) {} 87 88 ConditionWaiter::timedout::~timedout() {} 89 90 ConditionWaiter::timedout & 91 ConditionWaiter::timedout::operator =(timedout const &) { return *this; } 92 93 ConditionWaiter::ConditionWaiter(Condition& aCond) 94 : m_aCond(aCond) 95 { 96 while(true) { 97 osl_waitCondition(m_aCond.m_aCondition,0); 98 m_aCond.m_aMutex.acquire(); 99 100 if(m_aCond.applies()) 101 break; 102 else { 103 osl_resetCondition(m_aCond.m_aCondition); 104 m_aCond.m_aMutex.release(); 105 } 106 } 107 } 108 109 110 ConditionWaiter::ConditionWaiter(Condition& aCond,sal_uInt32 milliSec) 111 throw( 112 ConditionWaiter::timedout 113 ) 114 : m_aCond(aCond) 115 { 116 TimeValue aTime; 117 aTime.Seconds = milliSec / 1000; 118 aTime.Nanosec = 1000000 * ( milliSec % 1000 ); 119 120 while(true) { 121 if( osl_waitCondition(m_aCond.m_aCondition,&aTime) == 122 osl_cond_result_timeout ) 123 throw timedout(); 124 125 m_aCond.m_aMutex.acquire(); 126 127 if(m_aCond.applies()) 128 break; 129 else { 130 osl_resetCondition(m_aCond.m_aCondition); 131 m_aCond.m_aMutex.release(); 132 } 133 } 134 } 135 136 137 ConditionWaiter::~ConditionWaiter() 138 { 139 if(! m_aCond.applies()) 140 osl_resetCondition(m_aCond.m_aCondition); 141 m_aCond.m_aMutex.release(); 142 } 143