arbiters.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Copyright (C) 1995,1996, 1997, 2000, 2001, 2004, 2005, 2006, 2008, 2011 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include "libguile/ports.h"
  23. #include "libguile/smob.h"
  24. #include "libguile/validate.h"
  25. #include "libguile/arbiters.h"
  26. /* FETCH_STORE sets "fet" to the value fetched from "mem" and then stores
  27. "sto" there. The fetch and store are done atomically, so once the fetch
  28. has been done no other thread or processor can fetch from there before
  29. the store is done.
  30. The operands are scm_t_bits, fet and sto are plain variables, mem is a
  31. memory location (ie. an lvalue).
  32. ENHANCE-ME: Add more cpu-specifics. glibc atomicity.h has some of the
  33. sort of thing required. FETCH_STORE could become some sort of
  34. compare-and-store if that better suited what various cpus do. */
  35. #if defined (__GNUC__) && defined (i386) && SIZEOF_SCM_T_BITS == 4
  36. /* This is for i386 with the normal 32-bit scm_t_bits. The xchg instruction
  37. is atomic on a single processor, and it automatically asserts the "lock"
  38. bus signal so it's atomic on a multi-processor (no need for the lock
  39. prefix on the instruction).
  40. The mem operand is read-write but "+" is not used since old gcc
  41. (eg. 2.7.2) doesn't support that. "1" for the mem input doesn't work
  42. (eg. gcc 3.3) when mem is a pointer dereference like current usage below.
  43. Having mem as a plain input should be ok though. It tells gcc the value
  44. is live, but as an "m" gcc won't fetch it itself (though that would be
  45. harmless). */
  46. #define FETCH_STORE(fet,mem,sto) \
  47. do { \
  48. asm ("xchg %0, %1" \
  49. : "=r" (fet), "=m" (mem) \
  50. : "0" (sto), "m" (mem)); \
  51. } while (0)
  52. #endif
  53. #ifndef FETCH_STORE
  54. /* This is a generic version, with a mutex to ensure the operation is
  55. atomic. Unfortunately this approach probably makes arbiters no faster
  56. than mutexes (though still using less memory of course), so some
  57. CPU-specifics are highly desirable. */
  58. #define FETCH_STORE(fet,mem,sto) \
  59. do { \
  60. scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex); \
  61. (fet) = (mem); \
  62. (mem) = (sto); \
  63. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex); \
  64. } while (0)
  65. #endif
  66. static scm_t_bits scm_tc16_arbiter;
  67. #define SCM_LOCK_VAL (scm_tc16_arbiter | (1L << 16))
  68. #define SCM_UNLOCK_VAL scm_tc16_arbiter
  69. #define SCM_ARB_LOCKED(arb) ((SCM_CELL_WORD_0 (arb)) & (1L << 16))
  70. static int
  71. arbiter_print (SCM exp, SCM port, scm_print_state *pstate)
  72. {
  73. scm_puts_unlocked ("#<arbiter ", port);
  74. if (SCM_ARB_LOCKED (exp))
  75. scm_puts_unlocked ("locked ", port);
  76. scm_iprin1 (SCM_PACK (SCM_SMOB_DATA (exp)), port, pstate);
  77. scm_putc_unlocked ('>', port);
  78. return !0;
  79. }
  80. SCM_DEFINE (scm_make_arbiter, "make-arbiter", 1, 0, 0,
  81. (SCM name),
  82. "Return an arbiter object, initially unlocked. Currently\n"
  83. "@var{name} is only used for diagnostic output.")
  84. #define FUNC_NAME s_scm_make_arbiter
  85. {
  86. SCM_RETURN_NEWSMOB (scm_tc16_arbiter, SCM_UNPACK (name));
  87. }
  88. #undef FUNC_NAME
  89. /* The atomic FETCH_STORE here is so two threads can't both see the arbiter
  90. unlocked and return #t. The arbiter itself wouldn't be corrupted by
  91. this, but two threads both getting #t would be contrary to the intended
  92. semantics. */
  93. SCM_DEFINE (scm_try_arbiter, "try-arbiter", 1, 0, 0,
  94. (SCM arb),
  95. "If @var{arb} is unlocked, then lock it and return @code{#t}.\n"
  96. "If @var{arb} is already locked, then do nothing and return\n"
  97. "@code{#f}.")
  98. #define FUNC_NAME s_scm_try_arbiter
  99. {
  100. scm_t_bits old;
  101. scm_t_bits *loc;
  102. SCM_VALIDATE_SMOB (1, arb, arbiter);
  103. loc = (scm_t_bits*)SCM_SMOB_OBJECT_N_LOC (arb, 0);
  104. FETCH_STORE (old, *loc, SCM_LOCK_VAL);
  105. return scm_from_bool (old == SCM_UNLOCK_VAL);
  106. }
  107. #undef FUNC_NAME
  108. /* The atomic FETCH_STORE here is so two threads can't both see the arbiter
  109. locked and return #t. The arbiter itself wouldn't be corrupted by this,
  110. but we don't want two threads both thinking they were the unlocker. The
  111. intended usage is for the code which locked to be responsible for
  112. unlocking, but we guarantee the return value even if multiple threads
  113. compete. */
  114. SCM_DEFINE (scm_release_arbiter, "release-arbiter", 1, 0, 0,
  115. (SCM arb),
  116. "If @var{arb} is locked, then unlock it and return @code{#t}.\n"
  117. "If @var{arb} is already unlocked, then do nothing and return\n"
  118. "@code{#f}.\n"
  119. "\n"
  120. "Typical usage is for the thread which locked an arbiter to\n"
  121. "later release it, but that's not required, any thread can\n"
  122. "release it.")
  123. #define FUNC_NAME s_scm_release_arbiter
  124. {
  125. scm_t_bits old;
  126. scm_t_bits *loc;
  127. SCM_VALIDATE_SMOB (1, arb, arbiter);
  128. loc = (scm_t_bits*)SCM_SMOB_OBJECT_N_LOC (arb, 0);
  129. FETCH_STORE (old, *loc, SCM_UNLOCK_VAL);
  130. return scm_from_bool (old == SCM_LOCK_VAL);
  131. }
  132. #undef FUNC_NAME
  133. void
  134. scm_init_arbiters ()
  135. {
  136. scm_tc16_arbiter = scm_make_smob_type ("arbiter", 0);
  137. scm_set_smob_print (scm_tc16_arbiter, arbiter_print);
  138. #include "libguile/arbiters.x"
  139. }
  140. /*
  141. Local Variables:
  142. c-file-style: "gnu"
  143. End:
  144. */