Acquire a read lock for the current thread, waiting at most timeout seconds or doing a non-blocking check in case timeout is <= 0. In case timeout is None, the call to acquireRead blocks until the lock request can be serviced. In case the timeout expires before the
(self, timeout=None)
| 61 | self.__readers = {} |
| 62 | |
| 63 | def acquireRead(self, timeout=None): |
| 64 | """Acquire a read lock for the current thread, waiting at most |
| 65 | timeout seconds or doing a non-blocking check in case timeout is <= 0. |
| 66 | |
| 67 | In case timeout is None, the call to acquireRead blocks until the |
| 68 | lock request can be serviced. |
| 69 | |
| 70 | In case the timeout expires before the lock could be serviced, a |
| 71 | RuntimeError is thrown.""" |
| 72 | |
| 73 | if timeout is not None: |
| 74 | endtime = time() + timeout |
| 75 | me = currentThread() |
| 76 | self.__condition.acquire() |
| 77 | try: |
| 78 | if self.__writer is me: |
| 79 | # If we are the writer, grant a new read lock, always. |
| 80 | self.__writercount += 1 |
| 81 | return |
| 82 | while True: |
| 83 | if self.__writer is None: |
| 84 | # Only test anything if there is no current writer. |
| 85 | if self.__upgradewritercount or self.__pendingwriters: |
| 86 | if me in self.__readers: |
| 87 | # Only grant a read lock if we already have one |
| 88 | # in case writers are waiting for their turn. |
| 89 | # This means that writers can't easily get starved |
| 90 | # (but see below, readers can). |
| 91 | self.__readers[me] += 1 |
| 92 | return |
| 93 | # No, we aren't a reader (yet), wait for our turn. |
| 94 | else: |
| 95 | # Grant a new read lock, always, in case there are |
| 96 | # no pending writers (and no writer). |
| 97 | self.__readers[me] = self.__readers.get(me, 0) + 1 |
| 98 | return |
| 99 | if timeout is not None: |
| 100 | remaining = endtime - time() |
| 101 | if remaining <= 0: |
| 102 | # Timeout has expired, signal caller of this. |
| 103 | raise RuntimeError("Acquiring read lock timed out") |
| 104 | self.__condition.wait(remaining) |
| 105 | else: |
| 106 | self.__condition.wait() |
| 107 | finally: |
| 108 | self.__condition.release() |
| 109 | |
| 110 | def acquireWrite(self, timeout=None): |
| 111 | """Acquire a write lock for the current thread, waiting at most |
no test coverage detected