Skip to content

Commit 8d2118e

Browse files
committed
Run -> CondRun; Seems to be better
CondRun seems to cope better with signal handling. At least it seems to be stable now on both Linux and Mac (the Run()-based implementation still had problems on Mac).
1 parent 194615b commit 8d2118e

1 file changed

Lines changed: 44 additions & 43 deletions

File tree

run/O2PrimaryServerDevice.h

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <atomic>
4040
#include "PrimaryServerState.h"
4141
#include "SimPublishChannelHelper.h"
42+
#include <chrono>
4243

4344
namespace o2
4445
{
@@ -319,60 +320,60 @@ class O2PrimaryServerDevice final : public FairMQDevice
319320
return true;
320321
}
321322

322-
void Run() override
323+
bool ConditionalRun() override
323324
{
324-
while (mState != O2PrimaryServerState::Stopped) {
325-
// we might come here in IDLE mode
326-
if (mState == O2PrimaryServerState::Idle) {
327-
if (mWaitingControlInput.load() == 0) {
328-
if (mControlThread.joinable()) {
329-
mControlThread.join();
330-
}
331-
mControlThread = std::thread(&O2PrimaryServerDevice::waitForControlInput, this);
325+
// we might come here in IDLE mode
326+
if (mState == O2PrimaryServerState::Idle) {
327+
if (mWaitingControlInput.load() == 0) {
328+
if (mControlThread.joinable()) {
329+
mControlThread.join();
332330
}
331+
mControlThread = std::thread(&O2PrimaryServerDevice::waitForControlInput, this);
333332
}
333+
}
334334

335-
auto& channel = fChannels.at("primary-get").at(0);
336-
PrimaryChunkRequest requestpayload;
337-
std::unique_ptr<FairMQMessage> request(channel.NewSimpleMessage(requestpayload));
338-
auto bytes = channel.Receive(request);
339-
if (bytes < 0) {
340-
LOG(ERROR) << "Some error/interrupt occurred on socket during receive";
341-
// This may likely be due to an external signal which disrupts the polling
342-
LOG(INFO) << "Get current state : " << GetCurrentStateName();
343-
LOG(INFO) << "New state pending : " << NewStatePending();
344-
if (NewStatePending()) { // new state is typically pending if (term) signal was received
345-
WaitForNextState();
346-
// ask ourselves for termination of this loop
347-
stateTransition(O2PrimaryServerState::Stopped, "CONDRUN");
348-
}
349-
continue;
335+
auto& channel = fChannels.at("primary-get").at(0);
336+
PrimaryChunkRequest requestpayload;
337+
std::unique_ptr<FairMQMessage> request(channel.NewSimpleMessage(requestpayload));
338+
auto bytes = channel.Receive(request);
339+
if (bytes < 0) {
340+
LOG(ERROR) << "Some error/interrupt occurred on socket during receive";
341+
if (NewStatePending()) { // new state is typically pending if (term) signal was received
342+
WaitForNextState();
343+
// ask ourselves for termination of this loop
344+
stateTransition(O2PrimaryServerState::Stopped, "CONDRUN");
350345
}
346+
return false;
347+
}
351348

352-
TStopwatch timer;
353-
timer.Start();
354-
auto& r = *((PrimaryChunkRequest*)(request->GetData()));
355-
LOG(INFO) << "PARTICLE REQUEST IN STATE " << PrimStateToString[(int)mState.load()] << " from " << r.workerid << ":" << r.requestid;
356-
357-
auto prestate = mState.load();
358-
auto more = HandleRequest(request, 0, channel);
359-
if (!more) {
360-
if (mAsService) {
361-
if (prestate == O2PrimaryServerState::ReadyToServe || prestate == O2PrimaryServerState::WaitingEvent) {
362-
stateTransition(O2PrimaryServerState::Idle, "CONDRUN");
363-
}
364-
} else {
365-
stateTransition(O2PrimaryServerState::Stopped, "CONDRUN");
349+
TStopwatch timer;
350+
timer.Start();
351+
auto& r = *((PrimaryChunkRequest*)(request->GetData()));
352+
LOG(INFO) << "PARTICLE REQUEST IN STATE " << PrimStateToString[(int)mState.load()] << " from " << r.workerid << ":" << r.requestid;
353+
354+
auto prestate = mState.load();
355+
auto more = HandleRequest(request, 0, channel);
356+
if (!more) {
357+
if (mAsService) {
358+
if (prestate == O2PrimaryServerState::ReadyToServe || prestate == O2PrimaryServerState::WaitingEvent) {
359+
stateTransition(O2PrimaryServerState::Idle, "CONDRUN");
366360
}
361+
} else {
362+
stateTransition(O2PrimaryServerState::Stopped, "CONDRUN");
367363
}
368-
timer.Stop();
369-
auto time = timer.CpuTime();
370-
LOG(INFO) << "COND-RUN TOOK " << time << " s";
371364
}
372-
// wait for info thread
365+
timer.Stop();
366+
auto time = timer.CpuTime();
367+
LOG(INFO) << "COND-RUN TOOK " << time << " s";
368+
return mState != O2PrimaryServerState::Stopped;
369+
}
370+
371+
void PostRun() override
372+
{
373373
while (!mInfoThreadStopped) {
374374
LOG(INFO) << "Waiting info thread";
375-
sleep(1);
375+
using namespace std::chrono_literals;
376+
std::this_thread::sleep_for(100ms);
376377
}
377378
}
378379

0 commit comments

Comments
 (0)