(root)/
Python-3.12.0/
Python/
thread_nt.h
       1  #include "pycore_interp.h"    // _PyInterpreterState.threads.stacksize
       2  
       3  /* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */
       4  /* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */
       5  /* Eliminated some memory leaks, gsw@agere.com */
       6  
       7  #include <windows.h>
       8  #include <limits.h>
       9  #ifdef HAVE_PROCESS_H
      10  #include <process.h>
      11  #endif
      12  
      13  /* options */
      14  #ifndef _PY_USE_CV_LOCKS
      15  #define _PY_USE_CV_LOCKS 1     /* use locks based on cond vars */
      16  #endif
      17  
      18  /* Now, define a non-recursive mutex using either condition variables
      19   * and critical sections (fast) or using operating system mutexes
      20   * (slow)
      21   */
      22  
      23  #if _PY_USE_CV_LOCKS
      24  
      25  #include "condvar.h"
      26  
      27  typedef struct _NRMUTEX
      28  {
      29      PyMUTEX_T cs;
      30      PyCOND_T cv;
      31      int locked;
      32  } NRMUTEX;
      33  typedef NRMUTEX *PNRMUTEX;
      34  
      35  static PNRMUTEX
      36  AllocNonRecursiveMutex(void)
      37  {
      38      PNRMUTEX m = (PNRMUTEX)PyMem_RawMalloc(sizeof(NRMUTEX));
      39      if (!m)
      40          return NULL;
      41      if (PyCOND_INIT(&m->cv))
      42          goto fail;
      43      if (PyMUTEX_INIT(&m->cs)) {
      44          PyCOND_FINI(&m->cv);
      45          goto fail;
      46      }
      47      m->locked = 0;
      48      return m;
      49  fail:
      50      PyMem_RawFree(m);
      51      return NULL;
      52  }
      53  
      54  static VOID
      55  FreeNonRecursiveMutex(PNRMUTEX mutex)
      56  {
      57      if (mutex) {
      58          PyCOND_FINI(&mutex->cv);
      59          PyMUTEX_FINI(&mutex->cs);
      60          PyMem_RawFree(mutex);
      61      }
      62  }
      63  
      64  static DWORD
      65  EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
      66  {
      67      DWORD result = WAIT_OBJECT_0;
      68      if (PyMUTEX_LOCK(&mutex->cs))
      69          return WAIT_FAILED;
      70      if (milliseconds == INFINITE) {
      71          while (mutex->locked) {
      72              if (PyCOND_WAIT(&mutex->cv, &mutex->cs)) {
      73                  result = WAIT_FAILED;
      74                  break;
      75              }
      76          }
      77      } else if (milliseconds != 0) {
      78          /* wait at least until the deadline */
      79          _PyTime_t nanoseconds = _PyTime_FromNanoseconds((_PyTime_t)milliseconds * 1000000);
      80          _PyTime_t deadline = _PyTime_Add(_PyTime_GetPerfCounter(), nanoseconds);
      81          while (mutex->locked) {
      82              _PyTime_t microseconds = _PyTime_AsMicroseconds(nanoseconds,
      83                                                              _PyTime_ROUND_TIMEOUT);
      84              if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, microseconds) < 0) {
      85                  result = WAIT_FAILED;
      86                  break;
      87              }
      88              nanoseconds = deadline - _PyTime_GetPerfCounter();
      89              if (nanoseconds <= 0) {
      90                  break;
      91              }
      92          }
      93      }
      94      if (!mutex->locked) {
      95          mutex->locked = 1;
      96          result = WAIT_OBJECT_0;
      97      } else if (result == WAIT_OBJECT_0)
      98          result = WAIT_TIMEOUT;
      99      /* else, it is WAIT_FAILED */
     100      PyMUTEX_UNLOCK(&mutex->cs); /* must ignore result here */
     101      return result;
     102  }
     103  
     104  static BOOL
     105  LeaveNonRecursiveMutex(PNRMUTEX mutex)
     106  {
     107      BOOL result;
     108      if (PyMUTEX_LOCK(&mutex->cs))
     109          return FALSE;
     110      mutex->locked = 0;
     111      /* condvar APIs return 0 on success. We need to return TRUE on success. */
     112      result = !PyCOND_SIGNAL(&mutex->cv);
     113      PyMUTEX_UNLOCK(&mutex->cs);
     114      return result;
     115  }
     116  
     117  #else /* if ! _PY_USE_CV_LOCKS */
     118  
     119  /* NR-locks based on a kernel mutex */
     120  #define PNRMUTEX HANDLE
     121  
     122  static PNRMUTEX
     123  AllocNonRecursiveMutex(void)
     124  {
     125      return CreateSemaphore(NULL, 1, 1, NULL);
     126  }
     127  
     128  static VOID
     129  FreeNonRecursiveMutex(PNRMUTEX mutex)
     130  {
     131      /* No in-use check */
     132      CloseHandle(mutex);
     133  }
     134  
     135  static DWORD
     136  EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
     137  {
     138      return WaitForSingleObjectEx(mutex, milliseconds, FALSE);
     139  }
     140  
     141  static BOOL
     142  LeaveNonRecursiveMutex(PNRMUTEX mutex)
     143  {
     144      return ReleaseSemaphore(mutex, 1, NULL);
     145  }
     146  #endif /* _PY_USE_CV_LOCKS */
     147  
     148  unsigned long PyThread_get_thread_ident(void);
     149  
     150  #ifdef PY_HAVE_THREAD_NATIVE_ID
     151  unsigned long PyThread_get_thread_native_id(void);
     152  #endif
     153  
     154  /*
     155   * Initialization for the current runtime.
     156   */
     157  static void
     158  PyThread__init_thread(void)
     159  {
     160      // Initialization of the C package should not be needed.
     161  }
     162  
     163  /*
     164   * Thread support.
     165   */
     166  
     167  typedef struct {
     168      void (*func)(void*);
     169      void *arg;
     170  } callobj;
     171  
     172  /* thunker to call adapt between the function type used by the system's
     173  thread start function and the internally used one. */
     174  static unsigned __stdcall
     175  bootstrap(void *call)
     176  {
     177      callobj *obj = (callobj*)call;
     178      void (*func)(void*) = obj->func;
     179      void *arg = obj->arg;
     180      HeapFree(GetProcessHeap(), 0, obj);
     181      func(arg);
     182      return 0;
     183  }
     184  
     185  unsigned long
     186  PyThread_start_new_thread(void (*func)(void *), void *arg)
     187  {
     188      HANDLE hThread;
     189      unsigned threadID;
     190      callobj *obj;
     191  
     192      if (!initialized)
     193          PyThread_init_thread();
     194  
     195      obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
     196      if (!obj)
     197          return PYTHREAD_INVALID_THREAD_ID;
     198      obj->func = func;
     199      obj->arg = arg;
     200      PyThreadState *tstate = _PyThreadState_GET();
     201      size_t stacksize = tstate ? tstate->interp->threads.stacksize : 0;
     202      hThread = (HANDLE)_beginthreadex(0,
     203                        Py_SAFE_DOWNCAST(stacksize, Py_ssize_t, unsigned int),
     204                        bootstrap, obj,
     205                        0, &threadID);
     206      if (hThread == 0) {
     207          /* I've seen errno == EAGAIN here, which means "there are
     208           * too many threads".
     209           */
     210          int e = errno;
     211          threadID = (unsigned)-1;
     212          HeapFree(GetProcessHeap(), 0, obj);
     213      }
     214      else {
     215          CloseHandle(hThread);
     216      }
     217      return threadID;
     218  }
     219  
     220  /*
     221   * Return the thread Id instead of a handle. The Id is said to uniquely identify the
     222   * thread in the system
     223   */
     224  unsigned long
     225  PyThread_get_thread_ident(void)
     226  {
     227      if (!initialized)
     228          PyThread_init_thread();
     229  
     230      return GetCurrentThreadId();
     231  }
     232  
     233  #ifdef PY_HAVE_THREAD_NATIVE_ID
     234  /*
     235   * Return the native Thread ID (TID) of the calling thread.
     236   * The native ID of a thread is valid and guaranteed to be unique system-wide
     237   * from the time the thread is created until the thread has been terminated.
     238   */
     239  unsigned long
     240  PyThread_get_thread_native_id(void)
     241  {
     242      if (!initialized) {
     243          PyThread_init_thread();
     244      }
     245  
     246      DWORD native_id;
     247      native_id = GetCurrentThreadId();
     248      return (unsigned long) native_id;
     249  }
     250  #endif
     251  
     252  void _Py_NO_RETURN
     253  PyThread_exit_thread(void)
     254  {
     255      if (!initialized)
     256          exit(0);
     257      _endthreadex(0);
     258  }
     259  
     260  /*
     261   * Lock support. It has to be implemented as semaphores.
     262   * I [Dag] tried to implement it with mutex but I could find a way to
     263   * tell whether a thread already own the lock or not.
     264   */
     265  PyThread_type_lock
     266  PyThread_allocate_lock(void)
     267  {
     268      PNRMUTEX mutex;
     269  
     270      if (!initialized)
     271          PyThread_init_thread();
     272  
     273      mutex = AllocNonRecursiveMutex() ;
     274  
     275      PyThread_type_lock aLock = (PyThread_type_lock) mutex;
     276      assert(aLock);
     277  
     278      return aLock;
     279  }
     280  
     281  void
     282  PyThread_free_lock(PyThread_type_lock aLock)
     283  {
     284      FreeNonRecursiveMutex(aLock) ;
     285  }
     286  
     287  // WaitForSingleObject() accepts timeout in milliseconds in the range
     288  // [0; 0xFFFFFFFE] (DWORD type). INFINITE value (0xFFFFFFFF) means no
     289  // timeout. 0xFFFFFFFE milliseconds is around 49.7 days.
     290  const DWORD TIMEOUT_MS_MAX = 0xFFFFFFFE;
     291  
     292  /*
     293   * Return 1 on success if the lock was acquired
     294   *
     295   * and 0 if the lock was not acquired. This means a 0 is returned
     296   * if the lock has already been acquired by this thread!
     297   */
     298  PyLockStatus
     299  PyThread_acquire_lock_timed(PyThread_type_lock aLock,
     300                              PY_TIMEOUT_T microseconds, int intr_flag)
     301  {
     302      assert(aLock);
     303  
     304      /* Fow now, intr_flag does nothing on Windows, and lock acquires are
     305       * uninterruptible.  */
     306      PyLockStatus success;
     307      PY_TIMEOUT_T milliseconds;
     308  
     309      if (microseconds >= 0) {
     310          milliseconds = microseconds / 1000;
     311          // Round milliseconds away from zero
     312          if (microseconds % 1000 > 0) {
     313              milliseconds++;
     314          }
     315          if (milliseconds > (PY_TIMEOUT_T)TIMEOUT_MS_MAX) {
     316              // bpo-41710: PyThread_acquire_lock_timed() cannot report timeout
     317              // overflow to the caller, so clamp the timeout to
     318              // [0, TIMEOUT_MS_MAX] milliseconds.
     319              //
     320              // _thread.Lock.acquire() and _thread.RLock.acquire() raise an
     321              // OverflowError if microseconds is greater than PY_TIMEOUT_MAX.
     322              milliseconds = TIMEOUT_MS_MAX;
     323          }
     324          assert(milliseconds != INFINITE);
     325      }
     326      else {
     327          milliseconds = INFINITE;
     328      }
     329  
     330      if (EnterNonRecursiveMutex((PNRMUTEX)aLock,
     331                                 (DWORD)milliseconds) == WAIT_OBJECT_0) {
     332          success = PY_LOCK_ACQUIRED;
     333      }
     334      else {
     335          success = PY_LOCK_FAILURE;
     336      }
     337  
     338      return success;
     339  }
     340  int
     341  PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
     342  {
     343      return PyThread_acquire_lock_timed(aLock, waitflag ? -1 : 0, 0);
     344  }
     345  
     346  void
     347  PyThread_release_lock(PyThread_type_lock aLock)
     348  {
     349      assert(aLock);
     350      (void)LeaveNonRecursiveMutex((PNRMUTEX) aLock);
     351  }
     352  
     353  /* minimum/maximum thread stack sizes supported */
     354  #define THREAD_MIN_STACKSIZE    0x8000          /* 32 KiB */
     355  #define THREAD_MAX_STACKSIZE    0x10000000      /* 256 MiB */
     356  
     357  /* set the thread stack size.
     358   * Return 0 if size is valid, -1 otherwise.
     359   */
     360  static int
     361  _pythread_nt_set_stacksize(size_t size)
     362  {
     363      /* set to default */
     364      if (size == 0) {
     365          _PyInterpreterState_GET()->threads.stacksize = 0;
     366          return 0;
     367      }
     368  
     369      /* valid range? */
     370      if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
     371          _PyInterpreterState_GET()->threads.stacksize = size;
     372          return 0;
     373      }
     374  
     375      return -1;
     376  }
     377  
     378  #define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)
     379  
     380  
     381  /* Thread Local Storage (TLS) API
     382  
     383     This API is DEPRECATED since Python 3.7.  See PEP 539 for details.
     384  */
     385  
     386  int
     387  PyThread_create_key(void)
     388  {
     389      DWORD result = TlsAlloc();
     390      if (result == TLS_OUT_OF_INDEXES)
     391          return -1;
     392      return (int)result;
     393  }
     394  
     395  void
     396  PyThread_delete_key(int key)
     397  {
     398      TlsFree(key);
     399  }
     400  
     401  int
     402  PyThread_set_key_value(int key, void *value)
     403  {
     404      BOOL ok = TlsSetValue(key, value);
     405      return ok ? 0 : -1;
     406  }
     407  
     408  void *
     409  PyThread_get_key_value(int key)
     410  {
     411      /* because TLS is used in the Py_END_ALLOW_THREAD macro,
     412       * it is necessary to preserve the windows error state, because
     413       * it is assumed to be preserved across the call to the macro.
     414       * Ideally, the macro should be fixed, but it is simpler to
     415       * do it here.
     416       */
     417      DWORD error = GetLastError();
     418      void *result = TlsGetValue(key);
     419      SetLastError(error);
     420      return result;
     421  }
     422  
     423  void
     424  PyThread_delete_key_value(int key)
     425  {
     426      /* NULL is used as "key missing", and it is also the default
     427       * given by TlsGetValue() if nothing has been set yet.
     428       */
     429      TlsSetValue(key, NULL);
     430  }
     431  
     432  
     433  /* reinitialization of TLS is not necessary after fork when using
     434   * the native TLS functions.  And forking isn't supported on Windows either.
     435   */
     436  void
     437  PyThread_ReInitTLS(void)
     438  {
     439  }
     440  
     441  
     442  /* Thread Specific Storage (TSS) API
     443  
     444     Platform-specific components of TSS API implementation.
     445  */
     446  
     447  int
     448  PyThread_tss_create(Py_tss_t *key)
     449  {
     450      assert(key != NULL);
     451      /* If the key has been created, function is silently skipped. */
     452      if (key->_is_initialized) {
     453          return 0;
     454      }
     455  
     456      DWORD result = TlsAlloc();
     457      if (result == TLS_OUT_OF_INDEXES) {
     458          return -1;
     459      }
     460      /* In Windows, platform-specific key type is DWORD. */
     461      key->_key = result;
     462      key->_is_initialized = 1;
     463      return 0;
     464  }
     465  
     466  void
     467  PyThread_tss_delete(Py_tss_t *key)
     468  {
     469      assert(key != NULL);
     470      /* If the key has not been created, function is silently skipped. */
     471      if (!key->_is_initialized) {
     472          return;
     473      }
     474  
     475      TlsFree(key->_key);
     476      key->_key = TLS_OUT_OF_INDEXES;
     477      key->_is_initialized = 0;
     478  }
     479  
     480  int
     481  PyThread_tss_set(Py_tss_t *key, void *value)
     482  {
     483      assert(key != NULL);
     484      BOOL ok = TlsSetValue(key->_key, value);
     485      return ok ? 0 : -1;
     486  }
     487  
     488  void *
     489  PyThread_tss_get(Py_tss_t *key)
     490  {
     491      assert(key != NULL);
     492      /* because TSS is used in the Py_END_ALLOW_THREAD macro,
     493       * it is necessary to preserve the windows error state, because
     494       * it is assumed to be preserved across the call to the macro.
     495       * Ideally, the macro should be fixed, but it is simpler to
     496       * do it here.
     497       */
     498      DWORD error = GetLastError();
     499      void *result = TlsGetValue(key->_key);
     500      SetLastError(error);
     501      return result;
     502  }