global_atomic.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #ifndef __wasi__
  6. #error This example only compiles to WASM/WASI target
  7. #endif
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <assert.h>
  11. #include <stdbool.h>
  12. #include "wasi_thread_start.h"
  13. enum CONSTANTS {
  14. NUM_THREADS = 4,
  15. NUM_ITER = 1000,
  16. SECOND = 1000 * 1000 * 1000, /* 1 second */
  17. TIMEOUT = 10LL * SECOND
  18. };
  19. int g_count = 0;
  20. typedef struct {
  21. start_args_t base;
  22. int th_done;
  23. } shared_t;
  24. void
  25. __wasi_thread_start_C(int thread_id, int *start_arg)
  26. {
  27. shared_t *data = (shared_t *)start_arg;
  28. for (int i = 0; i < NUM_ITER; i++)
  29. __atomic_fetch_add(&g_count, 1, __ATOMIC_SEQ_CST);
  30. __atomic_store_n(&data->th_done, 1, __ATOMIC_SEQ_CST);
  31. __builtin_wasm_memory_atomic_notify(&data->th_done, 1);
  32. }
  33. int
  34. main(int argc, char **argv)
  35. {
  36. shared_t data[NUM_THREADS] = { 0 };
  37. int thread_ids[NUM_THREADS];
  38. for (int i = 0; i < NUM_THREADS; i++) {
  39. assert(start_args_init(&data[i].base));
  40. thread_ids[i] = __wasi_thread_spawn(&data[i]);
  41. ASSERT_VALID_TID(thread_ids[i]);
  42. }
  43. printf("Wait for threads to finish\n");
  44. for (int i = 0; i < NUM_THREADS; i++) {
  45. if (__builtin_wasm_memory_atomic_wait32(&data[i].th_done, 0, TIMEOUT)
  46. == 2) {
  47. assert(false && "Wait should not time out");
  48. }
  49. start_args_deinit(&data[i].base);
  50. }
  51. printf("Value of count after update: %d\n", g_count);
  52. assert(g_count == (NUM_THREADS * NUM_ITER)
  53. && "Global count not updated correctly");
  54. return EXIT_SUCCESS;
  55. }