Skip to content

Commit e9a662c

Browse files
Adding force stopping mechanism for always online services
1 parent c794e1e commit e9a662c

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

include/taskr/runtime.hpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ class Runtime
344344
*/
345345
__INLINE__ void run()
346346
{
347+
// Clear force terminate flag
348+
_forceTerminate = false;
349+
347350
// Verify taskr is correctly initialized and not running
348351
if (_state == state_t::uninitialized) HICR_THROW_LOGIC("Trying to run TaskR, but it was not initialized");
349352
if (_state == state_t::running) HICR_THROW_LOGIC("Trying to run TaskR, but it is currently running");
@@ -434,6 +437,15 @@ class Runtime
434437
*/
435438
__INLINE__ void addService(taskr::service_t *service) { _serviceQueue->push(service); }
436439

440+
441+
/**
442+
* Funtion to force termination in case the application does not have its own termination logic
443+
*/
444+
__INLINE__ void forceTermination()
445+
{
446+
_forceTerminate = true;
447+
}
448+
437449
private:
438450

439451
__INLINE__ taskr::Task *serviceWorkerLoop(const workerId_t serviceWorkerId)
@@ -447,7 +459,7 @@ class Runtime
447459

448460
// Getting worker pointer
449461
auto worker = _serviceWorkers[serviceWorkerId];
450-
462+
451463
// Checking for termination
452464
auto terminated = checkTermination(worker.get());
453465

@@ -493,6 +505,22 @@ class Runtime
493505
// Getting worker pointer
494506
auto worker = _taskWorkers[taskWorkerId];
495507

508+
// If force termination is set, clearing all tasks from the general and worker's queue
509+
if (_forceTerminate == true)
510+
{
511+
while (_commonReadyTaskQueue->wasEmpty() == false)
512+
{
513+
auto task = _commonReadyTaskQueue->pop();
514+
if (task != nullptr) _activeTaskCount--;
515+
}
516+
517+
while (worker->getReadyTaskQueue()->wasEmpty() == false)
518+
{
519+
auto task = worker->getReadyTaskQueue()->pop();
520+
if (task != nullptr) _activeTaskCount--;
521+
}
522+
}
523+
496524
// If required, perform a service task
497525
if (_makeTaskWorkersRunServices == true)
498526
{
@@ -723,6 +751,14 @@ class Runtime
723751
this->_workerCallbackMap.trigger(taskrWorker, HiCR::tasking::Worker::callback_t::onWorkerTerminate);
724752
}
725753

754+
755+
756+
/**
757+
* Flag to indicate whether execution must be forcibly terminated. It is discouraged to use this if the application
758+
* has implemented a clear ending logic. This is only useful for always-on services-like applications
759+
*/
760+
bool _forceTerminate;
761+
726762
/**
727763
* A flag to indicate whether taskR was initialized
728764
*/

0 commit comments

Comments
 (0)