Esempio n. 1
0
    def test_get_instance_with_concurrent_mode(self, mr_rlock: RLockFactory):
        try:
            _rlock = mr_rlock.get_instance()
        except ValueError as ve:
            assert "FeatureMode is None. Please configure it as one of 'multirunnable.mode.FeatureMode'." in str(
                ve), "It should set the FeatureMode first."

        mr_rlock.feature_mode = FeatureMode.Concurrent
        _rlock = mr_rlock.get_instance()
        from threading import RLock
        assert _rlock is not None and isinstance(_rlock, type(RLock(
        ))) is True, "This type of RLock instance should be 'threading.RLock'."
Esempio n. 2
0
def RLock():
    """
    Returns a recursive lock object
    """
    from multiprocessing.synchronize import RLock
    return RLock()
Esempio n. 3
0
 def __init__(self):
     self._i = itertools.count(0)
     self._items = {}
     self._destroy = False
     self.__lock = RLock()
Esempio n. 4
0
class Synchroniser(object):
    """ 
    @summary: Makes inter-thread comms synchronous.
    """

    class Item(object):
        def __init__(self, i):
            self._i = i
            self._sem = Semaphore(0)
            self._result = []

        def acquire(self, timeout=None):
            return self._sem.acquire(block=True, timeout=timeout)

        def result(self, result):
            self._result.append(result)
            #    Now the result is complete, release it:
            self._sem.release()

        def getResult(self):
            return self._result.pop(0)

    def __init__(self):
        self._i = itertools.count(0)
        self._items = {}
        self._destroy = False
        self.__lock = RLock()

    def _lock(self):
        self.__lock.acquire()

    def _unlock(self):
        self.__lock.release()

    def isValidTransactionId(self, i):
        if i is None:
            return False
        with self.__lock:
            return i in self._items.keys()

    def create(self, enabler=True):
        if not enabler:
            return
        with self.__lock:
            self.checkDestroyed()
            i = self._i.next()
            self._items[i] = self._getItem(i)
            return i

    def _getItem(self, i):
        return Synchroniser.Item(i)

    def acquire(self, i, timeout=None, purge=False):
        with self.__lock:
            self.checkDestroyed()
            try:
                item = self._items[i]
            except KeyError, _e:
                sys.stderr.write("Acquire on a non-existent synchroniser: %(I)s\n" % {"I": i})
                raise
        result = item.acquire(timeout=timeout)
        self.checkDestroyed()
        if not result:
            purge and self.purge(i)
            raise TransactionFailed(i)
        result = item.getResult()
        if purge:
            self.purge(i)
        return result
def RLock():
    from multiprocessing.synchronize import RLock
    return RLock()
Esempio n. 6
0
class Logger(object):
    '''
    Receives information from all the components, and saves to a file.
    
    @cvar __instance: the instance to make possible the Singleton.
    @ivar lock: the lock to keep synchronized the access to the file.
    '''

    __instance = None

    @classmethod
    def __new__(cls, *args, **kwargs):
        '''
        Redefines the constructor to make sure that you can't create 
        more than one instance.
        '''
        if cls.__instance is None:
            cls.__instance = object.__new__(cls)
        return cls.__instance

    @classmethod
    def get_instance(cls):
        return cls.__instance

    def __init__(self):
        '''Constructor of Logger.'''
        self.lock = RLock()

    def open_log_file(self):
        self._log_file = open(LOG_PATH, 'a')
        self.write('The logger start its activity.')

    def get_log(self):
        '''Getter of _log_file.'''
        return self._log_file

    def close_log_file(self):
        self.write('The logger has finished its activity.')
        self._log_file.close()

    def get_timestamp(self):
        '''Get the current timestamp in a pretty format.'''
        timestmp = datetime.now()
        res = timestmp.strftime('%Y-%m-%d %H:%M:%S')
        return '[' + res + '] '

    def write(self, msg):
        '''Put into the log the message received.'''
        self.lock.acquire()
        self.get_log().write(self.get_timestamp() + msg + '\n')
        self.lock.release()

    def clear_log_file(self):
        pass

# file system logging

    def log_del(self, name, fileordir):
        self.write('FM - ' + fileordir + ' ' + name +
                   ' was removed from the system')

    def log_create(self, name, path, dirorfile):
        res = 'FM - ' + dirorfile + ' ' + name \
            + ' was created in the directory ' + path
        self.write(res)

    def log_rename(self, old, new, dirorfile):
        self.write('FM - ' + dirorfile + ' ' + old + ' was renamed to ' + new)

    def log_datasaved(self, file_name):
        self.write('FM - Data saved to file ' + file_name)

    def log_openfile(self, name, pos):
        res = 'FM - file ' + name + ' was opened in the address #' + str(pos)
        self.write(res)

    def log_closefile(self, name):
        self.write('FM - file ' + name + ' was closed')

# CPU logging

    def log_request(self, pid, req, msg):
        res = 'CPU - request ' + str(req) + ' of process ' \
                               + str(pid) + ' was ' + msg
        self.write(res)

    def log_free(self, pcb):
        self.write('CPU - process ' + str(pcb.get_pid()) +
                   ' free its resources')

    def log_expropiate(self, pid):
        self.write('CPU - process ' + str(pid) + ' was expropiated')

    def log_execinst(self, pcb):
        res = 'CPU - process ' + str(pcb.get_pid()) \
            + ' executes its instruction #' + str(pcb.get_pc())
        self.write(res)

# I/O logging

# other logging

    def log_newprocess(self, proc):
        res = 'OS - process ' + str(proc.get_pid()) + '(' \
            + proc.get_name() + ') enter to the system'
        self.write(res)

    def log_killprocess(self, pcb):
        res = 'OS - process ' + str(pcb.get_pid()) + '(' \
            + pcb.get_name() + ') finished or was killed.'
        self.write(res)
        print('process ' + str(pcb.get_pid()) + ' finish')
Esempio n. 7
0
 def __init__(self):
     '''Constructor of Logger.'''
     self.lock = RLock()