Skip to content

Commit 541f577

Browse files
authored
Implement wasm_runtime_init_thread_env for Windows platform (#683)
Implement wasm_runtime_init_thread_env() for Windows platform by calling os_thread_env_init(): if current thread is created by developer himself but not runtime, developer should call wasm_runtime_init_thread_env() to init the thread environment before calling wasm function, and call wasm_runtime_destroy_thread_env() before thread exits. And clear compile warnings for Windows platform, fix compile error for AliOS-Things platform Signed-off-by: Wenyong Huang <[email protected]>
1 parent 445722c commit 541f577

File tree

9 files changed

+115
-28
lines changed

9 files changed

+115
-28
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
13241324
uint16 param_count = func_type->param_count;
13251325
uint16 result_count = func_type->result_count;
13261326
const uint8 *types = func_type->types;
1327-
#if BH_PLATFORM_WINDOWS
1327+
#ifdef BH_PLATFORM_WINDOWS
13281328
const char *exce;
13291329
int result;
13301330
#endif

core/iwasm/common/wasm_runtime_common.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,9 +823,19 @@ wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env)
823823
bool
824824
wasm_runtime_init_thread_env()
825825
{
826+
#ifdef BH_PLATFORM_WINDOWS
827+
if (os_thread_env_init() != 0)
828+
return false;
829+
#endif
830+
826831
#if WASM_ENABLE_AOT != 0
827832
#ifdef OS_ENABLE_HW_BOUND_CHECK
828-
return aot_signal_init();
833+
if (!aot_signal_init()) {
834+
#ifdef BH_PLATFORM_WINDOWS
835+
os_thread_env_destroy();
836+
#endif
837+
return false;
838+
}
829839
#endif
830840
#endif
831841
return true;
@@ -839,6 +849,10 @@ wasm_runtime_destroy_thread_env()
839849
aot_signal_destroy();
840850
#endif
841851
#endif
852+
853+
#ifdef BH_PLATFORM_WINDOWS
854+
os_thread_env_destroy();
855+
#endif
842856
}
843857

844858
#if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)

core/iwasm/compilation/aot_compiler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ read_leb(const uint8 *buf, const uint8 *buf_end,
6666
}
6767
if (sign && (shift < maxbits) && (byte & 0x40)) {
6868
/* Sign extend */
69-
result |= (uint64)(- (((uint64)1) << shift));
69+
result |= (~((uint64)0)) << shift;
7070
}
7171
*p_result = result;
7272
return true;

core/iwasm/interpreter/wasm_interp_classic.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ rotl32(uint32 n, uint32 c)
5050
const uint32 mask = (31);
5151
c = c % 32;
5252
c &= mask;
53-
return (n<<c) | (n>>( (-c)&mask ));
53+
return (n << c) | (n >> ((0 - c) & mask));
5454
}
5555

5656
static inline uint32
@@ -59,7 +59,7 @@ rotr32(uint32 n, uint32 c)
5959
const uint32 mask = (31);
6060
c = c % 32;
6161
c &= mask;
62-
return (n>>c) | (n<<( (-c)&mask ));
62+
return (n >> c) | (n << ((0 - c) & mask));
6363
}
6464

6565
static inline uint64
@@ -68,7 +68,7 @@ rotl64(uint64 n, uint64 c)
6868
const uint64 mask = (63);
6969
c = c % 64;
7070
c &= mask;
71-
return (n<<c) | (n>>( (-c)&mask ));
71+
return (n << c) | (n >> ((0 - c) & mask));
7272
}
7373

7474
static inline uint64
@@ -77,7 +77,7 @@ rotr64(uint64 n, uint64 c)
7777
const uint64 mask = (63);
7878
c = c % 64;
7979
c &= mask;
80-
return (n>>c) | (n<<( (-c)&mask ));
80+
return (n >> c) | (n << ((0 - c) & mask));
8181
}
8282

8383
static inline double
@@ -2623,7 +2623,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
26232623
goto out_of_bounds;
26242624

26252625
bh_memcpy_s(maddr, linear_mem_size - addr,
2626-
data + offset, bytes);
2626+
data + offset, (uint32)bytes);
26272627
break;
26282628
}
26292629
case WASM_OP_DATA_DROP:
@@ -2717,9 +2717,9 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
27172717
bh_memcpy_s(
27182718
(uint8 *)(tbl_inst)
27192719
+ offsetof(WASMTableInstance, base_addr) + d * sizeof(uint32),
2720-
(tbl_inst->cur_size - d) * sizeof(uint32),
2720+
(uint32)((tbl_inst->cur_size - d) * sizeof(uint32)),
27212721
module->module->table_segments[elem_idx].func_indexes + s,
2722-
n * sizeof(uint32));
2722+
(uint32)(n * sizeof(uint32)));
27232723

