krillplayer-plugin 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/LICENSE +2 -1
  2. package/README.md +1 -0
  3. package/dist/cli.js +351 -73
  4. package/dist/crypto.js +37 -11
  5. package/dist/engine/engine.c +6 -3
  6. package/kernel/Sources/CQuickJS/include/kernel_qjs.h +8 -1
  7. package/kernel/Sources/CQuickJS/kernel_internal.h +54 -0
  8. package/kernel/Sources/CQuickJS/kernel_qjs.c +17 -29
  9. package/kernel/Sources/CQuickJS/kernel_wasm.c +1155 -0
  10. package/kernel/Sources/CWAMR/include/kernel_wamr.h +151 -0
  11. package/kernel/Sources/CWAMR/include/kernel_wamr_config.h +66 -0
  12. package/kernel/Sources/CWAMR/vendor/LICENSE +219 -0
  13. package/kernel/Sources/CWAMR/vendor/core/config.h +727 -0
  14. package/kernel/Sources/CWAMR/vendor/core/iwasm/aot/aot_runtime.h +899 -0
  15. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/arch/invokeNative_general.c +123 -0
  16. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_application.c +933 -0
  17. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_blocking_op.c +92 -0
  18. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api.c +5391 -0
  19. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api_internal.h +246 -0
  20. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.c +336 -0
  21. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.h +328 -0
  22. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.c +242 -0
  23. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.h +62 -0
  24. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.c +1999 -0
  25. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.h +154 -0
  26. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.c +1532 -0
  27. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.h +117 -0
  28. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.c +8070 -0
  29. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.h +1396 -0
  30. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.c +477 -0
  31. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.h +63 -0
  32. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_suspend_flags.h +52 -0
  33. package/kernel/Sources/CWAMR/vendor/core/iwasm/compilation/aot.h +366 -0
  34. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/lib_export.h +60 -0
  35. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_c_api.h +910 -0
  36. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_export.h +2440 -0
  37. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm.h +1492 -0
  38. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp.h +122 -0
  39. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp_fast.c +7998 -0
  40. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.c +16909 -0
  41. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.h +80 -0
  42. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_opcode.h +1043 -0
  43. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.c +5174 -0
  44. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.h +912 -0
  45. package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.c +1569 -0
  46. package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.h +256 -0
  47. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_alloc.c +1169 -0
  48. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.c +497 -0
  49. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.h +350 -0
  50. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc_internal.h +384 -0
  51. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_hmu.c +95 -0
  52. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_kfc.c +547 -0
  53. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.c +252 -0
  54. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.h +96 -0
  55. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/memory/mremap.c +12 -0
  56. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_blocking_op.c +70 -0
  57. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_malloc.c +72 -0
  58. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_memmap.c +302 -0
  59. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_sleep.c +20 -0
  60. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_thread.c +784 -0
  61. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_time.c +28 -0
  62. package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_init.c +43 -0
  63. package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_internal.h +126 -0
  64. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_extension.h +1676 -0
  65. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_vmcore.h +192 -0
  66. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_common.h +204 -0
  67. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_wasi_types.h +610 -0
  68. package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_init.c +43 -0
  69. package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_internal.h +139 -0
  70. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.c +25 -0
  71. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.h +42 -0
  72. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_atomic.h +300 -0
  73. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.c +27 -0
  74. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.h +114 -0
  75. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.c +217 -0
  76. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.h +83 -0
  77. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.c +339 -0
  78. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.h +168 -0
  79. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.c +77 -0
  80. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.h +30 -0
  81. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.c +111 -0
  82. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.h +109 -0
  83. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.c +111 -0
  84. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.h +94 -0
  85. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_platform.h +38 -0
  86. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.c +256 -0
  87. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.h +80 -0
  88. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.c +279 -0
  89. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.h +126 -0
  90. package/kernel/Sources/CWAMR/vendor/core/shared/utils/gnuc.h +14 -0
  91. package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.c +469 -0
  92. package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.h +51 -0
  93. package/kernel/Sources/CWAMR/vendor/core/version.h +24 -0
  94. package/kernel/Sources/CWAMR/wamr_bh_assert.c +98 -0
  95. package/kernel/Sources/CWAMR/wamr_bh_bitmap.c +98 -0
  96. package/kernel/Sources/CWAMR/wamr_bh_common.c +98 -0
  97. package/kernel/Sources/CWAMR/wamr_bh_hashmap.c +98 -0
  98. package/kernel/Sources/CWAMR/wamr_bh_leb128.c +98 -0
  99. package/kernel/Sources/CWAMR/wamr_bh_list.c +98 -0
  100. package/kernel/Sources/CWAMR/wamr_bh_log.c +98 -0
  101. package/kernel/Sources/CWAMR/wamr_bh_queue.c +98 -0
  102. package/kernel/Sources/CWAMR/wamr_bh_vector.c +98 -0
  103. package/kernel/Sources/CWAMR/wamr_ems_alloc.c +98 -0
  104. package/kernel/Sources/CWAMR/wamr_ems_gc.c +98 -0
  105. package/kernel/Sources/CWAMR/wamr_ems_hmu.c +98 -0
  106. package/kernel/Sources/CWAMR/wamr_ems_kfc.c +98 -0
  107. package/kernel/Sources/CWAMR/wamr_invokeNative_general.c +98 -0
  108. package/kernel/Sources/CWAMR/wamr_mem_alloc.c +98 -0
  109. package/kernel/Sources/CWAMR/wamr_mremap.c +98 -0
  110. package/kernel/Sources/CWAMR/wamr_platform_init_darwin.c +100 -0
  111. package/kernel/Sources/CWAMR/wamr_platform_init_linux.c +100 -0
  112. package/kernel/Sources/CWAMR/wamr_posix_blocking_op.c +98 -0
  113. package/kernel/Sources/CWAMR/wamr_posix_malloc.c +98 -0
  114. package/kernel/Sources/CWAMR/wamr_posix_memmap.c +98 -0
  115. package/kernel/Sources/CWAMR/wamr_posix_sleep.c +98 -0
  116. package/kernel/Sources/CWAMR/wamr_posix_thread.c +98 -0
  117. package/kernel/Sources/CWAMR/wamr_posix_time.c +98 -0
  118. package/kernel/Sources/CWAMR/wamr_runtime_timer.c +98 -0
  119. package/kernel/Sources/CWAMR/wamr_thread_manager.c +98 -0
  120. package/kernel/Sources/CWAMR/wamr_wasm_application.c +98 -0
  121. package/kernel/Sources/CWAMR/wamr_wasm_blocking_op.c +98 -0
  122. package/kernel/Sources/CWAMR/wamr_wasm_c_api.c +98 -0
  123. package/kernel/Sources/CWAMR/wamr_wasm_exec_env.c +98 -0
  124. package/kernel/Sources/CWAMR/wamr_wasm_interp_fast.c +98 -0
  125. package/kernel/Sources/CWAMR/wamr_wasm_loader.c +98 -0
  126. package/kernel/Sources/CWAMR/wamr_wasm_loader_common.c +98 -0
  127. package/kernel/Sources/CWAMR/wamr_wasm_memory.c +98 -0
  128. package/kernel/Sources/CWAMR/wamr_wasm_native.c +98 -0
  129. package/kernel/Sources/CWAMR/wamr_wasm_runtime.c +98 -0
  130. package/kernel/Sources/CWAMR/wamr_wasm_runtime_common.c +98 -0
  131. package/kernel/Sources/CWAMR/wamr_wasm_shared_memory.c +98 -0
  132. package/kernel/Sources/Kernel/PluginPrelude.js +5 -2
  133. package/kernel/Tests/KernelTests/CasePins.js +50 -0
  134. package/package.json +2 -2
  135. package/skill/krillplayer-plugin/SKILL.md +2 -0
  136. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-content-rejected.json +8 -4
  137. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-login-expired.json +8 -4
  138. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-muted.json +8 -4
  139. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-other.json +8 -4
  140. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-rate-limited.json +8 -4
  141. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-sent.json +8 -4
  142. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-logged-in.json +39 -0
  143. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-backup-line.json +8 -0
  144. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-line-gone.json +8 -0
  145. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live.json +8 -0
  146. package/skill/krillplayer-plugin/assets/samples/live-video-platform/plugin.ts +26 -9
  147. package/skill/krillplayer-plugin/references/cases.md +19 -1
  148. package/skill/krillplayer-plugin/references/contract.d.ts +104 -11
  149. package/skill/krillplayer-plugin/references/crypto-tools.md +104 -0
  150. package/skill/krillplayer-plugin/references/errors-and-fixes.md +5 -1
