Acquire a write lock for the current thread, waiting at most timeout seconds or doing a non-blocking check in case timeout is <= 0. In case the write lock cannot be serviced due to the deadlock condition mentioned above, a ValueError is raised. In case timeout is No
(self, timeout=None)
| 108 | self.__condition.release() |
| 109 | |
| 110 | def acquireWrite(self, timeout=None): |
| 111 | """Acquire a write lock for the current thread, waiting at most |
| 112 | timeout seconds or doing a non-blocking check in case timeout is <= 0. |
| 113 | |
| 114 | In case the write lock cannot be serviced due to the deadlock |
| 115 | condition mentioned above, a ValueError is raised. |
| 116 | |
| 117 | In case timeout is None, the call to acquireWrite blocks until the |
| 118 | lock request can be serviced. |
| 119 | |
| 120 | In case the timeout expires before the lock could be serviced, a |
| 121 | RuntimeError is thrown.""" |
| 122 | |
| 123 | if timeout is not None: |
| 124 | endtime = time() + timeout |
| 125 | me, upgradewriter = currentThread(), False |
| 126 | self.__condition.acquire() |
| 127 | try: |
| 128 | if self.__writer is me: |
| 129 | # If we are the writer, grant a new write lock, always. |
| 130 | self.__writercount += 1 |
| 131 | return |
| 132 | elif me in self.__readers: |
| 133 | # If we are a reader, no need to add us to pendingwriters, |
| 134 | # we get the upgradewriter slot. |
| 135 | if self.__upgradewritercount: |
| 136 | # If we are a reader and want to upgrade, and someone |
| 137 | # else also wants to upgrade, there is no way we can do |
| 138 | # this except if one of us releases all his read locks. |
| 139 | # Signal this to user. |
| 140 | raise ValueError( |
| 141 | "Inevitable dead lock, denying write lock" |
| 142 | ) |
| 143 | upgradewriter = True |
| 144 | self.__upgradewritercount = self.__readers.pop(me) |
| 145 | else: |
| 146 | # We aren't a reader, so add us to the pending writers queue |
| 147 | # for synchronization with the readers. |
| 148 | self.__pendingwriters.append(me) |
| 149 | while True: |
| 150 | if not self.__readers and self.__writer is None: |
| 151 | # Only test anything if there are no readers and writers. |
| 152 | if self.__upgradewritercount: |
| 153 | if upgradewriter: |
| 154 | # There is a writer to upgrade, and it's us. Take |
| 155 | # the write lock. |
| 156 | self.__writer = me |
| 157 | self.__writercount = self.__upgradewritercount + 1 |
| 158 | self.__upgradewritercount = 0 |
| 159 | return |
| 160 | # There is a writer to upgrade, but it's not us. |
| 161 | # Always leave the upgrade writer the advance slot, |
| 162 | # because he presumes he'll get a write lock directly |
| 163 | # from a previously held read lock. |
| 164 | elif self.__pendingwriters[0] is me: |
| 165 | # If there are no readers and writers, it's always |
| 166 | # fine for us to take the writer slot, removing us |
| 167 | # from the pending writers queue. |