use insert function instead of for loop
[LibreOffice.git] / slideshow / source / engine / eventqueue.cxx
blob86d58a5fe6b2be1ddc319663cfab4d25e2f59610
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 <comphelper/diagnose_ex.hxx>
22 #include <sal/log.hxx>
24 #include <event.hxx>
25 #include <eventqueue.hxx>
26 #include <slideshowexceptions.hxx>
28 #include <limits>
29 #include <memory>
30 #include <utility>
33 using namespace ::com::sun::star;
35 namespace slideshow::internal
37 bool EventQueue::EventEntry::operator<( const EventEntry& rEvent ) const
39 // negate comparison, we want priority queue to be sorted
40 // in increasing order of activation times
41 return nTime > rEvent.nTime;
45 EventQueue::EventQueue(
46 std::shared_ptr<canvas::tools::ElapsedTime> pPresTimer )
47 : maMutex(),
48 maEvents(),
49 maNextEvents(),
50 maNextNextEvents(),
51 mpTimer(std::move( pPresTimer ))
55 EventQueue::~EventQueue()
57 // add in all that have been added explicitly for this round:
58 for ( const auto& rEvent : maNextEvents )
60 maEvents.push(rEvent);
62 EventEntryVector().swap( maNextEvents );
64 // dispose event queue
65 while( !maEvents.empty() )
67 try
69 maEvents.top().pEvent->dispose();
71 catch (const uno::Exception&)
73 TOOLS_WARN_EXCEPTION("slideshow", "");
75 maEvents.pop();
79 bool EventQueue::addEvent( const EventSharedPtr& rEvent )
81 std::unique_lock aGuard( maMutex );
83 SAL_INFO("slideshow.eventqueue", "adding event \"" << rEvent->GetDescription()
84 << "\" [" << rEvent.get()
85 << "] at " << mpTimer->getElapsedTime()
86 << " with delay " << rEvent->getActivationTime(0.0)
88 ENSURE_OR_RETURN_FALSE( rEvent,
89 "EventQueue::addEvent: event ptr NULL" );
91 // prepare entry
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
99 // add entry
100 maEvents.push( EventEntry( rEvent, rEvent->getActivationTime(
101 mpTimer->getElapsedTime()) ) );
102 return true;
105 bool EventQueue::addEventForNextRound( EventSharedPtr const& rEvent )
107 std::unique_lock aGuard( maMutex );
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)
115 ENSURE_OR_RETURN_FALSE( rEvent,
116 "EventQueue::addEvent: event ptr NULL" );
117 maNextEvents.emplace_back( rEvent, rEvent->getActivationTime(
118 mpTimer->getElapsedTime()) );
119 return true;
122 bool EventQueue::addEventWhenQueueIsEmpty (const EventSharedPtr& rpEvent)
124 std::unique_lock aGuard( maMutex );
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)
132 ENSURE_OR_RETURN_FALSE( rpEvent, "EventQueue::addEvent: event ptr NULL");
134 maNextNextEvents.push(
135 EventEntry(
136 rpEvent,
137 rpEvent->getActivationTime(mpTimer->getElapsedTime())));
139 return true;
142 void EventQueue::forceEmpty()
144 process_(true);
147 void EventQueue::process()
149 process_(false);
152 void EventQueue::process_( bool bFireAllEvents )
154 std::unique_lock aGuard( maMutex );
156 SAL_INFO("slideshow.verbose", "EventQueue: heartbeat" );
158 // add in all that have been added explicitly for this round:
159 for ( const auto& rEvent : maNextEvents ) {
160 maEvents.push(rEvent);
162 EventEntryVector().swap( maNextEvents );
164 // perform topmost, ready-to-execute event
165 // =======================================
167 const double nCurrTime( mpTimer->getElapsedTime() );
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))
175 const EventEntry aEvent (maNextNextEvents.top());
176 maNextNextEvents.pop();
177 maEvents.push(aEvent);
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) )
187 EventEntry event( maEvents.top() );
188 maEvents.pop();
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() )
199 aGuard.unlock();
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)
208 event.pEvent->fire();
209 SAL_INFO("slideshow.eventqueue", "event \""
210 << event.pEvent->GetDescription()
211 << "\" [" << event.pEvent.get() << "] fired"
214 catch( uno::RuntimeException& )
216 throw;
218 catch( uno::Exception& )
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.
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", "" );
232 catch( SlideShowException& )
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.
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" );
246 aGuard.lock();
248 else
250 SAL_INFO(
251 "slideshow.eventqueue",
252 "Ignoring discharged event: unknown ("
253 << event.pEvent.get() << "), timeout was: "
254 << event.pEvent->getActivationTime(0.0));
259 bool EventQueue::isEmpty() const
261 std::unique_lock aGuard( maMutex );
263 return maEvents.empty() && maNextEvents.empty() && maNextNextEvents.empty();
266 double EventQueue::nextTimeout() const
268 std::unique_lock aGuard( maMutex );
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);
280 return nTimeout;
283 void EventQueue::clear()
285 std::unique_lock aGuard( maMutex );
287 // TODO(P1): Maybe a plain vector and vector.swap will
288 // be faster here. Profile.
289 maEvents = ImplQueueType();
291 maNextEvents.clear();
292 maNextNextEvents = ImplQueueType();
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */