Skip to content

Instantly share code, notes, and snippets.

@blackwithwhite666
Created December 23, 2013 08:16
Show Gist options
  • Select an option

  • Save blackwithwhite666/8093366 to your computer and use it in GitHub Desktop.

Select an option

Save blackwithwhite666/8093366 to your computer and use it in GitHub Desktop.
Usage of thread-local static variable for slab in pyuv
diff --git a/setup.py b/setup.py
index 67ddc8f..4a957e0 100644
--- a/setup.py
+++ b/setup.py
@@ -37,6 +37,7 @@ setup(name = "pyuv",
'sdist' : libuv_sdist},
ext_modules = [Extension('pyuv',
sources = ['src/pyuv.c'],
+ extra_compile_args=['-std=c99'],
define_macros=[('MODULE_VERSION', __version__),
('LIBUV_REVISION', libuv_build_ext.libuv_revision)]
)]
diff --git a/src/pyuv.h b/src/pyuv.h
index 885071f..3d43537 100644
--- a/src/pyuv.h
+++ b/src/pyuv.h
@@ -28,6 +28,14 @@ typedef int Bool;
#define True 1
#define False 0
+/* Standart lib */
+#if defined(__clang__)
+ #define _Thread_local __thread
+#elif defined(__GNUC__) || defined(__GNUG__)
+ #define _Thread_local __thread
+#else
+ #define _Thread_local
+#endif
/* Utility macros */
#define PYUV_STRINGIFY_HELPER(x) #x
diff --git a/src/stream.c b/src/stream.c
index 903a380..08989c8 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -19,7 +19,7 @@ typedef struct {
static void
on_stream_alloc(uv_stream_t* handle, size_t suggested_size, uv_buf_t *buf)
{
- static char slab[PYUV_SLAB_SIZE];
+ static _Thread_local char slab[PYUV_SLAB_SIZE];
UNUSED_ARG(handle);
buf->base = slab;
buf->len = sizeof(slab);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment