-
Notifications
You must be signed in to change notification settings - Fork 911
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- publish-test-07
- publish-test-06
- publish-test-05
- publish-test-04
- publish-test-03
- publish-test-02
- publish-test-01
- pub
- cluster-single-node-alpha
- 1.47.3
- 1.47.2
- 1.47.1
- 1.47.0
- 1.46.8
- 1.46.7
- 1.46.6
- 1.46.5
- 1.46.4
- 1.46.3
- 1.46.2
- 1.46.1
- 1.46.0
- 1.45.1
- 1.45.0
- 1.44.6
- 1.44.5
- 1.44.4
- 1.44.3
- 1.44.2
- 1.44.1
- 1.44.0
- 1.43.0
- 1.42.1
- 1.42.0
- 1.41.7
- 1.41.6
- 1.41.5
- 1.41.4
- 1.41.3
- 1.41.2
- 1.41.1
- 1.41.0
- 1.40.0
- 1.39.1
- 1.39.0
- 1.38.2
- 1.38.1
- 1.38.0
- 1.37.0
- 1.36.0
- 1.35.1
- 1.35.0
- 1.34.0
- 1.33.1
- 1.33.0
- 1.32.0
- 1.31.2
- 1.31.1
- 1.31.0
- 1.30.0
- 1.29.0
- 1.28.2
- 1.28.1
- 1.28.0
- 1.27.0
- 1.26.0
- 1.25.1
- 1.25.0
- 1.24.0
- 1.23.1
- 1.23.0
- 1.22.1
- 1.22.0
- 1.21.2
- 1.21.1
- 1.21.0
- 1.20.0
- 1.19.1
- 1.19.0
- 1.18.0
- 1.17.0
- 1.16.0
- 1.15.3
- 1.15.2
- 1.15.1
- 1.15.0
- 1.14.0
- 1.13.0
- 1.12.0
- 1.11.3
- 1.11.2
- 1.11.1
- 1.11.0
- 1.10.5
- 1.10.4
- 1.10.3
- 1.10.2
- 1.10.1
- 1.10.0
- 1.9.3
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.0
- 1.6.0
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.1
- 1.4.0
1 parent
8b309e4
commit 33c1033
Showing
5 changed files
with
197 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Copyright 2014-2017 Real Logic Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#ifndef AERON_AGENTINVOKER_H | ||
#define AERON_AGENTINVOKER_H | ||
|
||
#include <util/Exceptions.h> | ||
#include <functional> | ||
#include <thread> | ||
#include <atomic> | ||
#include <concurrent/logbuffer/TermReader.h> | ||
|
||
namespace aeron { | ||
|
||
namespace concurrent { | ||
|
||
template<typename Agent> | ||
class AgentInvoker | ||
{ | ||
public: | ||
AgentInvoker(Agent& agent, logbuffer::exception_handler_t& exceptionHandler) : | ||
m_agent(agent), | ||
m_exceptionHandler(exceptionHandler), | ||
m_isStarted(false), | ||
m_isRunning(false), | ||
m_isClosed(false) | ||
{ | ||
} | ||
|
||
/** | ||
* Has the Agent been started? | ||
* | ||
* @return has the Agent been started? | ||
*/ | ||
inline bool isStarted() | ||
{ | ||
return m_isStarted; | ||
} | ||
|
||
/** | ||
* Is the Agent running? | ||
* | ||
* @return is the Agent been started successfully and not closed? | ||
*/ | ||
inline bool isRunning() | ||
{ | ||
return m_isRunning; | ||
} | ||
|
||
/** | ||
* Has the Agent been closed? | ||
* | ||
* @return has the Agent been closed? | ||
*/ | ||
inline bool isClosed() | ||
{ | ||
return m_isClosed; | ||
} | ||
|
||
/** | ||
* Mark the invoker as started and call the Agent::onStart() method. | ||
* <p> | ||
* Startup logic will only be performed once. | ||
*/ | ||
inline void start() | ||
{ | ||
try | ||
{ | ||
if (!m_isStarted) | ||
{ | ||
m_isStarted = true; | ||
m_agent.onStart(); | ||
m_isRunning = true; | ||
} | ||
} | ||
catch (const util::SourcedException &exception) | ||
{ | ||
m_exceptionHandler(exception); | ||
close(); | ||
} | ||
} | ||
|
||
/** | ||
* Invoke the Agent::doWork() method and return the work count. | ||
* | ||
* If not successfully started or after closed then this method will return without invoking the {@link Agent}. | ||
* | ||
* @return the work count for the Agent::doWork() method. | ||
*/ | ||
inline int invoke() | ||
{ | ||
int workCount = 0; | ||
|
||
if (m_isRunning) | ||
{ | ||
try | ||
{ | ||
workCount = m_agent.doWork(); | ||
} | ||
catch (const util::SourcedException &exception) | ||
{ | ||
m_exceptionHandler(exception); | ||
} | ||
} | ||
|
||
return workCount; | ||
} | ||
|
||
/** | ||
* Mark the invoker as closed and call the Agent::onClose() logic for clean up. | ||
* | ||
* The clean up logic will only be performed once. | ||
*/ | ||
inline void close() | ||
{ | ||
try | ||
{ | ||
if (!m_isClosed) | ||
{ | ||
m_isRunning = false; | ||
m_isClosed = true; | ||
m_agent.onClose(); | ||
} | ||
} | ||
catch (const util::SourcedException &exception) | ||
{ | ||
m_exceptionHandler(exception); | ||
} | ||
} | ||
|
||
private: | ||
Agent& m_agent; | ||
logbuffer::exception_handler_t& m_exceptionHandler; | ||
bool m_isStarted; | ||
bool m_isRunning; | ||
bool m_isClosed; | ||
}; | ||
|
||
}} | ||
|
||
#endif |