atomic.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright (C) 2016, 2018 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/validate.h"
  24. #include "libguile/atomics-internal.h"
  25. #include "libguile/atomic.h"
  26. SCM_DEFINE (scm_make_atomic_box, "make-atomic-box", 1, 0, 0,
  27. (SCM init),
  28. "Return an atomic box initialized to value @var{init}.")
  29. #define FUNC_NAME s_scm_make_atomic_box
  30. {
  31. SCM ret = scm_cell (scm_tc7_atomic_box, SCM_UNPACK (SCM_UNDEFINED));
  32. scm_atomic_box_set_x (ret, init);
  33. return ret;
  34. }
  35. #undef FUNC_NAME
  36. SCM_DEFINE (scm_atomic_box_p, "atomic-box?", 1, 0, 0,
  37. (SCM obj),
  38. "Return @code{#t} if @var{obj} is an atomic-box object, else\n"
  39. "return @code{#f}.")
  40. #define FUNC_NAME s_scm_atomic_box_p
  41. {
  42. return scm_from_bool (scm_is_atomic_box (obj));
  43. }
  44. #undef FUNC_NAME
  45. SCM_DEFINE (scm_atomic_box_ref, "atomic-box-ref", 1, 0, 0,
  46. (SCM box),
  47. "Fetch the value stored in the atomic box @var{box} and\n"
  48. "return it.")
  49. #define FUNC_NAME s_scm_atomic_box_ref
  50. {
  51. SCM_VALIDATE_ATOMIC_BOX (1, box);
  52. return scm_atomic_ref_scm (scm_atomic_box_loc (box));
  53. }
  54. #undef FUNC_NAME
  55. SCM_DEFINE (scm_atomic_box_set_x, "atomic-box-set!", 2, 0, 0,
  56. (SCM box, SCM val),
  57. "Store @var{val} into the atomic box @var{box}.")
  58. #define FUNC_NAME s_scm_atomic_box_set_x
  59. {
  60. SCM_VALIDATE_ATOMIC_BOX (1, box);
  61. scm_atomic_set_scm (scm_atomic_box_loc (box), val);
  62. return SCM_UNSPECIFIED;
  63. }
  64. #undef FUNC_NAME
  65. SCM_DEFINE (scm_atomic_box_swap_x, "atomic-box-swap!", 2, 0, 0,
  66. (SCM box, SCM val),
  67. "Store @var{val} into the atomic box @var{box},\n"
  68. "and return the value that was previously stored in\n"
  69. "the box.")
  70. #define FUNC_NAME s_scm_atomic_box_swap_x
  71. {
  72. SCM_VALIDATE_ATOMIC_BOX (1, box);
  73. return scm_atomic_swap_scm (scm_atomic_box_loc (box), val);
  74. }
  75. #undef FUNC_NAME
  76. SCM_DEFINE (scm_atomic_box_compare_and_swap_x,
  77. "atomic-box-compare-and-swap!", 3, 0, 0,
  78. (SCM box, SCM expected, SCM desired),
  79. "If the value of the atomic box @var{box} is the same as,\n"
  80. "@var{expected} (in the sense of @code{eq?}), replace the\n"
  81. "contents of the box with @var{desired}. Otherwise does not\n"
  82. "update the box. Returns the previous value of the box in\n"
  83. "either case, so you can know if the swap worked by checking\n"
  84. "if the return value is @code{eq?} to @var{expected}.")
  85. #define FUNC_NAME s_scm_atomic_box_compare_and_swap_x
  86. {
  87. SCM result = expected;
  88. SCM_VALIDATE_ATOMIC_BOX (1, box);
  89. while (!scm_atomic_compare_and_swap_scm (scm_atomic_box_loc (box),
  90. &result, desired)
  91. && scm_is_eq (result, expected))
  92. {
  93. /* 'scm_atomic_compare_and_swap_scm' has spuriously failed,
  94. i.e. it has returned 0 to indicate failure, although the
  95. observed value is 'eq?' to EXPECTED. In this case, we *must*
  96. try again, because the API of 'atomic-box-compare-and-swap!'
  97. provides no way to indicate to the caller that the exchange
  98. failed when the observed value is 'eq?' to EXPECTED. */
  99. }
  100. return result;
  101. }
  102. #undef FUNC_NAME
  103. void
  104. scm_i_atomic_box_print (SCM exp, SCM port, scm_print_state *pstate)
  105. {
  106. scm_puts ("#<atomic-box ", port);
  107. scm_uintprint (SCM_UNPACK (exp), 16, port);
  108. scm_puts (" value: ", port);
  109. scm_iprin1 (scm_atomic_box_ref (exp), port, pstate);
  110. scm_putc ('>', port);
  111. }
  112. static void
  113. scm_init_atomic (void)
  114. {
  115. #include "libguile/atomic.x"
  116. }
  117. void
  118. scm_register_atomic (void)
  119. {
  120. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  121. "scm_init_atomic",
  122. (scm_t_extension_init_func) scm_init_atomic,
  123. NULL);
  124. }