Exemple #1
0
 def fRelease(oSelf):
     mDebugOutput_HideInCallStack = True
     # Errors are often easier to read if this function is left out of the stack.
     try:
         oSelf.__xLastAcquireCallStackOrThreadId = oSelf.__oQueue.get(
             False, 0)
     except queue.Empty:
         oSelf.__fTerminateWithOverUnlocked(
             c0CallStack.foForThisFunctionsCaller(
             ) if c0CallStack else threading.current_thread().ident)
     if not oSelf.__oQueue.full():
         oSelf.__xFinalReleaseCallStackOrThreadId = c0CallStack.foForThisFunctionsCaller(
         ) if c0CallStack else threading.current_thread().ident
     oSelf.fFireCallbacks("unlocked")
Exemple #2
0
 def __init__(oSelf, fMain, *txArguments, **dxArguments):
     oSelf.__fMain = fMain
     oSelf.__o0CreateCallStack = c0CallStack.foForThisFunctionsCaller(
     ) if c0CallStack else None
     oSelf.__o0StartCallStack = None
     oSelf.__txArguments = txArguments
     oSelf.__dxArguments = dxArguments
     oSelf.__bVital = True
     oSelf.__uId = None
     oSelf.__bStarted = False
     oSelf.__bRunning = False
     oSelf.__bTerminated = False
     oSelf.__oTerminatedLock = cLock("cThread.__oTerminatedLock",
                                     bLocked=True)
     if hasattr(fMain, "im_self"):
         oMainSelf = getattr(fMain, "im_self")
         oSelf.sMain = "%s.%s" % (oMainSelf.__class__.__name__,
                                  fMain.__name__)
         oSelf.sMainId = "%s#%X.%s" % (oMainSelf.__class__.__name__,
                                       id(oMainSelf), fMain.__name__)
     elif hasattr(fMain, "im_class"):
         cMainClass = getattr(fMain, "im_class")
         oSelf.sMain = oSelf.sMainId = "%s.%s" % (cMainClass.__name__,
                                                  fMain.__name__)
     else:
         oSelf.sMain = oSelf.sMainId = fMain.__name__
     oSelf.uIndex = fuCountNewThreadAndReturnIndex()
Exemple #3
0
    def fStart(oSelf, bVital=None):
        o0CurrentCallStack = c0CallStack.foForThisFunctionsCaller(
        ) if c0CallStack else None
        if oSelf.__bStarted:
            aasStackConsoleOutputLines = (
                oSelf.__faxsGetConsoleOutputLinesForStack(
                    "This thread was created in", oSelf.__o0CreateCallStack)
                if oSelf.__o0CreateCallStack else []) + [""] + (
                    oSelf.__faxsGetConsoleOutputLinesForStack(
                        "This thread was started in", oSelf.__o0StartCallStack)
                    if oSelf.__o0StartCallStack else
                    []) + (oSelf.__faxsGetConsoleOutputLinesForStack(
                        "This thread was started again in", o0CurrentCallStack)
                           if o0CurrentCallStack else [])
            goOutputLock.acquire()
            try:
                if f0TerminateWithConsoleOutput:
                    f0TerminateWithConsoleOutput(
                        sTitle="This thread was started twice",
                        aasConsoleOutputLines=aasStackConsoleOutputLines,
                        uExitCode=oSelf.uExitCodeInternalError,
                    )
                print("This thread was started twice:")
                if aasStackConsoleOutputLines:
                    oSelf.__fPrintConsoleOutputLines(
                        aasStackConsoleOutputLines)
                else:
                    print("Traceback (most recent call last):")
                    for sLine in traceback.format_list(
                            traceback.extract_stack()):
                        print(sLine)
            finally:
                goOutputLock.release()
                sys.exit(oSelf.uExitCodeInternalError)

        oSelf.__o0StartCallStack = o0CurrentCallStack
        if bVital is not None:
            oSelf.__bVital = bVital
        oSelf.__bStarted = True
        oSelf.__oPythonThread = threading.Thread(
            target=oSelf.__fMainWrapper,
            name=oSelf.sMainId,
        )
        oSelf.__oPythonThread.daemon = not oSelf.__bVital
        oSelf.__oPythonThread.start()
Exemple #4
0
 def __init__(oSelf,
              s0Description=None,
              uSize=1,
              bLocked=False,
              n0DeadlockTimeoutInSeconds=None):
     oSelf.__sDescription = s0Description or str(id(oSelf))
     oSelf.__uSize = uSize
     oSelf.__oQueue = queue.Queue(uSize)
     oSelf.__oQueuePutLock = queue.Queue(1)
     if n0DeadlockTimeoutInSeconds is not None:
         assert isinstance(n0DeadlockTimeoutInSeconds, (int, float)) and n0DeadlockTimeoutInSeconds >=0, \
             "Invalid timeout value %s" % repr(nTimeoutInSeconds)
     oSelf.__n0DeadlockTimeoutInSeconds = n0DeadlockTimeoutInSeconds
     oSelf.fAddEvents("locked", "unlocked")
     if bLocked:
         oSelf.__xLastAcquireCallStackOrThreadId = c0CallStack.foForThisFunctionsCaller(
         ) if c0CallStack else threading.current_thread().ident
         for u in range(uSize):
             oSelf.__oQueue.put(oSelf.__xLastAcquireCallStackOrThreadId)
     else:
         oSelf.__xLastAcquireCallStackOrThreadId = None
     oSelf.__xFinalReleaseCallStackOrThreadId = None
Exemple #5
0
 def fAcquire(oSelf):
     mDebugOutput_HideInCallStack = True
     # Errors are often easier to read if this function is left out of the stack.
     assert oSelf.__n0DeadlockTimeoutInSeconds is not None, \
         "Cannot acquire a lock without a timeout if no deadlock timeout is provided."
     xCallStackOrThreadId = c0CallStack.foForThisFunctionsCaller(
     ) if c0CallStack else threading.current_thread().ident
     if not oSelf.__fbAcquire(xCallStackOrThreadId,
                              oSelf.__n0DeadlockTimeoutInSeconds):
         xLastAcquireCallStackOrThreadId = oSelf.__xLastAcquireCallStackOrThreadId
         if (xLastAcquireCallStackOrThreadId is not None and
             (xLastAcquireCallStackOrThreadId.u0ThreadId is
              xCallStackOrThreadId.u0ThreadId if c0CallStack else
              xLastAcquireCallStackOrThreadId is xCallStackOrThreadId)):
             oSelf.__fTerminateWithSingleThreadDeadlock(
                 xFirstLockStackOrThreadId=oSelf.
                 __xLastAcquireCallStackOrThreadId,
                 xSecondLockStackOrThreadId=xCallStackOrThreadId,
             )
         oSelf.__fTerminateWithMultiThreadDeadlock(
             xFirstLockStackOrThreadId=oSelf.
             __xLastAcquireCallStackOrThreadId,
             xSecondLockStackOrThreadId=xCallStackOrThreadId,
         )
Exemple #6
0
 def fbAcquire(oSelf, nTimeoutInSeconds=0):
     mDebugOutput_HideInCallStack = True
     # Errors are often easier to read if this function is left out of the stack.
     xCallStackOrThreadId = c0CallStack.foForThisFunctionsCaller(
     ) if c0CallStack else threading.current_thread().ident
     return oSelf.__fbAcquire(xCallStackOrThreadId, nTimeoutInSeconds)