LibreOffice Module slideshow (master) 1
eventqueue.cxx
Go to the documentation of this file.
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
22#include <sal/log.hxx>
23
24#include <event.hxx>
25#include <eventqueue.hxx>
27
28#include <limits>
29#include <memory>
30#include <utility>
31
32
33using namespace ::com::sun::star;
34
35namespace slideshow::internal
36{
38 {
39 // negate comparison, we want priority queue to be sorted
40 // in increasing order of activation times
41 return nTime > rEvent.nTime;
42 }
43
44
46 std::shared_ptr<canvas::tools::ElapsedTime> pPresTimer )
47 : maMutex(),
48 maEvents(),
51 mpTimer(std::move( pPresTimer ))
52 {
53 }
54
56 {
57 // add in all that have been added explicitly for this round:
58 for ( const auto& rEvent : maNextEvents )
59 {
60 maEvents.push(rEvent);
61 }
63
64 // dispose event queue
65 while( !maEvents.empty() )
66 {
67 try
68 {
69 maEvents.top().pEvent->dispose();
70 }
71 catch (const uno::Exception&)
72 {
73 TOOLS_WARN_EXCEPTION("slideshow", "");
74 }
75 maEvents.pop();
76 }
77 }
78
80 {
81 std::unique_lock aGuard( maMutex );
82
83 SAL_INFO("slideshow.eventqueue", "adding event \"" << rEvent->GetDescription()
84 << "\" [" << rEvent.get()
85 << "] at " << mpTimer->getElapsedTime()
86 << " with delay " << rEvent->getActivationTime(0.0)
87 );
89 "EventQueue::addEvent: event ptr NULL" );
90
91 // prepare entry
92
93 // A seemingly obvious optimization cannot be used here,
94 // because it breaks assumed order of notification: zero
95 // timeout events could be fired() immediately, but that
96 // would not unwind the stack and furthermore changes
97 // order of notification
98
99 // add entry
100 maEvents.push( EventEntry( rEvent, rEvent->getActivationTime(
101 mpTimer->getElapsedTime()) ) );
102 return true;
103 }
104
106 {
107 std::unique_lock aGuard( maMutex );
108
109 SAL_INFO("slideshow.eventqueue", "adding event \"" << rEvent->GetDescription()
110 << "\" [" << rEvent.get()
111 << "] for the next round at " << mpTimer->getElapsedTime()
112 << " with delay " << rEvent->getActivationTime(0.0)
113 );
114
116 "EventQueue::addEvent: event ptr NULL" );
117 maNextEvents.emplace_back( rEvent, rEvent->getActivationTime(
118 mpTimer->getElapsedTime()) );
119 return true;
120 }
121
123 {
124 std::unique_lock aGuard( maMutex );
125
126 SAL_INFO("slideshow.eventqueue", "adding event \"" << rpEvent->GetDescription()
127 << "\" [" << rpEvent.get()
128 << "] for execution when the queue is empty at " << mpTimer->getElapsedTime()
129 << " with delay " << rpEvent->getActivationTime(0.0)
130 );
131
132 ENSURE_OR_RETURN_FALSE( rpEvent, "EventQueue::addEvent: event ptr NULL");
133
134 maNextNextEvents.push(
136 rpEvent,
137 rpEvent->getActivationTime(mpTimer->getElapsedTime())));
138
139 return true;
140 }
141
143 {
144 process_(true);
145 }
146
148 {
149 process_(false);
150 }
151
152 void EventQueue::process_( bool bFireAllEvents )
153 {
154 std::unique_lock aGuard( maMutex );
155
156 SAL_INFO("slideshow.verbose", "EventQueue: heartbeat" );
157
158 // add in all that have been added explicitly for this round:
159 for ( const auto& rEvent : maNextEvents ) {
160 maEvents.push(rEvent);
161 }
163
164 // perform topmost, ready-to-execute event
165 // =======================================
166
167 const double nCurrTime( mpTimer->getElapsedTime() );
168
169 // When maEvents does not contain any events that are due now
170 // then process one event from maNextNextEvents.
171 if (!maNextNextEvents.empty()
172 && !bFireAllEvents
173 && (maEvents.empty() || maEvents.top().nTime > nCurrTime))
174 {
175 const EventEntry aEvent (maNextNextEvents.top());
176 maNextNextEvents.pop();
177 maEvents.push(aEvent);
178 }
179
180 // process ready/elapsed events. Note that the 'perceived'
181 // current time remains constant for this loop, thus we're
182 // processing only those events which where ready when we
183 // entered this method.
184 while( !maEvents.empty() &&
185 (bFireAllEvents || maEvents.top().nTime <= nCurrTime) )
186 {
187 EventEntry event( maEvents.top() );
188 maEvents.pop();
189
190 // only process event, if it is still 'charged',
191 // i.e. the fire() call effects something. This is
192 // used when e.g. having events registered at multiple
193 // places, which should fire only once: after the
194 // initial fire() call, those events become inactive
195 // and return false on isCharged. This frees us from
196 // the need to prune queues of those inactive shells.
197 if( event.pEvent->isCharged() )
198 {
199 aGuard.unlock();
200 try
201 {
202 SAL_INFO("slideshow.eventqueue", "firing event \""
203 << event.pEvent->GetDescription()
204 << "\" [" << event.pEvent.get()
205 << "] at " << mpTimer->getElapsedTime()
206 << " with delay " << event.pEvent->getActivationTime(0.0)
207 );
208 event.pEvent->fire();
209 SAL_INFO("slideshow.eventqueue", "event \""
210 << event.pEvent->GetDescription()
211 << "\" [" << event.pEvent.get() << "] fired"
212 );
213 }
214 catch( uno::RuntimeException& )
215 {
216 throw;
217 }
218 catch( uno::Exception& )
219 {
220 // catch anything here, we don't want
221 // to leave this scope under _any_
222 // circumstance. Although, do _not_
223 // reinsert an activity that threw
224 // once.
225
226 // NOTE: we explicitly don't catch(...) here,
227 // since this will also capture segmentation
228 // violations and the like. In such a case, we
229 // still better let our clients now...
230 TOOLS_WARN_EXCEPTION( "slideshow", "" );
231 }
232 catch( SlideShowException& )
233 {
234 // catch anything here, we don't want
235 // to leave this scope under _any_
236 // circumstance. Although, do _not_
237 // reinsert an activity that threw
238 // once.
239
240 // NOTE: we explicitly don't catch(...) here,
241 // since this will also capture segmentation
242 // violations and the like. In such a case, we
243 // still better let our clients now...
244 SAL_WARN("slideshow.eventqueue", "::presentation::internal::EventQueue: Event threw a SlideShowException, action might not have been fully performed" );
245 }
246 aGuard.lock();
247 }
248 else
249 {
250 SAL_INFO(
251 "slideshow.eventqueue",
252 "Ignoring discharged event: unknown ("
253 << event.pEvent.get() << "), timeout was: "
254 << event.pEvent->getActivationTime(0.0));
255 }
256 }
257 }
258
260 {
261 std::unique_lock aGuard( maMutex );
262
263 return maEvents.empty() && maNextEvents.empty() && maNextNextEvents.empty();
264 }
265
267 {
268 std::unique_lock aGuard( maMutex );
269
270 // return time for next entry (if any)
271 double nTimeout (::std::numeric_limits<double>::max());
272 const double nCurrentTime (mpTimer->getElapsedTime());
273 if ( ! maEvents.empty())
274 nTimeout = maEvents.top().nTime - nCurrentTime;
275 if ( ! maNextEvents.empty())
276 nTimeout = ::std::min(nTimeout, maNextEvents.front().nTime - nCurrentTime);
277 if ( ! maNextNextEvents.empty())
278 nTimeout = ::std::min(nTimeout, maNextNextEvents.top().nTime - nCurrentTime);
279
280 return nTimeout;
281 }
282
284 {
285 std::unique_lock aGuard( maMutex );
286
287 // TODO(P1): Maybe a plain vector and vector.swap will
288 // be faster here. Profile.
290
291 maNextEvents.clear();
293 }
294}
295
296/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
EventQueue(std::shared_ptr< ::canvas::tools::ElapsedTime > pPresTimer)
std::shared_ptr< ::canvas::tools::ElapsedTime > mpTimer
Definition: eventqueue.hxx:142
bool addEventForNextRound(const EventSharedPtr &event)
Add the given event to the queue.
Definition: eventqueue.cxx:105
void forceEmpty()
Forces an empty queue, firing all events immediately without minding any times.
Definition: eventqueue.cxx:142
::std::priority_queue< EventEntry > ImplQueueType
Definition: eventqueue.hxx:132
bool addEvent(const EventSharedPtr &event)
Add the given event to the queue.
Definition: eventqueue.cxx:79
double nextTimeout() const
Query timeout for the topmost event in the queue.
Definition: eventqueue.cxx:266
::std::vector< EventEntry > EventEntryVector
Definition: eventqueue.hxx:134
void process()
Process the event queue.
Definition: eventqueue.cxx:147
void process_(bool bFireAllEvents)
Definition: eventqueue.cxx:152
bool addEventWhenQueueIsEmpty(const EventSharedPtr &rpEvent)
Another way to control the order of asynchronous event execution.
Definition: eventqueue.cxx:122
void clear()
Remove all pending events from the queue.
Definition: eventqueue.cxx:283
bool isEmpty() const
Query state of the queue.
Definition: eventqueue.cxx:259
#define TOOLS_WARN_EXCEPTION(area, stream)
#define ENSURE_OR_RETURN_FALSE(c, m)
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
::std::shared_ptr< Event > EventSharedPtr
Definition: event.hxx:76
bool operator<(const EventEntry &) const
Definition: eventqueue.cxx:37