tid_allocator.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 "tid_allocator.h"
  6. #include "wasm_export.h"
  7. #include "bh_log.h"
  8. bh_static_assert(TID_MIN <= TID_MAX);
  9. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  10. bool
  11. tid_allocator_init(TidAllocator *tid_allocator)
  12. {
  13. tid_allocator->size = MIN(TID_ALLOCATOR_INIT_SIZE, TID_MAX - TID_MIN + 1);
  14. tid_allocator->pos = tid_allocator->size;
  15. tid_allocator->ids =
  16. wasm_runtime_malloc(tid_allocator->size * sizeof(int32));
  17. if (tid_allocator->ids == NULL)
  18. return false;
  19. for (int64 i = tid_allocator->pos - 1; i >= 0; i--)
  20. tid_allocator->ids[i] = TID_MIN + (tid_allocator->pos - 1 - i);
  21. return true;
  22. }
  23. void
  24. tid_allocator_deinit(TidAllocator *tid_allocator)
  25. {
  26. wasm_runtime_free(tid_allocator->ids);
  27. }
  28. int32
  29. tid_allocator_get_tid(TidAllocator *tid_allocator)
  30. {
  31. if (tid_allocator->pos == 0) { // Resize stack and push new thread ids
  32. if (tid_allocator->size == TID_MAX - TID_MIN + 1) {
  33. LOG_ERROR("Maximum thread identifier reached");
  34. return -1;
  35. }
  36. uint32 old_size = tid_allocator->size;
  37. uint32 new_size = MIN(tid_allocator->size * 2, TID_MAX - TID_MIN + 1);
  38. if (new_size != TID_MAX - TID_MIN + 1
  39. && new_size / 2 != tid_allocator->size) {
  40. LOG_ERROR("Overflow detected during new size calculation");
  41. return -1;
  42. }
  43. size_t realloc_size = new_size * sizeof(int32);
  44. if (realloc_size / sizeof(int32) != new_size) {
  45. LOG_ERROR("Overflow detected during realloc");
  46. return -1;
  47. }
  48. int32 *tmp = wasm_runtime_realloc(tid_allocator->ids, realloc_size);
  49. if (tmp == NULL) {
  50. LOG_ERROR("Thread ID allocator realloc failed");
  51. return -1;
  52. }
  53. tid_allocator->size = new_size;
  54. tid_allocator->pos = new_size - old_size;
  55. tid_allocator->ids = tmp;
  56. for (int64 i = tid_allocator->pos - 1; i >= 0; i--)
  57. tid_allocator->ids[i] = TID_MIN + (tid_allocator->size - 1 - i);
  58. }
  59. // Pop available thread identifier from the stack
  60. return tid_allocator->ids[--tid_allocator->pos];
  61. }
  62. void
  63. tid_allocator_release_tid(TidAllocator *tid_allocator, int32 thread_id)
  64. {
  65. // Release thread identifier by pushing it into the stack
  66. bh_assert(tid_allocator->pos < tid_allocator->size);
  67. tid_allocator->ids[tid_allocator->pos++] = thread_id;
  68. }