summaryrefslogtreecommitdiff
path: root/Modules/hashlib.h
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2012-10-06 02:23:36 +0200
committerChristian Heimes <christian@cheimes.de>2012-10-06 02:23:36 +0200
commit4a0270d82bfd782c89a8ae2b869102dafb81ffea (patch)
tree5bd4262a7a42e94493aa4a0fdf7cdd32185f3a44 /Modules/hashlib.h
parent8c6db45d3e8a20cabe50f93b2bbc33b0040af5a3 (diff)
downloadcpython-git-4a0270d82bfd782c89a8ae2b869102dafb81ffea.tar.gz
Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Diffstat (limited to 'Modules/hashlib.h')
-rw-r--r--Modules/hashlib.h33
1 files changed, 33 insertions, 0 deletions
diff --git a/Modules/hashlib.h b/Modules/hashlib.h
index db39cea7aa..7cb6ee5eae 100644
--- a/Modules/hashlib.h
+++ b/Modules/hashlib.h
@@ -26,3 +26,36 @@
return NULL; \
} \
} while(0);
+
+/*
+ * Helper code to synchronize access to the hash object when the GIL is
+ * released around a CPU consuming hashlib operation. All code paths that
+ * access a mutable part of obj must be enclosed in a ENTER_HASHLIB /
+ * LEAVE_HASHLIB block or explicitly acquire and release the lock inside
+ * a PY_BEGIN / END_ALLOW_THREADS block if they wish to release the GIL for
+ * an operation.
+ */
+
+#ifdef WITH_THREAD
+#include "pythread.h"
+ #define ENTER_HASHLIB(obj) \
+ if ((obj)->lock) { \
+ if (!PyThread_acquire_lock((obj)->lock, 0)) { \
+ Py_BEGIN_ALLOW_THREADS \
+ PyThread_acquire_lock((obj)->lock, 1); \
+ Py_END_ALLOW_THREADS \
+ } \
+ }
+ #define LEAVE_HASHLIB(obj) \
+ if ((obj)->lock) { \
+ PyThread_release_lock((obj)->lock); \
+ }
+#else
+ #define ENTER_HASHLIB(obj)
+ #define LEAVE_HASHLIB(obj)
+#endif
+
+/* TODO(gps): We should probably make this a module or EVPobject attribute
+ * to allow the user to optimize based on the platform they're using. */
+#define HASHLIB_GIL_MINSIZE 2048
+