/src/libreoffice/salhelper/source/condition.cxx
Line | Count | Source (jump to first uncovered line) |
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 | | #include <osl/mutex.hxx> |
24 | | |
25 | | using namespace salhelper; |
26 | | |
27 | | |
28 | | /****************************************************************** |
29 | | * * |
30 | | * Condition * |
31 | | * * |
32 | | ******************************************************************/ |
33 | | |
34 | | Condition::Condition(osl::Mutex& aMutex) |
35 | 0 | : m_aMutex(aMutex) |
36 | 0 | { |
37 | 0 | } |
38 | | |
39 | | |
40 | | Condition::~Condition() |
41 | 0 | { |
42 | 0 | } |
43 | | |
44 | | |
45 | | /****************************************************************** |
46 | | * * |
47 | | * ConditionModifier * |
48 | | * * |
49 | | ******************************************************************/ |
50 | | |
51 | | ConditionModifier::ConditionModifier(Condition& aCond) |
52 | 0 | : m_aCond(aCond) |
53 | 0 | { |
54 | 0 | m_aCond.m_aMutex.acquire(); |
55 | 0 | } |
56 | | |
57 | | |
58 | | ConditionModifier::~ConditionModifier() |
59 | 0 | { |
60 | 0 | if(m_aCond.applies()) |
61 | 0 | m_aCond.m_aCondition.set(); |
62 | |
|
63 | 0 | m_aCond.m_aMutex.release(); |
64 | 0 | } |
65 | | |
66 | | |
67 | | /****************************************************************** |
68 | | * * |
69 | | * ConditionWaiter * |
70 | | * * |
71 | | ******************************************************************/ |
72 | | |
73 | 0 | ConditionWaiter::timedout::timedout() {} |
74 | | |
75 | 0 | ConditionWaiter::timedout::timedout(timedout const &) {} |
76 | | |
77 | 0 | ConditionWaiter::timedout::~timedout() {} |
78 | | |
79 | | ConditionWaiter::timedout & |
80 | 0 | ConditionWaiter::timedout::operator =(timedout const &) { return *this; } |
81 | | |
82 | | ConditionWaiter::ConditionWaiter(Condition& aCond) |
83 | 0 | : m_aCond(aCond) |
84 | 0 | { |
85 | 0 | while(true) { |
86 | 0 | m_aCond.m_aCondition.wait(); |
87 | 0 | m_aCond.m_aMutex.acquire(); |
88 | |
|
89 | 0 | if(m_aCond.applies()) |
90 | 0 | break; |
91 | 0 | else { |
92 | 0 | m_aCond.m_aCondition.reset(); |
93 | 0 | m_aCond.m_aMutex.release(); |
94 | 0 | } |
95 | 0 | } |
96 | 0 | } |
97 | | |
98 | | |
99 | | ConditionWaiter::ConditionWaiter(Condition& aCond,sal_uInt32 milliSec) |
100 | 0 | : m_aCond(aCond) |
101 | 0 | { |
102 | 0 | TimeValue aTime; |
103 | 0 | aTime.Seconds = milliSec / 1000; |
104 | 0 | aTime.Nanosec = 1000000 * ( milliSec % 1000 ); |
105 | |
|
106 | 0 | while(true) { |
107 | 0 | if( m_aCond.m_aCondition.wait(&aTime) == |
108 | 0 | osl::Condition::result_timeout ) |
109 | 0 | throw timedout(); |
110 | | |
111 | 0 | m_aCond.m_aMutex.acquire(); |
112 | |
|
113 | 0 | if(m_aCond.applies()) |
114 | 0 | break; |
115 | 0 | else { |
116 | 0 | m_aCond.m_aCondition.reset(); |
117 | 0 | m_aCond.m_aMutex.release(); |
118 | 0 | } |
119 | 0 | } |
120 | 0 | } |
121 | | |
122 | | |
123 | | ConditionWaiter::~ConditionWaiter() |
124 | 0 | { |
125 | 0 | if(! m_aCond.applies()) |
126 | 0 | m_aCond.m_aCondition.reset(); |
127 | 0 | m_aCond.m_aMutex.release(); |
128 | 0 | } |
129 | | |
130 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |