Mutex.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the llvm::sys::Mutex class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Config/config.h"
  14. #include "llvm/Support/Mutex.h"
  15. //===----------------------------------------------------------------------===//
  16. //=== WARNING: Implementation here must contain only TRULY operating system
  17. //=== independent code.
  18. //===----------------------------------------------------------------------===//
  19. #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
  20. // Define all methods as no-ops if threading is explicitly disabled
  21. namespace llvm {
  22. using namespace sys;
  23. MutexImpl::MutexImpl( bool recursive) { }
  24. MutexImpl::~MutexImpl() { }
  25. bool MutexImpl::acquire() { return true; }
  26. bool MutexImpl::release() { return true; }
  27. bool MutexImpl::tryacquire() { return true; }
  28. }
  29. #else
  30. #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
  31. #include <cassert>
  32. #include <pthread.h>
  33. #include <stdlib.h>
  34. namespace llvm {
  35. using namespace sys;
  36. // Construct a Mutex using pthread calls
  37. MutexImpl::MutexImpl( bool recursive)
  38. : data_(nullptr)
  39. {
  40. // Declare the pthread_mutex data structures
  41. pthread_mutex_t* mutex =
  42. static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
  43. pthread_mutexattr_t attr;
  44. // Initialize the mutex attributes
  45. int errorcode = pthread_mutexattr_init(&attr);
  46. assert(errorcode == 0); (void)errorcode;
  47. // Initialize the mutex as a recursive mutex, if requested, or normal
  48. // otherwise.
  49. int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
  50. errorcode = pthread_mutexattr_settype(&attr, kind);
  51. assert(errorcode == 0);
  52. // Initialize the mutex
  53. errorcode = pthread_mutex_init(mutex, &attr);
  54. assert(errorcode == 0);
  55. // Destroy the attributes
  56. errorcode = pthread_mutexattr_destroy(&attr);
  57. assert(errorcode == 0);
  58. // Assign the data member
  59. data_ = mutex;
  60. }
  61. // Destruct a Mutex
  62. MutexImpl::~MutexImpl()
  63. {
  64. pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
  65. assert(mutex != nullptr);
  66. pthread_mutex_destroy(mutex);
  67. free(mutex);
  68. }
  69. bool
  70. MutexImpl::acquire()
  71. {
  72. pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
  73. assert(mutex != nullptr);
  74. int errorcode = pthread_mutex_lock(mutex);
  75. return errorcode == 0;
  76. }
  77. bool
  78. MutexImpl::release()
  79. {
  80. pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
  81. assert(mutex != nullptr);
  82. int errorcode = pthread_mutex_unlock(mutex);
  83. return errorcode == 0;
  84. }
  85. bool
  86. MutexImpl::tryacquire()
  87. {
  88. pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
  89. assert(mutex != nullptr);
  90. int errorcode = pthread_mutex_trylock(mutex);
  91. return errorcode == 0;
  92. }
  93. }
  94. #elif defined(LLVM_ON_UNIX)
  95. #include "Unix/Mutex.inc"
  96. #elif defined( LLVM_ON_WIN32)
  97. #include "Windows/Mutex.inc"
  98. #else
  99. #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
  100. #endif
  101. #endif