Commit 1b047c45 authored by Alexey Borzenkov's avatar Alexey Borzenkov

Get rid of vfd_init(), alloc CRITICAL_SECTION dynamically instead

parent 5af13bbe
...@@ -4,9 +4,6 @@ cimport libev ...@@ -4,9 +4,6 @@ cimport libev
from python cimport * from python cimport *
libev.vfd_init()
__all__ = ['get_version', __all__ = ['get_version',
'get_header_version', 'get_header_version',
'supported_backends', 'supported_backends',
......
cdef extern from "libev_vfd.h": cdef extern from "libev_vfd.h":
void vfd_init()
long vfd_get(int) long vfd_get(int)
int vfd_open(long) except -1 int vfd_open(long) except -1
void vfd_free(int) void vfd_free(int)
......
...@@ -26,15 +26,27 @@ static PyObject* vfd_map = NULL; /* map OS handle -> virtual fd */ ...@@ -26,15 +26,27 @@ static PyObject* vfd_map = NULL; /* map OS handle -> virtual fd */
static vfd_entry* vfd_entries = NULL; /* list of virtual fd entries */ static vfd_entry* vfd_entries = NULL; /* list of virtual fd entries */
#ifdef WITH_THREAD #ifdef WITH_THREAD
static CRITICAL_SECTION vfd_lock; static CRITICAL_SECTION* volatile vfd_lock = NULL;
#define VFD_LOCK_INIT InitializeCriticalSection(&vfd_lock) static CRITICAL_SECTION* vfd_make_lock()
#define VFD_LOCK_ENTER EnterCriticalSection(&vfd_lock) {
#define VFD_LOCK_LEAVE LeaveCriticalSection(&vfd_lock) if (vfd_lock == NULL) {
/* must use malloc and not PyMem_Malloc here */
CRITICAL_SECTION* lock = malloc(sizeof(CRITICAL_SECTION));
InitializeCriticalSection(lock);
if (InterlockedCompareExchangePointer(&vfd_lock, lock, NULL) != NULL) {
/* another thread initialized lock first */
DeleteCriticalSection(lock);
free(lock);
}
}
return vfd_lock;
}
#define VFD_LOCK_ENTER EnterCriticalSection(vfd_make_lock())
#define VFD_LOCK_LEAVE LeaveCriticalSection(vfd_lock)
#define VFD_GIL_DECLARE PyGILState_STATE ___save #define VFD_GIL_DECLARE PyGILState_STATE ___save
#define VFD_GIL_ENSURE ___save = PyGILState_Ensure() #define VFD_GIL_ENSURE ___save = PyGILState_Ensure()
#define VFD_GIL_RELEASE PyGILState_Release(___save) #define VFD_GIL_RELEASE PyGILState_Release(___save)
#else #else
#define VFD_LOCK_INIT
#define VFD_LOCK_ENTER #define VFD_LOCK_ENTER
#define VFD_LOCK_LEAVE #define VFD_LOCK_LEAVE
#define VFD_GIL_DECLARE #define VFD_GIL_DECLARE
...@@ -42,11 +54,6 @@ static CRITICAL_SECTION vfd_lock; ...@@ -42,11 +54,6 @@ static CRITICAL_SECTION vfd_lock;
#define VFD_GIL_RELEASE #define VFD_GIL_RELEASE
#endif #endif
static void vfd_init()
{
VFD_LOCK_INIT;
}
/* /*
* Given a virtual fd returns an OS handle or -1 * Given a virtual fd returns an OS handle or -1
* This function is speed critical, so it cannot use GIL * This function is speed critical, so it cannot use GIL
...@@ -188,7 +195,6 @@ done: ...@@ -188,7 +195,6 @@ done:
* using runtime fds in libev. Note that it will leak * using runtime fds in libev. Note that it will leak
* fds, because there's no way of closing them safely * fds, because there's no way of closing them safely
*/ */
#define vfd_init() do {} while(0)
#define vfd_get(fd) _get_osfhandle((fd)) #define vfd_get(fd) _get_osfhandle((fd))
#define vfd_open(fd) _open_osfhandle((fd), 0) #define vfd_open(fd) _open_osfhandle((fd), 0)
#define vfd_free(fd) #define vfd_free(fd)
...@@ -197,7 +203,6 @@ done: ...@@ -197,7 +203,6 @@ done:
/* /*
* On non-win32 platforms vfd_* are noop macros * On non-win32 platforms vfd_* are noop macros
*/ */
#define vfd_init() do {} while(0)
#define vfd_get(fd) (fd) #define vfd_get(fd) (fd)
#define vfd_open(fd) ((int)(fd)) #define vfd_open(fd) ((int)(fd))
#define vfd_free(fd) #define vfd_free(fd)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment