def acquire_write(self, timeout=None):
if timeout is not None:<EOL><INDENT>endtime = time() + timeout<EOL><DEDENT>me, upgradewriter = currentThread(), False<EOL>self.__condition.acquire()<EOL>try:<EOL><INDENT>if self.__writer is me:<EOL><INDENT>self.__writercount += <NUM_LIT:1><EOL>return<EOL><DEDENT>elif me in self.__readers:<EOL><INDENT>if self.__upgradewritercount:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>upgradewriter = True<EOL>self.__upgradewritercount = self.__readers.pop(me)<EOL><DEDENT>else:<EOL><INDENT>self.__pendingwriters.append(me)<EOL><DEDENT>while True:<EOL><INDENT>if not self.__readers and self.__writer is None:<EOL><INDENT>if self.__upgradewritercount:<EOL><INDENT>if upgradewriter:<EOL><INDENT>self.__writer = me<EOL>self.__writercount = self.__upgradewritercount + <NUM_LIT:1><EOL>self.__upgradewritercount = <NUM_LIT:0><EOL>return<EOL><DEDENT><DEDENT>elif self.__pendingwriters[<NUM_LIT:0>] is me:<EOL><INDENT>self.__writer = me<EOL>self.__writercount = <NUM_LIT:1><EOL>self.__pendingwriters = self.__pendingwriters[<NUM_LIT:1>:]<EOL>return<EOL><DEDENT><DEDENT>if timeout is not None:<EOL><INDENT>remaining = endtime - time()<EOL>if remaining <= <NUM_LIT:0>:<EOL><INDENT>if upgradewriter:<EOL><INDENT>self.__readers[me] = self.__upgradewritercount<EOL>self.__upgradewritercount = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>self.__pendingwriters.remove(me)<EOL><DEDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>self.__condition.wait(remaining)<EOL><DEDENT>else:<EOL><INDENT>self.__condition.wait()<EOL><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>self.__condition.release()<EOL><DEDENT>
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 None, the call to acquire_write blocks until the
        lock request can be serviced.

        In case the timeout expires before the lock could be serviced, a
        RuntimeError is thrown.