Debian Codegraph @

Source/Concurrent/CEventThread.cpp

raw
1 /*
2 * Copyright (C) 2013-2015, 2019 by the Konclude Developer Team.
3 *
4 * This file is part of the reasoning system Konclude.
5 * For details and support, see <http://konclude.com/>.
6 *
7 * Konclude is free software: you can redistribute it and/or modify
8 * it under the terms of version 3 of the GNU Lesser General Public
9 * License (LGPLv3) as published by the Free Software Foundation.
10 *
11 * Konclude is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU (Lesser) General Public License for more details.
15 *
16 * You should have received a copy of the GNU (Lesser) General Public
17 * License along with Konclude. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include "CEventThread.h"
22
23
24 namespace Konclude {
25
26 namespace Concurrent {
27
28
29 CEventThread::CEventThread() : CThread("EventThread") {
30 mHandleEventsCountPerStep = 10;
31 mHandleEventStopped = false;
32 mEventSignalized = false;
33 mLastProcessingStartedTag = 0;
34 mLastProcessingStartRequestTag = 0;
35 mEventProcessingStopped = true;
36 threadContext = nullptr;
37 }
38
39 CEventThread::~CEventThread() {
40 }
41
42
43 CEventHandler* CEventThread::createInstalledEventHandler(CEventProcessor* eventProcessor) {
44 CQueuedLinkedEventHandler* eventHandler = new CQueuedLinkedEventHandler(eventProcessor,this);
45 mAddEventHandlerSyncMutex.lock();
46 CXLinker<CEventHandler*>* eventHandlerLinker = new CXLinker<CEventHandler*>(eventHandler,mEventHandlerLinker);
47 mEventHandlerLinker = eventHandlerLinker;
48 mAddEventHandlerSyncMutex.unlock();
49 return eventHandler;
50 }
51
52 CThreadActivator* CEventThread::signalizeEvent() {
53 mEventSignalized = true;
54 if (mEventProcessingStopped) {
55 // post processing event
56 if (mLastProcessingStartedTag == mLastProcessingStartRequestTag) {
57 ++mLastProcessingStartRequestTag;
58 postEvent(new CHandleEventsEvent());
59 }
60 }
61 return this;
62 }
63
64
65 bool CEventThread::processControlEvents(QEvent::Type type, CControlEvent *event) {
66 if (CThread::processControlEvents(type,event)) {
67 return true;
68 } else {
69 if (type == CHandleEventsEvent::EVENTTYPE) {
70 mLastProcessingStartedTag = mLastProcessingStartRequestTag;
71 handleEvents();
72 return true;
73 }
74 }
75 return false;
76 }
77
78 bool CEventThread::handleEvents(double minEventPriority, cint64 maxHandleEventCount) {
79 bool handleNextRound = true;
80 mEventProcessingStopped = false;
81 bool roundEventProcessed = false;
82 while (handleNextRound && !mHandleEventStopped) {
83 roundEventProcessed = false;
84 CXLinker<CEventHandler*>* eventHandlerLinkerIt = mEventHandlerLinker;
85 if (eventHandlerLinkerIt) {
86 while (maxHandleEventCount != 0 && !mHandleEventStopped) {
87
88 CEventHandler* eventHandler = eventHandlerLinkerIt->getData();
89 if (eventHandler->needEventProcessing(minEventPriority)) {
90 cint64 maxHandleEventCountStep = mHandleEventsCountPerStep;
91 if (maxHandleEventCount > 0) {
92 if (maxHandleEventCountStep > maxHandleEventCount) {
93 maxHandleEventCountStep = maxHandleEventCount;
94 }
95 }
96 cint64 handledEventCount = eventHandler->handleEvents(threadContext,minEventPriority,maxHandleEventCountStep);
97 if (maxHandleEventCount > 0) {
98 maxHandleEventCount -= handledEventCount;
99 }
100 roundEventProcessed = handledEventCount > 0;
101 }
102 if (roundEventProcessed) {
103 mEventProcessingStopped = false;
104 }
105 // round robin
106 eventHandlerLinkerIt = eventHandlerLinkerIt->getNext();
107 if (!eventHandlerLinkerIt) {
108 if (mEventSignalized || roundEventProcessed) {
109 handleNextRound = true;
110 mEventSignalized = false;
111 } else {
112 if (!mEventProcessingStopped) {
113 handleNextRound = true;
114 mEventProcessingStopped = true;
115 mEventSignalized = false;
116 } else {
117 handleNextRound = false;
118 }
119 }
120 eventHandlerLinkerIt = mEventHandlerLinker;
121 }
122 }
123 } else {
124 handleNextRound = false;
125 }
126 }
127 return true;
128 }
129
130
131 }; // end namespace Concurrent
132
133 }; // end namespace Konclude
134