spin_mutex.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* spin_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 spin_mutex.h
  40. *
  41. * @brief Support for Cilk runtime mutexes.
  42. *
  43. * Cilk runtime mutexes are implemented as simple spin loops.
  44. *
  45. * This file is similar to a worker_mutex, except it does not have an
  46. * owner field.
  47. *
  48. * TBD: This class, worker_mutex, and os_mutex overlap quite a bit in
  49. * functionality. Can we unify these mutexes somehow?
  50. */
  51. #ifndef INCLUDED_SPIN_MUTEX_DOT_H
  52. #define INCLUDED_SPIN_MUTEX_DOT_H
  53. #include <cilk/common.h>
  54. #include "rts-common.h"
  55. #include "cilk_malloc.h"
  56. __CILKRTS_BEGIN_EXTERN_C
  57. /**
  58. * Mutexes are treated as an abstract data type within the Cilk
  59. * runtime system. They are implemented as simple spin loops.
  60. */
  61. typedef struct spin_mutex {
  62. /** Mutex spin loop variable. 0 if unowned, 1 if owned. */
  63. volatile int lock;
  64. /** Padding so the mutex takes up a cache line. */
  65. char pad[64/sizeof(int) - 1];
  66. } spin_mutex;
  67. /**
  68. * @brief Create a new Cilk spin_mutex.
  69. *
  70. * @return Returns an initialized spin mutex.
  71. */
  72. COMMON_PORTABLE
  73. spin_mutex* spin_mutex_create();
  74. /**
  75. * @brief Initialize a Cilk spin_mutex.
  76. *
  77. * @param m Spin_Mutex to be initialized.
  78. */
  79. COMMON_PORTABLE
  80. void spin_mutex_init(spin_mutex *m);
  81. /**
  82. * @brief Acquire a Cilk spin_mutex.
  83. *
  84. * If statistics are being gathered, the time spent
  85. * acquiring the spin_mutex will be attributed to the specified worker.
  86. *
  87. * @param m Spin_Mutex to be initialized.
  88. */
  89. COMMON_PORTABLE
  90. void spin_mutex_lock(struct spin_mutex *m);
  91. /**
  92. * @brief Attempt to lock a Cilk spin_mutex and fail if it isn't available.
  93. *
  94. * @param m Spin_Mutex to be acquired.
  95. *
  96. * @return 1 if the spin_mutex was acquired.
  97. * @return 0 if the spin_mutex was not acquired.
  98. */
  99. COMMON_PORTABLE
  100. int spin_mutex_trylock(struct spin_mutex *m);
  101. /**
  102. * @brief Release a Cilk spin_mutex.
  103. *
  104. * @param m Spin_Mutex to be released.
  105. */
  106. COMMON_PORTABLE
  107. void spin_mutex_unlock(struct spin_mutex *m);
  108. /**
  109. * @brief Deallocate a Cilk spin_mutex. Currently does nothing.
  110. *
  111. * @param m Spin_Mutex to be deallocated.
  112. */
  113. COMMON_PORTABLE
  114. void spin_mutex_destroy(struct spin_mutex *m);
  115. __CILKRTS_END_EXTERN_C
  116. #endif // ! defined(INCLUDED_SPIN_MUTEX_DOT_H)