os_mutex.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* os_mutex.h -*-C++-*-
  2. *
  3. *************************************************************************
  4. *
  5. * @copyright
  6. * Copyright (C) 2009-2013, Intel Corporation
  7. * All rights reserved.
  8. *
  9. * @copyright
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. * * Neither the name of Intel Corporation nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * @copyright
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  32. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  33. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  35. * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. **************************************************************************/
  38. /**
  39. * @file os_mutex.h
  40. *
  41. * @brief Portable interface to operating-system mutexes.
  42. *
  43. * Do not confuse os_mutex with Cilk runtime-specific spinlock mutexes.
  44. */
  45. #ifndef INCLUDED_OS_MUTEX_DOT_H
  46. #define INCLUDED_OS_MUTEX_DOT_H
  47. #include <cilk/common.h>
  48. #include "rts-common.h"
  49. __CILKRTS_BEGIN_EXTERN_C
  50. /// Opaque type
  51. typedef struct os_mutex os_mutex;
  52. /**
  53. * Allocate and initialize an os_mutex
  54. *
  55. * @return A pointer to the initialized os_mutex
  56. */
  57. COMMON_SYSDEP os_mutex* __cilkrts_os_mutex_create(void);
  58. /**
  59. * Acquire the os_mutex for exclusive use
  60. *
  61. * @param m The os_mutex that is to be acquired.
  62. */
  63. COMMON_SYSDEP void __cilkrts_os_mutex_lock(os_mutex *m);
  64. /**
  65. * Try to acquire the os_mutex.
  66. *
  67. * @param m The os_mutex to try to acquire
  68. * @return 0 if the lock acquire failed
  69. * @return nonzero if the lock was acquired
  70. */
  71. COMMON_SYSDEP int __cilkrts_os_mutex_trylock(os_mutex *m);
  72. /**
  73. * Release the os_mutex
  74. *
  75. * @param m The os_mutex that is to be released.
  76. */
  77. COMMON_SYSDEP void __cilkrts_os_mutex_unlock(os_mutex *m);
  78. /**
  79. * Release any resources and deallocate the os_mutex.
  80. *
  81. * @param m The os_mutex that is to be deallocated.
  82. */
  83. COMMON_SYSDEP void __cilkrts_os_mutex_destroy(os_mutex *m);
  84. /**
  85. * Acquire the global os_mutex for exclusive use. The global os_mutex
  86. * will be initialized the first time this function is called in a
  87. * thread-safe manner.
  88. */
  89. COMMON_SYSDEP void global_os_mutex_lock();
  90. /**
  91. * Release the global os_mutex. global_os_mutex_lock() must have been
  92. * called first.
  93. */
  94. COMMON_SYSDEP void global_os_mutex_unlock();
  95. #ifdef _MSC_VER
  96. /**
  97. * @brief Create the global OS mutex - Windows only.
  98. *
  99. * On Windows we use DllMain() to create the global OS mutex when cilkrts20.dll
  100. * is loaded. As opposed to Linux/MacOS where we use pthread_once to implement
  101. * a singleton since there are no guarantees about constructor or destructor
  102. * ordering between shared objects.
  103. */
  104. NON_COMMON void global_os_mutex_create();
  105. /**
  106. * @brief Destroy the global OS mutex - Windows only
  107. *
  108. * On Windows we use DllMain() to destroy the global OS mutex when
  109. * cilkrts20.dll is unloaded. As opposed to Linux/MacOS where we cannot
  110. * know when it's safe to destroy the global OS mutex since there are no
  111. * guarantees about constructor or destructor ordering.
  112. */
  113. NON_COMMON void global_os_mutex_destroy();
  114. #endif // _MSC_VER
  115. __CILKRTS_END_EXTERN_C
  116. #endif // ! defined(INCLUDED_OS_MUTEX_DOT_H)