27242724
break;
27252725
}
@@ -2764,10 +2764,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
27642764
bh_memmove_s(
27652765
(uint8 *)(dst_tbl_inst) + offsetof(WASMTableInstance, base_addr)
27662766
+ d * sizeof(uint32),
2767-
(dst_tbl_inst->cur_size - d) * sizeof(uint32),
2767+
(uint32)((dst_tbl_inst->cur_size - d) * sizeof(uint32)),
27682768
(uint8 *)(src_tbl_inst) + offsetof(WASMTableInstance, base_addr)
27692769
+ s * sizeof(uint32),
2770-
n * sizeof(uint32));
2770+
(uint32)(n * sizeof(uint32)));
27712771
break;
27722772
}
27732773
case WASM_OP_TABLE_GROW:

core/iwasm/interpreter/wasm_interp_fast.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ rotl32(uint32 n, uint32 c)
4848
const uint32 mask = (31);
4949
c = c % 32;
5050
c &= mask;
51-
return (n<<c) | (n>>( (-c)&mask ));
51+
return (n << c) | (n >> ((0 - c) & mask));
5252
}
5353

5454
static inline uint32
@@ -57,7 +57,7 @@ rotr32(uint32 n, uint32 c)
5757
const uint32 mask = (31);
5858
c = c % 32;
5959
c &= mask;
60-
return (n>>c) | (n<<( (-c)&mask ));
60+
return (n >> c) | (n << ((0 - c) & mask));
6161
}
6262

6363
static inline uint64
@@ -66,7 +66,7 @@ rotl64(uint64 n, uint64 c)
6666
const uint64 mask = (63);
6767
c = c % 64;
6868
c &= mask;
69-
return (n<<c) | (n>>( (-c)&mask ));
69+
return (n << c) | (n >> ((0 - c) & mask));
7070
}
7171

7272
static inline uint64
@@ -75,7 +75,7 @@ rotr64(uint64 n, uint64 c)
7575
const uint64 mask = (63);
7676
c = c % 64;
7777
c &= mask;
78-
return (n>>c) | (n<<( (-c)&mask ));
78+
return (n >> c) | (n << ((0 - c) & mask));
7979
}
8080

8181
static inline double
@@ -455,7 +455,8 @@ LOAD_PTR(void *addr)
455455

456456
#define DEF_OP_MATH(src_type, src_op_type, method) do { \
457457
SET_OPERAND(src_op_type, 2, \
458-
method(GET_OPERAND(src_type, src_op_type, 0))); \
458+
(src_type)method(GET_OPERAND(src_type, \
459+
src_op_type, 0))); \
459460
frame_ip += 4; \
460461
} while (0)
461462

@@ -2229,7 +2230,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
22292230
else if (isnan(b))
22302231
*(float32*)(frame_lp + GET_OFFSET()) = b;
22312232
else
2232-
*(float32*)(frame_lp + GET_OFFSET()) = wa_fmin(a, b);
2233+
*(float32*)(frame_lp + GET_OFFSET()) = (float32)wa_fmin(a, b);
22332234
HANDLE_OP_END ();
22342235
}
22352236

@@ -2245,7 +2246,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
22452246
else if (isnan(b))
22462247
*(float32*)(frame_lp + GET_OFFSET()) = b;
22472248
else
2248-
*(float32*)(frame_lp + GET_OFFSET()) = wa_fmax(a, b);
2249+
*(float32*)(frame_lp + GET_OFFSET()) = (float32)wa_fmax(a, b);
22492250
HANDLE_OP_END ();
22502251
}
22512252

@@ -2255,7 +2256,8 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
22552256

22562257
b = *(float32*)(frame_lp + GET_OFFSET());
22572258
a = *(float32*)(frame_lp + GET_OFFSET());
2258-
*(float32*)(frame_lp + GET_OFFSET()) = (signbit(b) ? -fabs(a) : fabs(a));
2259+
*(float32*)(frame_lp + GET_OFFSET()) =
2260+
(float32)(signbit(b) ? -fabs(a) : fabs(a));
22592261
HANDLE_OP_END ();
22602262
}
22612263

@@ -2606,7 +2608,8 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
26062608
if (offset + bytes > seg_len)
26072609
goto out_of_bounds;
26082610

