mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	gh-140544: Always assume that thread locals are available (GH-140690)
Python has required thread local support since 3.12 (see GH-103324). By assuming that thread locals are always supported, we can improve the performance of third-party extensions by allowing them to access the attached thread and interpreter states directly.
This commit is contained in:
		
							parent
							
								
									88ad41fa34
								
							
						
					
					
						commit
						2cefa70eb9
					
				
					 3 changed files with 12 additions and 34 deletions
				
			
		|  | @ -782,18 +782,13 @@ _PyImport_ClearModulesByIndex(PyInterpreterState *interp) | |||
|    substitute this (if the name actually matches). | ||||
| */ | ||||
| 
 | ||||
| #ifdef HAVE_THREAD_LOCAL | ||||
| _Py_thread_local const char *pkgcontext = NULL; | ||||
| # undef PKGCONTEXT | ||||
| # define PKGCONTEXT pkgcontext | ||||
| #endif | ||||
| 
 | ||||
| const char * | ||||
| _PyImport_ResolveNameWithPackageContext(const char *name) | ||||
| { | ||||
| #ifndef HAVE_THREAD_LOCAL | ||||
|     PyMutex_Lock(&EXTENSIONS.mutex); | ||||
| #endif | ||||
|     if (PKGCONTEXT != NULL) { | ||||
|         const char *p = strrchr(PKGCONTEXT, '.'); | ||||
|         if (p != NULL && strcmp(name, p+1) == 0) { | ||||
|  | @ -801,23 +796,14 @@ _PyImport_ResolveNameWithPackageContext(const char *name) | |||
|             PKGCONTEXT = NULL; | ||||
|         } | ||||
|     } | ||||
| #ifndef HAVE_THREAD_LOCAL | ||||
|     PyMutex_Unlock(&EXTENSIONS.mutex); | ||||
| #endif | ||||
|     return name; | ||||
| } | ||||
| 
 | ||||
| const char * | ||||
| _PyImport_SwapPackageContext(const char *newcontext) | ||||
| { | ||||
| #ifndef HAVE_THREAD_LOCAL | ||||
|     PyMutex_Lock(&EXTENSIONS.mutex); | ||||
| #endif | ||||
|     const char *oldcontext = PKGCONTEXT; | ||||
|     PKGCONTEXT = newcontext; | ||||
| #ifndef HAVE_THREAD_LOCAL | ||||
|     PyMutex_Unlock(&EXTENSIONS.mutex); | ||||
| #endif | ||||
|     return oldcontext; | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Peter Bierma
						Peter Bierma