|
3 | 3 | #include "pycore_lock.h" // PyMutex |
4 | 4 |
|
5 | 5 | /* |
6 | | - * Given a PyObject* obj, fill in the Py_buffer* viewp with the result |
7 | | - * of PyObject_GetBuffer. Sets an exception and issues the erraction |
8 | | - * on any errors, e.g. 'return NULL' or 'goto error'. |
| 6 | + * Given a PyObject* 'obj', fill in the Py_buffer* 'viewp' with the result |
| 7 | + * of PyObject_GetBuffer. Sets an exception and issues the 'erraction' |
| 8 | + * on any errors, e.g., 'return NULL' or 'goto error'. |
9 | 9 | */ |
10 | 10 | #define GET_BUFFER_VIEW_OR_ERROR(obj, viewp, erraction) do { \ |
11 | 11 | if (PyUnicode_Check((obj))) { \ |
|
34 | 34 |
|
35 | 35 | /* |
36 | 36 | * Helper code to synchronize access to the hash object when the GIL is |
37 | | - * released around a CPU consuming hashlib operation. All code paths that |
38 | | - * access a mutable part of obj must be enclosed in an ENTER_HASHLIB / |
39 | | - * LEAVE_HASHLIB block or explicitly acquire and release the lock inside |
40 | | - * a PY_BEGIN / END_ALLOW_THREADS block if they wish to release the GIL for |
41 | | - * an operation. |
| 37 | + * released around a CPU consuming hashlib operation. |
42 | 38 | * |
43 | | - * These only drop the GIL if the lock acquisition itself is likely to |
44 | | - * block. Thus the non-blocking acquire gating the GIL release for a |
45 | | - * blocking lock acquisition. The intent of these macros is to surround |
46 | | - * the assumed always "fast" operations that you aren't releasing the |
47 | | - * GIL around. Otherwise use code similar to what you see in hash |
48 | | - * function update() methods. |
| 39 | + * Code accessing a mutable part of the hash object must be enclosed in |
| 40 | + * an HASHLIB_{ACQUIRE,RELEASE}_LOCK block or explicitly acquire and release |
| 41 | + * the mutex inside a Py_BEGIN_ALLOW_THREADS -- Py_END_ALLOW_THREADS block if |
| 42 | + * they wish to release the GIL for an operation. |
49 | 43 | */ |
50 | 44 |
|
51 | | -#include "pythread.h" |
52 | | - |
53 | | -#define PyObject_HASHLIB_HEAD \ |
| 45 | +#define HASHLIB_OBJECT_HEAD \ |
54 | 46 | PyObject_HEAD \ |
55 | 47 | /* Guard against race conditions during incremental update(). */ \ |
56 | 48 | PyMutex mutex; |
57 | 49 |
|
58 | | -#define HASHLIB_ACQUIRE_LOCK(OBJ) PyMutex_Lock(&(OBJ)->mutex) |
59 | | -#define HASHLIB_RELEASE_LOCK(OBJ) PyMutex_Unlock(&(OBJ)->mutex) |
60 | | - |
61 | 50 | #define HASHLIB_INIT_MUTEX(OBJ) \ |
62 | 51 | do { \ |
63 | 52 | (OBJ)->mutex = (PyMutex){0}; \ |
64 | 53 | } while (0) |
65 | 54 |
|
| 55 | +#define HASHLIB_ACQUIRE_LOCK(OBJ) PyMutex_Lock(&(OBJ)->mutex) |
| 56 | +#define HASHLIB_RELEASE_LOCK(OBJ) PyMutex_Unlock(&(OBJ)->mutex) |
| 57 | + |
| 58 | +/* |
| 59 | + * Message length above which the GIL is to be released |
| 60 | + * when performing hashing operations. |
| 61 | + */ |
66 | 62 | #define HASHLIB_GIL_MINSIZE 2048 |
| 63 | + |
| 64 | +// Macros for executing code while conditionally holding the GIL. |
| 65 | +// |
| 66 | +// These only drop the GIL if the lock acquisition itself is likely to |
| 67 | +// block. Thus the non-blocking acquire gating the GIL release for a |
| 68 | +// blocking lock acquisition. The intent of these macros is to surround |
| 69 | +// the assumed always "fast" operations that you aren't releasing the |
| 70 | +// GIL around. |
| 71 | + |
| 72 | +/* |
| 73 | + * Execute a suite of C statements 'STATEMENTS'. |
| 74 | + * |
| 75 | + * The GIL is held if 'SIZE' is below the HASHLIB_GIL_MINSIZE threshold. |
| 76 | + */ |
67 | 77 | #define HASHLIB_EXTERNAL_INSTRUCTIONS_UNLOCKED(SIZE, STATEMENTS) \ |
68 | 78 | do { \ |
69 | 79 | if ((SIZE) > HASHLIB_GIL_MINSIZE) { \ |
|
76 | 86 | } \ |
77 | 87 | } while (0) |
78 | 88 |
|
| 89 | +/* |
| 90 | + * Lock 'OBJ' and execute a suite of C statements 'STATEMENTS'. |
| 91 | + * |
| 92 | + * The GIL is held if 'SIZE' is below the HASHLIB_GIL_MINSIZE threshold. |
| 93 | + */ |
79 | 94 | #define HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED(OBJ, SIZE, STATEMENTS) \ |
80 | 95 | do { \ |
81 | 96 | if ((SIZE) > HASHLIB_GIL_MINSIZE) { \ |
|
0 commit comments