2609-
bh_memcpy_s(maddr, linear_mem_size - addr, data + offset, bytes);
2611+
bh_memcpy_s(maddr, linear_mem_size - addr,
2612+
data + offset, (uint32)bytes);
26102613
break;
26112614
}
26122615
case WASM_OP_DATA_DROP:
@@ -2695,9 +2698,9 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
26952698
bh_memcpy_s(
26962699
(uint8 *)tbl_inst + offsetof(WASMTableInstance, base_addr)
26972700
+ d * sizeof(uint32),
2698-
(tbl_inst->cur_size - d) * sizeof(uint32),
2701+
(uint32)((tbl_inst->cur_size - d) * sizeof(uint32)),
26992702
module->module->table_segments[elem_idx].func_indexes + s,
2700-
n * sizeof(uint32));
2703+
(uint32)(n * sizeof(uint32)));
27012704
break;
27022705
}
27032706
case WASM_OP_ELEM_DROP:
@@ -2740,10 +2743,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
27402743
bh_memmove_s(
27412744
(uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, base_addr)
27422745
+ d * sizeof(uint32),
2743-
(dst_tbl_inst->cur_size - d) * sizeof(uint32),
2746+
(uint32)((dst_tbl_inst->cur_size - d) * sizeof(uint32)),
27442747
(uint8 *)src_tbl_inst
27452748
+ offsetof(WASMTableInstance, base_addr) + s * sizeof(uint32),
2746-
n * sizeof(uint32));
2749+
(uint32)(n * sizeof(uint32)));
27472750
break;
27482751
}
27492752
case WASM_OP_TABLE_GROW:

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,14 +2485,14 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
24852485
/* place holder, will overwrite it in wasm_c_api */
24862486
frame.instance = module_inst;
24872487
frame.module_offset = 0;
2488-
frame.func_index = func_inst - module_inst->functions;
2488+
frame.func_index = (uint32)(func_inst - module_inst->functions);
24892489

24902490
func_code_base = wasm_get_func_code(func_inst);
24912491
if (!cur_frame->ip || !func_code_base) {
24922492
frame.func_offset = 0;
24932493
}
24942494
else {
2495-
frame.func_offset = cur_frame->ip - func_code_base;
2495+
frame.func_offset = (uint32)(cur_frame->ip - func_code_base);
24962496
}
24972497

24982498
/* look for the function name */

core/shared/platform/include/platform_api_extension.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,24 @@ int os_thread_detach(korp_tid);
8484
*/
8585
void os_thread_exit(void *retval);
8686

87+
/**
88+
* Initialize current thread environment if current thread
89+
* is created by developer but not runtime
90+
*
91+
* @return 0 if success, -1 otherwise
92+
*/
93+
int os_thread_env_init();
94+
95+
/**
96+
* Destroy current thread environment
97+
*/
98+
void os_thread_env_destroy();
99+
87100
/**
88101
* Suspend execution of the calling thread for (at least)
89102
* usec microseconds
90103
*
91-
* @param return 0 if success, -1 otherwise
104+
* @return 0 if success, -1 otherwise
92105
*/
93106
int os_usleep(uint32 usec);
94107

core/shared/platform/windows/win_thread.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,62 @@ os_thread_exit(void *retval)
288288
_endthreadex(0);
289289
}
290290

291+
int
292+
os_thread_env_init()
293+
{
294+
os_thread_data *thread_data = TlsGetValue(thread_data_key);
295+
296+
if (thread_data)
297+
/* Already created */
298+
return BHT_OK;
299+
300+
if (!(thread_data = BH_MALLOC(sizeof(os_thread_data))))
301+
return BHT_ERROR;
302+
303+
memset(thread_data, 0, sizeof(os_thread_data));
304+
thread_data->thread_id = GetCurrentThreadId();
305+
306+
if (os_sem_init(&thread_data->wait_node.sem) != BHT_OK)
307+
goto fail1;
308+
309+
if (os_mutex_init(&thread_data->wait_lock) != BHT_OK)
310+
goto fail2;
311+
312+
if (os_cond_init(&thread_data->wait_cond) != BHT_OK)
313+
goto fail3;
314+
315+
if (!TlsSetValue(thread_data_key, thread_data))
316+
goto fail4;
317+
318+
return BHT_OK;
319+
320+
fail4:
321+
os_cond_destroy(&thread_data->wait_cond);
322+
fail3:
323+
os_mutex_destroy(&thread_data->wait_lock);
324+
fail2:
325+
os_sem_destroy(&thread_data->wait_node.sem);
326+
fail1:
327+
BH_FREE(thread_data);
328+
return BHT_ERROR;
329+
}
330+
331+
void
332+
os_thread_env_destroy()
333+
{
334+
os_thread_data *thread_data = TlsGetValue(thread_data_key);
335+
336+
/* Note that supervisor_thread_data's resources will be destroyed
337+
by os_thread_sys_destroy() */
338+
if (thread_data && thread_data != &supervisor_thread_data) {
339+
TlsSetValue(thread_data_key, NULL);
340+
os_cond_destroy(&thread_data->wait_cond);
341+
os_mutex_destroy(&thread_data->wait_lock);
342+
os_sem_destroy(&thread_data->wait_node.sem);
343+
BH_FREE(thread_data);
344+
}
345+
}
346+
291347
int
292348
os_sem_init(korp_sem *sem)
293349
{

product-mini/platforms/alios-things/aos.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ $(NAME)_SOURCES := ${SHARED_ROOT}/platform/alios/alios_platform.c \
9999
${IWASM_ROOT}/common/wasm_native.c \
100100
${IWASM_ROOT}/common/wasm_exec_env.c \
101101
${IWASM_ROOT}/common/wasm_memory.c \
102+
${IWASM_ROOT}/common/wasm_c_api.c \
102103
${IWASM_ROOT}/common/arch/${INVOKE_NATIVE} \
103104
src/main.c
104105

0 commit comments

Comments
 (0)