worker_mutex.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* worker_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 worker_mutex.h
  40. *
  41. * @brief Support for Cilk runtime mutexes.
  42. *
  43. * Cilk runtime mutexes are implemented as simple spin loops.
  44. */
  45. #ifndef INCLUDED_WORKER_MUTEX_DOT_H
  46. #define INCLUDED_WORKER_MUTEX_DOT_H
  47. #include <cilk/common.h>
  48. #include "rts-common.h"
  49. __CILKRTS_BEGIN_EXTERN_C
  50. /**
  51. * Mutexes are treated as an abstract data type within the Cilk
  52. * runtime system. They are implemented as simple spin loops and
  53. * owned by a __cilkrts_worker.
  54. */
  55. typedef struct mutex {
  56. /** Mutex spin loop variable. 0 if unowned, 1 if owned. */
  57. volatile int lock;
  58. /** Worker that owns the mutex. Must be 0 if mutex is unowned. */
  59. __cilkrts_worker *owner;
  60. } mutex;
  61. /**
  62. * @brief Initialize a Cilk mutex.
  63. *
  64. * @param m Mutex to be initialized.
  65. */
  66. COMMON_PORTABLE
  67. void __cilkrts_mutex_init(struct mutex *m);
  68. /**
  69. * @brief Acquire a Cilk mutex.
  70. *
  71. * If statistics are being gathered, the time spent
  72. * acquiring the mutex will be attributed to the specified worker.
  73. *
  74. * @param w Worker that will become the owner of this mutex.
  75. * @param m Mutex to be initialized.
  76. */
  77. COMMON_PORTABLE
  78. void __cilkrts_mutex_lock(__cilkrts_worker *w,
  79. struct mutex *m);
  80. /**
  81. * @brief Attempt to lock a Cilk mutex and fail if it isn't available.
  82. *
  83. * If statistics are being gathered, the time spent acquiring the
  84. * mutex will be attributed to the specified worker.
  85. *
  86. * @param w Worker that will become the owner of this mutex.
  87. * @param m Mutex to be acquired.
  88. *
  89. * @return 1 if the mutex was acquired.
  90. * @return 0 if the mutex was not acquired.
  91. */
  92. COMMON_PORTABLE
  93. int __cilkrts_mutex_trylock(__cilkrts_worker *w,
  94. struct mutex *m);
  95. /**
  96. * @brief Release a Cilk mutex.
  97. *
  98. * If statistics are being gathered, the time spent
  99. * acquiring the mutex will be attributed to the specified worker.
  100. *
  101. * @pre The mutex must be owned by the worker.
  102. *
  103. * @param w Worker that owns this mutex.
  104. * @param m Mutex to be released.
  105. */
  106. COMMON_PORTABLE
  107. void __cilkrts_mutex_unlock(__cilkrts_worker *w,
  108. struct mutex *m);
  109. /**
  110. * @brief Deallocate a Cilk mutex. Currently does nothing.
  111. *
  112. * @param w Unused.
  113. * @param m Mutex to be deallocated.
  114. */
  115. COMMON_PORTABLE
  116. void __cilkrts_mutex_destroy(__cilkrts_worker *w,
  117. struct mutex *m);
  118. __CILKRTS_END_EXTERN_C
  119. #endif // ! defined(INCLUDED_WORKER_MUTEX_DOT_H)