global_lock.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #if USE_CUSTOM_SYNC_PRIMITIVES != 0
  13. #include "sync_primitives.h"
  14. #else
  15. #include <pthread.h>
  16. #endif
  17. #include "wasi_thread_start.h"
  18. enum CONSTANTS {
  19. NUM_THREADS = 4,
  20. NUM_ITER = 200,
  21. SECOND = 1000 * 1000 * 1000, /* 1 second */
  22. TIMEOUT = 10LL * SECOND
  23. };
  24. pthread_mutex_t mutex;
  25. int g_count = 0;
  26. typedef struct {
  27. start_args_t base;
  28. int th_done;
  29. } shared_t;
  30. void
  31. __wasi_thread_start_C(int thread_id, int *start_arg)
  32. {
  33. shared_t *data = (shared_t *)start_arg;
  34. for (int i = 0; i < NUM_ITER; i++) {
  35. pthread_mutex_lock(&mutex);
  36. g_count++;
  37. pthread_mutex_unlock(&mutex);
  38. }
  39. __atomic_store_n(&data->th_done, 1, __ATOMIC_SEQ_CST);
  40. __builtin_wasm_memory_atomic_notify(&data->th_done, 1);
  41. }
  42. int
  43. main(int argc, char **argv)
  44. {
  45. shared_t data[NUM_THREADS] = { 0 };
  46. int thread_ids[NUM_THREADS];
  47. assert(pthread_mutex_init(&mutex, NULL) == 0 && "Failed to init mutex");
  48. for (int i = 0; i < NUM_THREADS; i++) {
  49. assert(start_args_init(&data[i].base));
  50. thread_ids[i] = __wasi_thread_spawn(&data[i]);
  51. ASSERT_VALID_TID(thread_ids[i]);
  52. }
  53. printf("Wait for threads to finish\n");
  54. for (int i = 0; i < NUM_THREADS; i++) {
  55. if (__builtin_wasm_memory_atomic_wait32(&data[i].th_done, 0, TIMEOUT)
  56. == 2) {
  57. assert(false && "Wait should not time out");
  58. }
  59. start_args_deinit(&data[i].base);
  60. }
  61. printf("Value of count after update: %d\n", g_count);
  62. assert(g_count == (NUM_THREADS * NUM_ITER)
  63. && "Global count not updated correctly");
  64. assert(pthread_mutex_destroy(&mutex) == 0 && "Failed to destroy mutex");
  65. return EXIT_SUCCESS;
  66. }