LibreOffice Module vcl (master)  1
salusereventlist.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 #include <salusereventlist.hxx>
21 #include <salwtype.hxx>
22 
23 #include <algorithm>
24 #include <cstdlib>
25 #include <exception>
26 #include <typeinfo>
27 
28 #include <com/sun/star/uno/Exception.hpp>
29 #include <tools/debug.hxx>
30 #include <tools/diagnose_ex.h>
31 #include <sal/log.hxx>
32 #include <sal/types.h>
33 #include <svdata.hxx>
34 
36  : m_bAllUserEventProcessedSignaled( true )
37  , m_aProcessingThread(0)
38 {
39 }
40 
41 SalUserEventList::~SalUserEventList() COVERITY_NOEXCEPT_FALSE
42 {
43 }
44 
46 {
47  auto aPair = m_aFrames.insert( pFrame );
48  assert( aPair.second ); (void) aPair;
49 }
50 
52 {
53  auto it = m_aFrames.find( pFrame );
54  assert( it != m_aFrames.end() );
55  if ( it != m_aFrames.end() )
56  m_aFrames.erase( it );
57 }
58 
59 bool SalUserEventList::DispatchUserEvents( bool bHandleAllCurrentEvents )
60 {
61  bool bWasEvent = false;
62  oslThreadIdentifier aCurId = osl::Thread::getCurrentIdentifier();
63 
65  osl::ResettableMutexGuard aResettableListGuard(m_aUserEventsMutex);
66 
67  if (!m_aUserEvents.empty())
68  {
69  if (bHandleAllCurrentEvents)
70  {
71  if (m_aProcessingUserEvents.empty())
73  else
75  }
76  else if (m_aProcessingUserEvents.empty())
77  {
78  m_aProcessingUserEvents.push_back( m_aUserEvents.front() );
79  m_aUserEvents.pop_front();
80  }
81  }
82 
83  if (HasUserEvents())
84  {
85  bWasEvent = true;
86  m_aProcessingThread = aCurId;
87 
88  SalUserEvent aEvent( nullptr, nullptr, SalEvent::NONE );
89  do {
90  if (m_aProcessingUserEvents.empty() || aCurId != m_aProcessingThread)
91  break;
92  aEvent = m_aProcessingUserEvents.front();
93  m_aProcessingUserEvents.pop_front();
94 
95  // remember to reset the guard before break or continue the loop
96  aResettableListGuard.clear();
97 
98  if ( !isFrameAlive( aEvent.m_pFrame ) )
99  {
100  if ( aEvent.m_nEvent == SalEvent::UserEvent )
101  delete static_cast< ImplSVEvent* >( aEvent.m_pData );
102  aResettableListGuard.reset();
103  continue;
104  }
105 
106  /*
107  * Current policy is that scheduler tasks aren't allowed to throw an exception.
108  * Because otherwise the exception is caught somewhere totally unrelated.
109  * TODO Ideally we could capture a proper backtrace and feed this into breakpad,
110  * which is do-able, but requires writing some assembly.
111  * See also Scheduler::CallbackTaskScheduling
112  */
113 #ifdef IOS
114  ProcessEvent( aEvent );
115 #else
116  // the noexcept here means that (a) we abort and (b) debuggers will
117  // likely trigger at the throw site instead of here, making the debugging
118  // experience better when something goes wrong.
119  auto process = [&aEvent, this] () noexcept { ProcessEvent(aEvent); };
120  process();
121 #endif
122  aResettableListGuard.reset();
123  if (!bHandleAllCurrentEvents)
124  break;
125  }
126  while( true );
127  }
128 
130  {
133  }
134 
135  return bWasEvent;
136 }
137 
138 void SalUserEventList::RemoveEvent( SalFrame* pFrame, void* pData, SalEvent nEvent )
139 {
140  SalUserEvent aEvent( pFrame, pData, nEvent );
141 
142  osl::MutexGuard aGuard( m_aUserEventsMutex );
143  auto it = std::find( m_aUserEvents.begin(), m_aUserEvents.end(), aEvent );
144  if ( it != m_aUserEvents.end() )
145  {
146  m_aUserEvents.erase( it );
147  }
148  else
149  {
150  it = std::find( m_aProcessingUserEvents.begin(), m_aProcessingUserEvents.end(), aEvent );
151  if ( it != m_aProcessingUserEvents.end() )
152  {
153  m_aProcessingUserEvents.erase( it );
154  }
155  }
156 
158  {
161  }
162 }
163 
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void insertFrame(SalFrame *pFrame)
const_iterator find(const Value &x) const
std::list< SalUserEvent > m_aProcessingUserEvents
virtual ~SalUserEventList() COVERITY_NOEXCEPT_FALSE
std::list< SalUserEvent > m_aUserEvents
virtual void TriggerAllUserEventsProcessed()
osl::Mutex m_aUserEventsMutex
bool DispatchUserEvents(bool bHandleAllCurrentEvents)
const_iterator end() const
virtual void ProcessEvent(SalUserEvent aEvent)=0
bool HasUserEvents() const
void RemoveEvent(SalFrame *pFrame, void *pData, SalEvent nEvent)
SalEvent
Definition: salwtype.hxx:41
A SalFrame is a system window (e.g. an X11 window).
Definition: salframe.hxx:114
void eraseFrame(SalFrame *pFrame)
bool isFrameAlive(const SalFrame *pFrame) const
bool m_bAllUserEventProcessedSignaled
#define DBG_TESTSOLARMUTEX()
oslThreadIdentifier m_aProcessingThread
std::pair< const_iterator, bool > insert(Value &&x)
AnyEventRef aEvent
size_type erase(const Value &x)
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo