worker_mutex.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* worker_mutex.c -*-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. #include "worker_mutex.h"
  39. #include "bug.h"
  40. #include "os.h"
  41. #include "stats.h"
  42. /* m->lock == 1 means that mutex M is locked */
  43. #define TRY_ACQUIRE(m) (__cilkrts_xchg(&(m)->lock, 1) == 0)
  44. /* ICC 11.1+ understands release semantics and generates an
  45. ordinary store with a software memory barrier. */
  46. #if __ICC >= 1110
  47. #define RELEASE(m) __sync_lock_release(&(m)->lock)
  48. #else
  49. #define RELEASE(m) __cilkrts_xchg(&(m)->lock, 0)
  50. #endif
  51. void __cilkrts_mutex_init(struct mutex *m)
  52. {
  53. m->owner = 0;
  54. // Use a simple assignment so Inspector doesn't bug us about the
  55. // interlocked exchange doing a read of an uninitialized variable.
  56. // By definition there can't be a race when we're initializing the
  57. // lock...
  58. m->lock = 0;
  59. }
  60. void __cilkrts_mutex_lock(__cilkrts_worker *w, struct mutex *m)
  61. {
  62. int count;
  63. const int maxspin = 1000; /* SWAG */
  64. NOTE_INTERVAL(w, INTERVAL_MUTEX_LOCK);
  65. if (!TRY_ACQUIRE(m)) {
  66. START_INTERVAL(w, INTERVAL_MUTEX_LOCK_SPINNING);
  67. count = 0;
  68. do {
  69. do {
  70. __cilkrts_short_pause();
  71. if (++count >= maxspin) {
  72. STOP_INTERVAL(w, INTERVAL_MUTEX_LOCK_SPINNING);
  73. START_INTERVAL(w, INTERVAL_MUTEX_LOCK_YIELDING);
  74. /* let the OS reschedule every once in a while */
  75. __cilkrts_yield();
  76. STOP_INTERVAL(w, INTERVAL_MUTEX_LOCK_YIELDING);
  77. START_INTERVAL(w, INTERVAL_MUTEX_LOCK_SPINNING);
  78. count = 0;
  79. }
  80. } while (m->lock != 0);
  81. } while (!TRY_ACQUIRE(m));
  82. STOP_INTERVAL(w, INTERVAL_MUTEX_LOCK_SPINNING);
  83. }
  84. CILK_ASSERT(m->owner == 0);
  85. m->owner = w;
  86. }
  87. int __cilkrts_mutex_trylock(__cilkrts_worker *w, struct mutex *m)
  88. {
  89. NOTE_INTERVAL(w, INTERVAL_MUTEX_TRYLOCK);
  90. if (TRY_ACQUIRE(m)) {
  91. CILK_ASSERT(m->owner == 0);
  92. m->owner = w;
  93. return 1;
  94. } else {
  95. return 0;
  96. }
  97. }
  98. void __cilkrts_mutex_unlock(__cilkrts_worker *w, struct mutex *m)
  99. {
  100. CILK_ASSERT(m->owner == w);
  101. m->owner = 0;
  102. RELEASE(m);
  103. }
  104. void __cilkrts_mutex_destroy(__cilkrts_worker *w, struct mutex *m)
  105. {
  106. (void)w; /* unused */
  107. (void)m; /* unused */
  108. }
  109. /* End worker_mutex.c */