src/os/unix/ngx_thread_mutex.c - nginx source code

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */

  5. #include <ngx_config.h>
  6. #include <ngx_core.h>


  7. /*
  8. * All modern pthread mutex implementations try to acquire a lock
  9. * atomically in userland before going to sleep in kernel.  Some
  10. * spins before the sleeping.
  11. *
  12. * In Solaris since version 8 all mutex types spin before sleeping.
  13. * The default spin count is 1000.  It can be overridden using
  14. * _THREAD_ADAPTIVE_SPIN=100 environment variable.
  15. *
  16. * In MacOSX all mutex types spin to acquire a lock protecting a mutex's
  17. * internals.  If the mutex is busy, thread calls Mach semaphore_wait().
  18. *
  19. *
  20. * PTHREAD_MUTEX_NORMAL lacks deadlock detection and is the fastest
  21. * mutex type.
  22. *
  23. *   Linux:    No spinning.  The internal name PTHREAD_MUTEX_TIMED_NP
  24. *             remains from the times when pthread_mutex_timedlock() was
  25. *             non-standard extension.  Alias name: PTHREAD_MUTEX_FAST_NP.
  26. *   FreeBSD:  No spinning.
  27. *
  28. *
  29. * PTHREAD_MUTEX_ERRORCHECK is usually as fast as PTHREAD_MUTEX_NORMAL
  30. * yet has lightweight deadlock detection.
  31. *
  32. *   Linux:    No spinning.  The internal name: PTHREAD_MUTEX_ERRORCHECK_NP.
  33. *   FreeBSD:  No spinning.
  34. *
  35. *
  36. * PTHREAD_MUTEX_RECURSIVE allows recursive locking.
  37. *
  38. *   Linux:    No spinning.  The internal name: PTHREAD_MUTEX_RECURSIVE_NP.
  39. *   FreeBSD:  No spinning.
  40. *
  41. *
  42. * PTHREAD_MUTEX_ADAPTIVE_NP spins on SMP systems before sleeping.
  43. *
  44. *   Linux:    No deadlock detection.  Dynamically changes a spin count
  45. *             for each mutex from 10 to 100 based on spin count taken
  46. *             previously.
  47. *   FreeBSD:  Deadlock detection.  The default spin count is 2000.
  48. *             It can be overridden using LIBPTHREAD_SPINLOOPS environment
  49. *             variable or by pthread_mutex_setspinloops_np().  If a lock
  50. *             is still busy, sched_yield() can be called on both UP and
  51. *             SMP systems.  The default yield loop count is zero, but
  52. *             it can be set by LIBPTHREAD_YIELDLOOPS environment
  53. *             variable or by pthread_mutex_setyieldloops_np().
  54. *   Solaris:  No PTHREAD_MUTEX_ADAPTIVE_NP.
  55. *   MacOSX:   No PTHREAD_MUTEX_ADAPTIVE_NP.
  56. *
  57. *
  58. * PTHREAD_MUTEX_ELISION_NP is a Linux extension to elide locks using
  59. * Intel Restricted Transactional Memory.  It is the most suitable for
  60. * rwlock pattern access because it allows simultaneous reads without lock.
  61. * Supported since glibc 2.18.
  62. *
  63. *
  64. * PTHREAD_MUTEX_DEFAULT is default mutex type.
  65. *
  66. *   Linux:    PTHREAD_MUTEX_NORMAL.
  67. *   FreeBSD:  PTHREAD_MUTEX_ERRORCHECK.
  68. *   Solaris:  PTHREAD_MUTEX_NORMAL.
  69. *   MacOSX:   PTHREAD_MUTEX_NORMAL.
  70. */


  71. ngx_int_t
  72. ngx_thread_mutex_create(ngx_thread_mutex_t *mtx, ngx_log_t *log)
  73. {
  74.     ngx_err_t            err;
  75.     pthread_mutexattr_t  attr;

  76.     err = pthread_mutexattr_init(&attr);
  77.     if (err != 0) {
  78.         ngx_log_error(NGX_LOG_EMERG, log, err,
  79.                       "pthread_mutexattr_init() failed");
  80.         return NGX_ERROR;
  81.     }

  82.     err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
  83.     if (err != 0) {
  84.         ngx_log_error(NGX_LOG_EMERG, log, err,
  85.                       "pthread_mutexattr_settype"
  86.                       "(PTHREAD_MUTEX_ERRORCHECK) failed");
  87.         return NGX_ERROR;
  88.     }

  89.     err = pthread_mutex_init(mtx, &attr);
  90.     if (err != 0) {
  91.         ngx_log_error(NGX_LOG_EMERG, log, err,
  92.                       "pthread_mutex_init() failed");
  93.         return NGX_ERROR;
  94.     }

  95.     err = pthread_mutexattr_destroy(&attr);
  96.     if (err != 0) {
  97.         ngx_log_error(NGX_LOG_ALERT, log, err,
  98.                       "pthread_mutexattr_destroy() failed");
  99.     }

  100.     return NGX_OK;
  101. }


  102. ngx_int_t
  103. ngx_thread_mutex_destroy(ngx_thread_mutex_t *mtx, ngx_log_t *log)
  104. {
  105.     ngx_err_t  err;

  106.     err = pthread_mutex_destroy(mtx);
  107.     if (err != 0) {
  108.         ngx_log_error(NGX_LOG_ALERT, log, err,
  109.                       "pthread_mutex_destroy() failed");
  110.         return NGX_ERROR;
  111.     }

  112.     return NGX_OK;
  113. }


  114. ngx_int_t
  115. ngx_thread_mutex_lock(ngx_thread_mutex_t *mtx, ngx_log_t *log)
  116. {
  117.     ngx_err_t  err;

  118.     err = pthread_mutex_lock(mtx);
  119.     if (err == 0) {
  120.         return NGX_OK;
  121.     }

  122.     ngx_log_error(NGX_LOG_ALERT, log, err, "pthread_mutex_lock() failed");

  123.     return NGX_ERROR;
  124. }


  125. ngx_int_t
  126. ngx_thread_mutex_unlock(ngx_thread_mutex_t *mtx, ngx_log_t *log)
  127. {
  128.     ngx_err_t  err;

  129.     err = pthread_mutex_unlock(mtx);

  130. #if 0
  131.     ngx_time_update();
  132. #endif

  133.     if (err == 0) {
  134.         return NGX_OK;
  135.     }

  136.     ngx_log_error(NGX_LOG_ALERT, log, err, "pthread_mutex_unlock() failed");

  137.     return NGX_ERROR;
  138. }