| 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 "CEventLinkerQueue.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Concurrent { |
| 27 |
|
| 28 |
|
| 29 |
CEventLinkerQueue::CEventLinkerQueue(CThreadActivator* threadActivator) { |
| 30 |
mThreadActivator = threadActivator; |
| 31 |
mDefaultEventChannel = new CSyncedEventChannel(this); |
| 32 |
mReverseSortedChannelLinker = new CEventChannelLinker(mDefaultEventChannel); |
| 33 |
|
| 34 |
mActivatorThreshold = 1; |
| 35 |
mInexactEventSingnals = 0; |
| 36 |
mThreadActivated = false; |
| 37 |
} |
| 38 |
|
| 39 |
CEventLinkerQueue::~CEventLinkerQueue() { |
| 40 |
while (mReverseSortedChannelLinker) { |
| 41 |
CEventChannelLinker* tmpDeleteChannel = mReverseSortedChannelLinker; |
| 42 |
mReverseSortedChannelLinker = mReverseSortedChannelLinker->getNext(); |
| 43 |
delete tmpDeleteChannel->getData(); |
| 44 |
delete tmpDeleteChannel; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
CSyncedEventChannel* CEventLinkerQueue::getDefaultSyncedEventChannel(double channelPriority) { |
| 50 |
return mDefaultEventChannel; |
| 51 |
} |
| 52 |
|
| 53 |
CSyncedFreeEventChannel* CEventLinkerQueue::createSyncedFreeEventChannel(double channelPriority) { |
| 54 |
mCreateChannelSyncMutex.lock(); |
| 55 |
CSyncedFreeEventChannel* tmpChannel = new CSyncedFreeEventChannel(this); |
| 56 |
CEventChannelLinker* tmpChannelLinker = new CEventChannelLinker(tmpChannel); |
| 57 |
mReverseSortedChannelLinker = mReverseSortedChannelLinker->insertSortedNextSorted(tmpChannelLinker); |
| 58 |
mCreateChannelSyncMutex.unlock(); |
| 59 |
return tmpChannel; |
| 60 |
} |
| 61 |
|
| 62 |
CThreadActivator* CEventLinkerQueue::signalizeEvent() { |
| 63 |
mInexactEventSingnals = mInexactEventSingnals+1; |
| 64 |
if (mInexactEventSingnals >= mActivatorThreshold) { |
| 65 |
if (true || !mThreadActivated) { |
| 66 |
// may called multiple times |
| 67 |
mThreadActivated = true; |
| 68 |
if (mThreadActivator) { |
| 69 |
mThreadActivator->signalizeEvent(); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
return this; |
| 74 |
} |
| 75 |
|
| 76 |
CEventLinker* CEventLinkerQueue::takeEvents(CEventLinker* addEventLinker, double minEventPriority) { |
| 77 |
mThreadActivated = false; |
| 78 |
mInexactEventSingnals = 0; |
| 79 |
CEventChannelLinker* channelLinkerIt = mReverseSortedChannelLinker; |
| 80 |
while (channelLinkerIt) { |
| 81 |
CEventChannel *channel = channelLinkerIt->getData(); |
| 82 |
if (channel->getChannelPriority() >= minEventPriority) { |
| 83 |
CEventLinkerChannel* linkerChannel = dynamic_cast<CEventLinkerChannel*>(channel); |
| 84 |
if (linkerChannel) { |
| 85 |
addEventLinker = linkerChannel->takeEvents(addEventLinker); |
| 86 |
} else { |
| 87 |
// take events step for step, currently not supported |
| 88 |
} |
| 89 |
} |
| 90 |
channelLinkerIt = channelLinkerIt->getNext(); |
| 91 |
} |
| 92 |
return addEventLinker; |
| 93 |
} |
| 94 |
|
| 95 |
bool CEventLinkerQueue::hasEvents(double minEventPriority) { |
| 96 |
CEventChannelLinker* channelLinkerIt = mReverseSortedChannelLinker; |
| 97 |
while (channelLinkerIt) { |
| 98 |
CEventChannel *channel = channelLinkerIt->getData(); |
| 99 |
if (channel->getChannelPriority() >= minEventPriority && channel->hasEvents()) { |
| 100 |
return true; |
| 101 |
} |
| 102 |
channelLinkerIt = channelLinkerIt->getNext(); |
| 103 |
} |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
}; // end namespace Concurrent |
| 112 |
|
| 113 |
}; // end namespace Konclude |
| 114 |
|