@@ -0,0 +1,126 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #ifndef _WASM_VECTOR_H
7
+ #define _WASM_VECTOR_H
8
+
9
+ #include "bh_platform.h"
10
+
11
+ #ifdef __cplusplus
12
+ extern "C" {
13
+ #endif
14
+
15
+ #define DEFAULT_VECTOR_INIT_SIZE 8
16
+
17
+ typedef struct Vector {
18
+ /* max element number */
19
+ size_t max_elems;
20
+ /* vector data allocated */
21
+ uint8 *data;
22
+ /* current element num */
23
+ size_t num_elems;
24
+ /* size of each element */
25
+ size_t size_elem;
26
+ void *lock;
27
+ } Vector;
28
+
29
+ /**
30
+ * Initialize vector
31
+ *
32
+ * @param vector the vector to init
33
+ * @param init_length the initial length of the vector
34
+ * @param size_elem size of each element
35
+ *
36
+ * @return true if success, false otherwise
37
+ */
38
+ bool
39
+ bh_vector_init(Vector *vector, size_t init_length, size_t size_elem,
40
+ bool use_lock);
41
+
42
+ /**
43
+ * Set element of vector
44
+ *
45
+ * @param vector the vector to set
46
+ * @param index the index of the element to set
47
+ * @param elem_buf the element buffer which stores the element data
48
+ *
49
+ * @return true if success, false otherwise
50
+ */
51
+ bool
52
+ bh_vector_set(Vector *vector, uint32 index, const void *elem_buf);
53
+
54
+ /**
55
+ * Get element of vector
56
+ *
57
+ * @param vector the vector to get
58
+ * @param index the index of the element to get
59
+ * @param elem_buf the element buffer to store the element data,
60
+ * whose length must be no less than element size
61
+ *
62
+ * @return true if success, false otherwise
63
+ */
64
+ bool
65
+ bh_vector_get(Vector *vector, uint32 index, void *elem_buf);
66
+
67
+ /**
68
+ * Insert element of vector
69
+ *
70
+ * @param vector the vector to insert
71
+ * @param index the index of the element to insert
72
+ * @param elem_buf the element buffer which stores the element data
73
+ *
74
+ * @return true if success, false otherwise
75
+ */
76
+ bool
77
+ bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf);
78
+
79
+ /**
80
+ * Append element to the end of vector
81
+ *
82
+ * @param vector the vector to append
83
+ * @param elem_buf the element buffer which stores the element data
84
+ *
85
+ * @return true if success, false otherwise
86
+ */
87
+ bool
88
+ bh_vector_append(Vector *vector, const void *elem_buf);
89
+
90
+ /**
91
+ * Remove element from vector
92
+ *
93
+ * @param vector the vector to remove element
94
+ * @param index the index of the element to remove
95
+ * @param old_elem_buf if not NULL, copies the element data to the buffer
96
+ *
97
+ * @return true if success, false otherwise
98
+ */
99
+ bool
100
+ bh_vector_remove(Vector *vector, uint32 index, void *old_elem_buf);
101
+
102
+ /**
103
+ * Return the size of the vector
104
+ *
105
+ * @param vector the vector to get size
106
+ *
107
+ * @return return the size of the vector
108
+ */
109
+ size_t
110
+ bh_vector_size(const Vector *vector);
111
+
112
+ /**
113
+ * Destroy the vector
114
+ *
115
+ * @param vector the vector to destroy
116
+ *
117
+ * @return true if success, false otherwise
118
+ */
119
+ bool
120
+ bh_vector_destroy(Vector *vector);
121
+
122
+ #ifdef __cplusplus
123
+ }
124
+ #endif
125
+
126
+ #endif /* endof _WASM_VECTOR_H */
@@ -0,0 +1,14 @@
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
+
6
+ #if !defined(__GNUC_PREREQ) && (defined(__GNUC__) || defined(__GNUG__)) \
7
+ && !defined(__clang__) && defined(__GNUC_MINOR__)
8
+ /* Depending on the platform the macro is defined in sys/features.h or
9
+ features.h Given the macro is simple, we re-implement it here instead of
10
+ dealing with two different paths.
11
+ */
12
+ #define __GNUC_PREREQ(maj, min) \
13
+ ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
14
+ #endif
@@ -0,0 +1,469 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #include "runtime_timer.h"
7
+
8
+ #if 1
9
+ #define PRINT(...) (void)0
10
+ #else
11
+ #define PRINT printf
12
+ #endif
13
+
14
+ typedef struct _app_timer {
15
+ struct _app_timer *next;
16
+ uint32 id;
17
+ uint32 interval;
18
+ uint64 expiry;
19
+ bool is_periodic;
20
+ } app_timer_t;
21
+
22
+ struct _timer_ctx {
23
+ app_timer_t *app_timers;
24
+ app_timer_t *idle_timers;
25
+ app_timer_t *free_timers;
26
+ uint32 max_timer_id;
27
+ int pre_allocated;
28
+ uint32 owner;
29
+
30
+ /* mutex and condition */
31
+ korp_cond cond;
32
+ korp_mutex mutex;
33
+
34
+ timer_callback_f timer_callback;
35
+ check_timer_expiry_f refresh_checker;
36
+ };
37
+
38
+ uint64
39
+ bh_get_tick_ms()
40
+ {
41
+ return os_time_get_boot_us() / 1000;
42
+ }
43
+
44
+ uint32
45
+ bh_get_elpased_ms(uint32 *last_system_clock)
46
+ {
47
+ uint32 elapsed_ms;
48
+ /* attention: the bh_get_tick_ms() returns a 64-bit integer, but
49
+ bh_get_elpased_ms() is designed to use a 32-bit clock count */
50
+ uint32 now = (uint32)bh_get_tick_ms();
51
+
52
+ /* system clock overrun */
53
+ if (now < *last_system_clock) {
54
+ PRINT("system clock overrun!\n");
55
+ elapsed_ms = now + (UINT32_MAX - *last_system_clock) + 1;
56
+ }
57
+ else {
58
+ elapsed_ms = now - *last_system_clock;
59
+ }
60
+
61
+ *last_system_clock = now;
62
+ return elapsed_ms;
63
+ }
64
+
65
+ static app_timer_t *
66
+ remove_timer_from(timer_ctx_t ctx, uint32 timer_id, bool active_list)
67
+ {
68
+ app_timer_t **head, *prev, *t;
69
+
70
+ os_mutex_lock(&ctx->mutex);
71
+
72
+ if (active_list)
73
+ head = &ctx->app_timers;
74
+ else
75
+ head = &ctx->idle_timers;
76
+
77
+ t = *head;
78
+ prev = NULL;
79
+
80
+ while (t) {
81
+ if (t->id == timer_id) {
82
+ if (prev == NULL) {
83
+ *head = t->next;
84
+ PRINT("removed timer [%d] at head from list %d\n", t->id,
85
+ active_list);
86
+ }
87
+ else {
88
+ prev->next = t->next;
89
+ PRINT("removed timer [%d] after [%d] from list %d\n", t->id,
90
+ prev->id, active_list);
91
+ }
92
+ os_mutex_unlock(&ctx->mutex);
93
+
94
+ if (active_list && prev == NULL && ctx->refresh_checker)
95
+ ctx->refresh_checker(ctx);
96
+ return t;
97
+ }
98
+ else {
99
+ prev = t;
100
+ t = t->next;
101
+ }
102
+ }
103
+
104
+ os_mutex_unlock(&ctx->mutex);
105
+ return NULL;
106
+ }
107
+
108
+ static app_timer_t *
109
+ remove_timer(timer_ctx_t ctx, uint32 timer_id, bool *active)
110
+ {
111
+ app_timer_t *t = remove_timer_from(ctx, timer_id, true);
112
+
113
+ if (t) {
114
+ if (active)
115
+ *active = true;
116
+ return t;
117
+ }
118
+
119
+ if (active)
120
+ *active = false;
121
+ return remove_timer_from(ctx, timer_id, false);
122
+ }
123
+
124
+ static void
125
+ reschedule_timer(timer_ctx_t ctx, app_timer_t *timer)
126
+ {
127
+ app_timer_t *t;
128
+ app_timer_t *prev = NULL;
129
+
130
+ os_mutex_lock(&ctx->mutex);
131
+
132
+ t = ctx->app_timers;
133
+ timer->next = NULL;
134
+ timer->expiry = bh_get_tick_ms() + timer->interval;
135
+
136
+ while (t) {
137
+ if (timer->expiry < t->expiry) {
138
+ if (prev == NULL) {
139
+ timer->next = ctx->app_timers;
140
+ ctx->app_timers = timer;
141
+ PRINT("rescheduled timer [%d] at head\n", timer->id);
142
+ }
143
+ else {
144
+ timer->next = t;
145
+ prev->next = timer;
146
+ PRINT("rescheduled timer [%d] after [%d]\n", timer->id,
147
+ prev->id);
148
+ }
149
+
150
+ goto out;
151
+ }
152
+ else {
153
+ prev = t;
154
+ t = t->next;
155
+ }
156
+ }
157
+
158
+ if (prev) {
159
+ /* insert to the list end */
160
+ prev->next = timer;
161
+ PRINT("rescheduled timer [%d] at end, after [%d]\n", timer->id,
162
+ prev->id);
163
+ }
164
+ else {
165
+ /* insert at the beginning */
166
+ bh_assert(ctx->app_timers == NULL);
167
+ ctx->app_timers = timer;
168
+ PRINT("rescheduled timer [%d] as first\n", timer->id);
169
+ }
170
+
171
+ out:
172
+ os_mutex_unlock(&ctx->mutex);
173
+
174
+ /* ensure the refresh_checker() is called out of the lock */
175
+ if (prev == NULL && ctx->refresh_checker)
176
+ ctx->refresh_checker(ctx);
177
+ }
178
+
179
+ static void
180
+ release_timer(timer_ctx_t ctx, app_timer_t *t)
181
+ {
182
+ if (ctx->pre_allocated) {
183
+ os_mutex_lock(&ctx->mutex);
184
+ t->next = ctx->free_timers;
185
+ ctx->free_timers = t;
186
+ PRINT("recycle timer :%d\n", t->id);
187
+ os_mutex_unlock(&ctx->mutex);
188
+ }
189
+ else {
190
+ PRINT("destroy timer :%d\n", t->id);
191
+ BH_FREE(t);
192
+ }
193
+ }
194
+
195
+ void
196
+ release_timer_list(app_timer_t **p_list)
197
+ {
198
+ app_timer_t *t = *p_list;
199
+
200
+ while (t) {
201
+ app_timer_t *next = t->next;
202
+ PRINT("destroy timer list:%d\n", t->id);
203
+ BH_FREE(t);
204
+ t = next;
205
+ }
206
+
207
+ *p_list = NULL;
208
+ }
209
+
210
+ /*
211
+ * API exposed
212
+ */
213
+
214
+ timer_ctx_t
215
+ create_timer_ctx(timer_callback_f timer_handler,
216
+ check_timer_expiry_f expiry_checker, int prealloc_num,
217
+ unsigned int owner)
218
+ {
219
+ timer_ctx_t ctx = (timer_ctx_t)BH_MALLOC(sizeof(struct _timer_ctx));
220
+
221
+ if (ctx == NULL)
222
+ return NULL;
223
+
224
+ memset(ctx, 0, sizeof(struct _timer_ctx));
225
+
226
+ ctx->timer_callback = timer_handler;
227
+ ctx->pre_allocated = prealloc_num;
228
+ ctx->refresh_checker = expiry_checker;
229
+ ctx->owner = owner;
230
+
231
+ while (prealloc_num > 0) {
232
+ app_timer_t *timer = (app_timer_t *)BH_MALLOC(sizeof(app_timer_t));
233
+
234
+ if (timer == NULL)
235
+ goto cleanup;
236
+
237
+ memset(timer, 0, sizeof(*timer));
238
+ timer->next = ctx->free_timers;
239
+ ctx->free_timers = timer;
240
+ prealloc_num--;
241
+ }
242
+
243
+ if (os_cond_init(&ctx->cond) != 0)
244
+ goto cleanup;
245
+
246
+ if (os_mutex_init(&ctx->mutex) != 0) {
247
+ os_cond_destroy(&ctx->cond);
248
+ goto cleanup;
249
+ }
250
+
251
+ PRINT("timer ctx created. pre-alloc: %d\n", ctx->pre_allocated);
252
+ return ctx;
253
+
254
+ cleanup:
255
+ if (ctx) {
256
+ release_timer_list(&ctx->free_timers);
257
+ BH_FREE(ctx);
258
+ }
259
+ PRINT("timer ctx create failed\n");
260
+ return NULL;
261
+ }
262
+
263
+ void
264
+ destroy_timer_ctx(timer_ctx_t ctx)
265
+ {
266
+ while (ctx->free_timers) {
267
+ void *tmp = ctx->free_timers;
268
+ ctx->free_timers = ctx->free_timers->next;
269
+ BH_FREE(tmp);
270
+ }
271
+
272
+ cleanup_app_timers(ctx);
273
+
274
+ os_cond_destroy(&ctx->cond);
275
+ os_mutex_destroy(&ctx->mutex);
276
+ BH_FREE(ctx);
277
+ }
278
+
279
+ unsigned int
280
+ timer_ctx_get_owner(timer_ctx_t ctx)
281
+ {
282
+ return ctx->owner;
283
+ }
284
+
285
+ void
286
+ add_idle_timer(timer_ctx_t ctx, app_timer_t *timer)
287
+ {
288
+ os_mutex_lock(&ctx->mutex);
289
+ timer->next = ctx->idle_timers;
290
+ ctx->idle_timers = timer;
291
+ os_mutex_unlock(&ctx->mutex);
292
+ }
293
+
294
+ uint32
295
+ sys_create_timer(timer_ctx_t ctx, int interval, bool is_period, bool auto_start)
296
+ {
297
+ app_timer_t *timer;
298
+
299
+ if (ctx->pre_allocated) {
300
+ if (ctx->free_timers == NULL) {
301
+ return (uint32)-1;
302
+ }
303
+ else {
304
+ timer = ctx->free_timers;
305
+ ctx->free_timers = timer->next;
306
+ }
307
+ }
308
+ else {
309
+ timer = (app_timer_t *)BH_MALLOC(sizeof(app_timer_t));
310
+ if (timer == NULL)
311
+ return (uint32)-1;
312
+ }
313
+
314
+ memset(timer, 0, sizeof(*timer));
315
+
316
+ ctx->max_timer_id++;
317
+ if (ctx->max_timer_id == (uint32)-1)
318
+ ctx->max_timer_id++;
319
+ timer->id = ctx->max_timer_id;
320
+ timer->interval = (uint32)interval;
321
+ timer->is_periodic = is_period;
322
+
323
+ if (auto_start)
324
+ reschedule_timer(ctx, timer);
325
+ else
326
+ add_idle_timer(ctx, timer);
327
+
328
+ return timer->id;
329
+ }
330
+
331
+ bool
332
+ sys_timer_cancel(timer_ctx_t ctx, uint32 timer_id)
333
+ {
334
+ bool from_active;
335
+ app_timer_t *t = remove_timer(ctx, timer_id, &from_active);
336
+
337
+ if (t == NULL)
338
+ return false;
339
+
340
+ add_idle_timer(ctx, t);
341
+
342
+ PRINT("sys_timer_stop called\n");
343
+ return from_active;
344
+ }
345
+
346
+ bool
347
+ sys_timer_destroy(timer_ctx_t ctx, uint32 timer_id)
348
+ {
349
+ bool from_active;
350
+ app_timer_t *t = remove_timer(ctx, timer_id, &from_active);
351
+
352
+ if (t == NULL)
353
+ return false;
354
+
355
+ release_timer(ctx, t);
356
+
357
+ PRINT("sys_timer_destroy called\n");
358
+ return true;
359
+ }
360
+
361
+ bool
362
+ sys_timer_restart(timer_ctx_t ctx, uint32 timer_id, int interval)
363
+ {
364
+ app_timer_t *t = remove_timer(ctx, timer_id, NULL);
365
+
366
+ if (t == NULL)
367
+ return false;
368
+
369
+ t->interval = (uint32)interval;
370
+
371
+ reschedule_timer(ctx, t);
372
+
373
+ PRINT("sys_timer_restart called\n");
374
+ return true;
375
+ }
376
+
377
+ /*
378
+ * API called by the timer manager from another thread or the kernel timer
379
+ * handler
380
+ */
381
+
382
+ /**
383
+ * lookup the app queue by the module name
384
+ * post a timeout message to the app queue
385
+ */
386
+ static void
387
+ handle_expired_timers(timer_ctx_t ctx, app_timer_t *expired)
388
+ {
389
+ while (expired) {
390
+ app_timer_t *t = expired;
391
+ ctx->timer_callback(t->id, ctx->owner);
392
+
393
+ /* get next expired timer first, since the following
394
+ operation may change expired->next */
395
+ expired = expired->next;
396
+ if (t->is_periodic) {
397
+ /* if it is repeating, then reschedule it */
398
+ reschedule_timer(ctx, t);
399
+ }
400
+ else {
401
+ /* else move it to idle list */
402
+ add_idle_timer(ctx, t);
403
+ }
404
+ }
405
+ }
406
+
407
+ uint32
408
+ get_expiry_ms(timer_ctx_t ctx)
409
+ {
410
+ uint32 ms_to_next_expiry;
411
+ uint64 now = bh_get_tick_ms();
412
+
413
+ os_mutex_lock(&ctx->mutex);
414
+ if (ctx->app_timers == NULL)
415
+ ms_to_next_expiry = (uint32)-1;
416
+ else if (ctx->app_timers->expiry >= now)
417
+ ms_to_next_expiry = (uint32)(ctx->app_timers->expiry - now);
418
+ else
419
+ ms_to_next_expiry = 0;
420
+ os_mutex_unlock(&ctx->mutex);
421
+
422
+ return ms_to_next_expiry;
423
+ }
424
+
425
+ uint32
426
+ check_app_timers(timer_ctx_t ctx)
427
+ {
428
+ app_timer_t *t, *expired = NULL, *expired_end = NULL;
429
+ uint64 now = bh_get_tick_ms();
430
+
431
+ os_mutex_lock(&ctx->mutex);
432
+
433
+ t = ctx->app_timers;
434
+ while (t) {
435
+ if (now >= t->expiry) {
436
+ ctx->app_timers = t->next;
437
+
438
+ /* append t to the end of expired list */
439
+ t->next = NULL;
440
+ if (!expired_end) {
441
+ expired = expired_end = t;
442
+ }
443
+ else {
444
+ expired_end->next = t;
445
+ expired_end = t;
446
+ }
447
+
448
+ t = ctx->app_timers;
449
+ }
450
+ else {
451
+ break;
452
+ }
453
+ }
454
+ os_mutex_unlock(&ctx->mutex);
455
+
456
+ handle_expired_timers(ctx, expired);
457
+ return get_expiry_ms(ctx);
458
+ }
459
+
460
+ void
461
+ cleanup_app_timers(timer_ctx_t ctx)
462
+ {
463
+ os_mutex_lock(&ctx->mutex);
464
+
465
+ release_timer_list(&ctx->app_timers);
466
+ release_timer_list(&ctx->idle_timers);
467
+
468
+ os_mutex_unlock(&ctx->mutex);
469
+ }
@@ -0,0 +1,51 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #ifndef LIB_BASE_RUNTIME_TIMER_H_
7
+ #define LIB_BASE_RUNTIME_TIMER_H_
8
+
9
+ #include "bh_platform.h"
10
+
11
+ #ifdef __cplusplus
12
+ extern "C" {
13
+ #endif
14
+
15
+ uint64
16
+ bh_get_tick_ms(void);
17
+ uint32
18
+ bh_get_elpased_ms(uint32 *last_system_clock);
19
+
20
+ struct _timer_ctx;
21
+ typedef struct _timer_ctx *timer_ctx_t;
22
+ typedef void (*timer_callback_f)(unsigned int id, unsigned int owner);
23
+ typedef void (*check_timer_expiry_f)(timer_ctx_t ctx);
24
+
25
+ timer_ctx_t
26
+ create_timer_ctx(timer_callback_f timer_handler, check_timer_expiry_f,
27
+ int prealloc_num, unsigned int owner);
28
+ void destroy_timer_ctx(timer_ctx_t);
29
+ unsigned int
30
+ timer_ctx_get_owner(timer_ctx_t ctx);
31
+
32
+ uint32
33
+ sys_create_timer(timer_ctx_t ctx, int interval, bool is_period,
34
+ bool auto_start);
35
+ bool
36
+ sys_timer_destroy(timer_ctx_t ctx, uint32 timer_id);
37
+ bool
38
+ sys_timer_cancel(timer_ctx_t ctx, uint32 timer_id);
39
+ bool
40
+ sys_timer_restart(timer_ctx_t ctx, uint32 timer_id, int interval);
41
+ void
42
+ cleanup_app_timers(timer_ctx_t ctx);
43
+ uint32
44
+ check_app_timers(timer_ctx_t ctx);
45
+ uint32
46
+ get_expiry_ms(timer_ctx_t ctx);
47
+
48
+ #ifdef __cplusplus
49
+ }
50
+ #endif
51
+ #endif /* LIB_BASE_RUNTIME_TIMER_H_ */
@@ -0,0 +1,24 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ /*
7
+ * version.h.in is a template file. version.h is a generated file.
8
+ * Please do not edit both files directly.
9
+ *
10
+ * Any changes to the version should be done in build-scripts/version.cmake.
11
+ *
12
+ * Continue to maintain the version.h for certain embedded platforms.
13
+ */
14
+
15
+ #ifndef _WAMR_VERSION_H_
16
+ #define _WAMR_VERSION_H_
17
+
18
+ /* clang-format off */
19
+ #define WAMR_VERSION_MAJOR 2
20
+ #define WAMR_VERSION_MINOR 4
21
+ #define WAMR_VERSION_PATCH 6
22
+ /* clang-format on */
23
+
24
+ #endif