# This file is part of Beremiz runtime.
# Copyright (C) 2018: Edouard TISSERANT
# See COPYING.Runtime file for copyrights details.
from __future__ import absolute_import
from threading import Lock, Condition
from six.moves import _thread
job to be executed by a worker
def __init__(self, call, *args, **kwargs):
self.job = (call, args, kwargs)
do the job by executing the call, and deal with exceptions
call, args, kwargs = self.job
self.result = call(*args, **kwargs)
self.exc_info = sys.exc_info()
serialize main thread load/unload of PLC shared objects
self.todo = Condition(self.mutex)
self.done = Condition(self.mutex)
self.free = Condition(self.mutex)
reraise exception happend in a job
@param job: job where original exception happend
exc_type = job.exc_info[0]
exc_value = job.exc_info[1]
exc_traceback = job.exc_info[2]
six.reraise(exc_type, exc_value, exc_traceback)
def runloop(self, *args, **kwargs):
meant to be called by worker thread (blocking)
self._threadID = _thread.get_ident()
_job = job(*args, **kwargs)
def call(self, *args, **kwargs):
creates a job, execute it in worker thread, and deliver result.
if job execution raise exception, re-raise same exception
meant to be called by non-worker threads, but this is accepted.
_job = job(*args, **kwargs)
if self._threadID == _thread.get_ident():
# if caller is worker thread execute immediately
# otherwise notify and wait for completion
while self.job is not None:
unblocks main thread, and terminate execution of runloop()