<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body bgcolor="#ffffff" text="#000000">
<tt><br>
Thread synchronization with threading.Lock can be expensive. But
consider this: Why should the active thread need to aquire a mutex,
when it already holds one? That would be the GIL. <br>
<br>
Instead of acqiring a lock (and possibly inducing thread swiching
etc.), it could just deny the other threads access to the GIL for a
while. The cost of that synchronization method would be completely
amortized, as check intervals happen anyway.<br>
<br>
Here is what a very naïve implementation would look like in ctypes
(real code could use C instead, or perhaps should not attempt this at
all...): <br>
<br>
<span class="Apple-style-span"
 style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span
 class="Apple-style-span"
 style="font-family: fixed-width,monospace; font-size: 12px;">from
contextlib import contextmanager<br>
import ctypes<br>
_Py_Ticker = ctypes.c_int.in_dll(ctypes.pythonapi,"_Py_Ticker")<br>
<br>
@contextmanager<br>
def threadsafe():<br>
    tmp = _Py_Ticker.value<br>
    _Py_Ticker.value = 0x7fffffff<br>
    yield<br>
    _Py_Ticker.value = tmp<br>
<br>
<br>
Now we can do this:<br>
<br>
with </span></span><span class="Apple-style-span"
 style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span
 class="Apple-style-span"
 style="font-family: fixed-width,monospace; font-size: 12px;">threadsafe</span></span><span
 class="Apple-style-span"
 style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span
 class="Apple-style-span"
 style="font-family: fixed-width,monospace; font-size: 12px;">():<br>
    # the GIL is mine,<br>
    # for as long as I want<br>
    pass<br>
<br>
The usecase for this "gillock" is about the same as for a spinlock C.
We want synchronization for a breif period of time, but don't want the
overhead of aquiring a mutex. <br>
<br>
In Python this gillock has one big advantage over a spinlock: We don't
have to wait, so we don't risk a tread switch on __enter__/aquire. But
there can be only one instance of this lock, as there is only one GIL.
That is the drawback compared to a spinlock.<br>
<br>
Therefore I think both a spinlock and a gillock should be added to the
threading module. These are synchronization methods that should be
available.<br>
<br>
P.S. A gillock working like the ctypes code above is of course very
dangerous. If a C extension releases the GIL while </span></span></tt><tt><span
 class="Apple-style-span"
 style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span
 class="Apple-style-span"
 style="font-family: fixed-width,monospace; font-size: 12px;">_Py_Ticker
is astronomic, we have a very bad situation... But real code could try
to safeguard against this.</span></span> E.g. Py_BEGIN_ALLOW_THREADS
and <br>
Py_END_ALLOW_THREADS could be defined to do nothing if _Py_Ticker is
above some ridiculous threshold.<br>
</tt><tt><span class="Apple-style-span"
 style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span
 class="Apple-style-span"
 style="font-family: fixed-width,monospace; font-size: 12px;"><br>
P.P.S. Yes I know about the newgil. But I have not thought about how to
achieve similar effect with that.<br>
 <br>
<br>
<br>
Sturla<br>
<br>
<br>
<br>
<br>
<br>
<br>
</span></span></tt>
</body>
</html>