sync_primitives.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdbool.h>
  6. /* Mutex */
  7. typedef int pthread_mutex_t;
  8. int
  9. pthread_mutex_init(pthread_mutex_t *mutex, void *unused)
  10. {
  11. *mutex = 0;
  12. return 0;
  13. }
  14. int
  15. pthread_mutex_destroy(pthread_mutex_t *mutex)
  16. {
  17. return 0;
  18. }
  19. static bool
  20. try_pthread_mutex_lock(pthread_mutex_t *mutex)
  21. {
  22. int expected = 0;
  23. return __atomic_compare_exchange_n(mutex, &expected, 1, false,
  24. __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
  25. }
  26. int
  27. pthread_mutex_lock(pthread_mutex_t *mutex)
  28. {
  29. while (!try_pthread_mutex_lock(mutex))
  30. __builtin_wasm_memory_atomic_wait32(mutex, 1, -1);
  31. return 0;
  32. }
  33. int
  34. pthread_mutex_unlock(pthread_mutex_t *mutex)
  35. {
  36. __atomic_store_n(mutex, 0, __ATOMIC_SEQ_CST);
  37. __builtin_wasm_memory_atomic_notify(mutex, 1);
  38. return 0;
  39. }
  40. /* Barrier */
  41. typedef struct {
  42. int count;
  43. int num_threads;
  44. int mutex;
  45. int ready;
  46. } pthread_barrier_t;
  47. int
  48. pthread_barrier_init(pthread_barrier_t *barrier, void *unused, int num_threads)
  49. {
  50. barrier->count = 0;
  51. barrier->num_threads = num_threads;
  52. barrier->ready = 0;
  53. pthread_mutex_init(&barrier->mutex, NULL);
  54. return 0;
  55. }
  56. int
  57. pthread_barrier_wait(pthread_barrier_t *barrier)
  58. {
  59. bool no_wait = false;
  60. int count;
  61. pthread_mutex_lock(&barrier->mutex);
  62. count = barrier->count++;
  63. if (barrier->count >= barrier->num_threads) {
  64. no_wait = true;
  65. barrier->count = 0;
  66. }
  67. pthread_mutex_unlock(&barrier->mutex);
  68. if (no_wait) {
  69. __atomic_store_n(&barrier->ready, 1, __ATOMIC_SEQ_CST);
  70. __builtin_wasm_memory_atomic_notify(&barrier->ready, count);
  71. return 0;
  72. }
  73. __builtin_wasm_memory_atomic_wait32(&barrier->ready, 0, -1);
  74. return 0;
  75. }