diff options
author | Masayuki Yamamoto <ma3yuki.8mamo10@gmail.com> | 2017-10-06 10:41:34 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2017-10-06 10:41:34 (GMT) |
commit | 731e18901484c75b60167a06a0ba0719a6d4827d (patch) | |
tree | fc9b8afc6eb8453729c130a146b838228ab103c6 /Python/thread.c | |
parent | b8ab9d3fc816f85f4d6dbef12b7414e6dc10e4dd (diff) | |
download | cpython-731e18901484c75b60167a06a0ba0719a6d4827d.zip cpython-731e18901484c75b60167a06a0ba0719a6d4827d.tar.gz cpython-731e18901484c75b60167a06a0ba0719a6d4827d.tar.bz2 |
bpo-25658: Implement PEP 539 for Thread Specific Storage (TSS) API (GH-1362)
See PEP 539 for details.
Highlights of changes:
- Add Thread Specific Storage (TSS) API
- Document the Thread Local Storage (TLS) API as deprecated
- Update code that used TLS API to use TSS API
Diffstat (limited to 'Python/thread.c')
-rw-r--r-- | Python/thread.c | 60 |
1 files changed, 28 insertions, 32 deletions
diff --git a/Python/thread.c b/Python/thread.c index f742d05..7eac836 100644 --- a/Python/thread.c +++ b/Python/thread.c @@ -84,7 +84,7 @@ PyThread_init_thread(void) # define PYTHREAD_NAME "nt" # include "thread_nt.h" #else -# error "Require native thread feature. See https://bugs.python.org/issue30832" +# error "Require native threads. See https://bugs.python.org/issue31370" #endif @@ -111,41 +111,37 @@ PyThread_set_stacksize(size_t size) } -/* ------------------------------------------------------------------------ -Per-thread data ("key") support. +/* Thread Specific Storage (TSS) API -Use PyThread_create_key() to create a new key. This is typically shared -across threads. - -Use PyThread_set_key_value(thekey, value) to associate void* value with -thekey in the current thread. Each thread has a distinct mapping of thekey -to a void* value. Caution: if the current thread already has a mapping -for thekey, value is ignored. - -Use PyThread_get_key_value(thekey) to retrieve the void* value associated -with thekey in the current thread. This returns NULL if no value is -associated with thekey in the current thread. - -Use PyThread_delete_key_value(thekey) to forget the current thread's associated -value for thekey. PyThread_delete_key(thekey) forgets the values associated -with thekey across *all* threads. - -While some of these functions have error-return values, none set any -Python exception. + Cross-platform components of TSS API implementation. +*/ -None of the functions does memory management on behalf of the void* values. -You need to allocate and deallocate them yourself. If the void* values -happen to be PyObject*, these functions don't do refcount operations on -them either. +Py_tss_t * +PyThread_tss_alloc(void) +{ + Py_tss_t *new_key = (Py_tss_t *)PyMem_RawMalloc(sizeof(Py_tss_t)); + if (new_key == NULL) { + return NULL; + } + new_key->_is_initialized = 0; + return new_key; +} -The GIL does not need to be held when calling these functions; they supply -their own locking. This isn't true of PyThread_create_key(), though (see -next paragraph). +void +PyThread_tss_free(Py_tss_t *key) +{ + if (key != NULL) { + PyThread_tss_delete(key); + PyMem_RawFree((void *)key); + } +} -There's a hidden assumption that PyThread_create_key() will be called before -any of the other functions are called. There's also a hidden assumption -that calls to PyThread_create_key() are serialized externally. ------------------------------------------------------------------------- */ +int +PyThread_tss_is_created(Py_tss_t *key) +{ + assert(key != NULL); + return key->_is_initialized; +} PyDoc_STRVAR(threadinfo__doc__, |