WebAssembly Micro Runtime first version
This commit is contained in:
31
core/shared-lib/platform/CMakeLists.txt
Executable file
31
core/shared-lib/platform/CMakeLists.txt
Executable file
@ -0,0 +1,31 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
include_directories (./include ../include ./${PLATFORM})
|
||||
|
||||
add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199309L -D_BSD_SOURCE)
|
||||
|
||||
file (GLOB_RECURSE source_all ${PLATFORM}/*.c)
|
||||
add_library (supportlib ${source_all})
|
||||
|
||||
target_link_libraries (supportlib -pthread -lrt)
|
||||
|
||||
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
|
||||
add_library (supportlib_ut ${source_all})
|
||||
|
||||
set_target_properties (supportlib_ut PROPERTIES COMPILE_DEFINITIONS BH_TEST=1)
|
||||
|
||||
target_link_libraries (supportlib_ut -pthread -lrt)
|
||||
endif ()
|
||||
|
||||
15
core/shared-lib/platform/Makefile
Normal file
15
core/shared-lib/platform/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
obj-y += zephyr/
|
||||
69
core/shared-lib/platform/android/bh_assert.c
Executable file
69
core/shared-lib/platform/android/bh_assert.c
Executable file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "bh_assert.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef BH_TEST
|
||||
#include <setjmp.h>
|
||||
#endif
|
||||
|
||||
#ifdef BH_TEST
|
||||
/* for exception throwing */
|
||||
jmp_buf bh_test_jb;
|
||||
#endif
|
||||
|
||||
void bh_assert_internal(int v, const char *file_name, int line_number,
|
||||
const char *expr_string)
|
||||
{
|
||||
if (v)
|
||||
return;
|
||||
|
||||
if (!file_name)
|
||||
file_name = "NULL FILENAME";
|
||||
if (!expr_string)
|
||||
expr_string = "NULL EXPR_STRING";
|
||||
|
||||
printf("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
|
||||
file_name, line_number);
|
||||
|
||||
#ifdef BH_TEST
|
||||
longjmp(bh_test_jb, 1);
|
||||
#endif
|
||||
|
||||
abort();
|
||||
}
|
||||
|
||||
void bh_debug_internal(const char *file_name, int line_number, const char *fmt,
|
||||
...)
|
||||
{
|
||||
#ifndef JEFF_TEST_VERIFIER
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
bh_assert(file_name);
|
||||
|
||||
printf("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
|
||||
vprintf(fmt, args);
|
||||
|
||||
va_end(args);
|
||||
printf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
81
core/shared-lib/platform/android/bh_definition.c
Executable file
81
core/shared-lib/platform/android/bh_definition.c
Executable file
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_definition.h"
|
||||
#include "bh_platform.h"
|
||||
|
||||
int bh_return(int ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define RSIZE_MAX 0x7FFFFFFF
|
||||
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
|
||||
{
|
||||
char *dest = (char*) s1;
|
||||
char *src = (char*) s2;
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (s1 == NULL || s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
if (s2 == NULL || n > s1max) {
|
||||
memset(dest, 0, s1max);
|
||||
return -1;
|
||||
}
|
||||
memcpy(dest, src, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int b_strcat_s(char * s1, size_t s1max, const char * s2)
|
||||
{
|
||||
if (NULL
|
||||
== s1|| NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1) || s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
strcat(s1, s2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int b_strcpy_s(char * s1, size_t s1max, const char * s2)
|
||||
{
|
||||
if (NULL
|
||||
== s1|| NULL == s2 || s1max < (strlen(s2) + 1) || s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
strcpy(s1, s2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fopen_s(FILE ** pFile, const char *filename, const char *mode)
|
||||
{
|
||||
if (NULL == pFile || NULL == filename || NULL == mode) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
*pFile = fopen(filename, mode);
|
||||
|
||||
if (NULL == *pFile)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
117
core/shared-lib/platform/android/bh_platform.h
Executable file
117
core/shared-lib/platform/android/bh_platform.h
Executable file
@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _BH_PLATFORM_H
|
||||
#define _BH_PLATFORM_H
|
||||
|
||||
#include "bh_config.h"
|
||||
#include "bh_types.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
typedef uint64_t uint64;
|
||||
typedef int64_t int64;
|
||||
|
||||
extern void DEBUGME(void);
|
||||
|
||||
#define DIE do{bh_debug("Die here\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); DEBUGME(void); while(1);}while(0)
|
||||
|
||||
#define BH_PLATFORM "Linux"
|
||||
|
||||
/* NEED qsort */
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <ctype.h>
|
||||
#include <pthread.h>
|
||||
#include <limits.h>
|
||||
#include <semaphore.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define _STACK_SIZE_ADJUSTMENT (32 * 1024)
|
||||
|
||||
/* Stack size of applet manager thread. */
|
||||
#define BH_APPLET_MANAGER_THREAD_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Stack size of HMC thread. */
|
||||
#define BH_HMC_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Stack size of watchdog thread. */
|
||||
#define BH_WATCHDOG_THREAD_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Stack size of applet threads's native part. */
|
||||
#define BH_APPLET_PRESERVED_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Stack size of remote invoke listen thread. */
|
||||
#define BH_REMOTE_INVOKE_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Stack size of remote post listen thread. */
|
||||
#define BH_REMOTE_POST_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Maximal recursion depth of interpreter. */
|
||||
#define BH_MAX_INTERP_RECURSION_DEPTH 8
|
||||
|
||||
#define BH_ROUTINE_MODIFIER
|
||||
#define BHT_TIMEDOUT ETIMEDOUT
|
||||
|
||||
#define INVALID_THREAD_ID 0xFFffFFff
|
||||
#define INVALID_SEM_ID SEM_FAILED
|
||||
|
||||
typedef pthread_t korp_tid;
|
||||
typedef pthread_mutex_t korp_mutex;
|
||||
typedef sem_t korp_sem;
|
||||
typedef pthread_cond_t korp_cond;
|
||||
typedef void* (*thread_start_routine_t)(void*);
|
||||
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
/* The following operations declared in string.h may be defined as
|
||||
macros on Linux, so don't declare them as functions here. */
|
||||
/* memset */
|
||||
/* memcpy */
|
||||
/* memmove */
|
||||
|
||||
/* #include <stdio.h> */
|
||||
|
||||
/* Unit test framework is based on C++, where the declaration of
|
||||
snprintf is different. */
|
||||
#ifndef __cplusplus
|
||||
int snprintf(char *buffer, size_t count, const char *format, ...);
|
||||
#endif
|
||||
|
||||
/* #include <math.h> */
|
||||
|
||||
double fmod(double x, double y);
|
||||
float fmodf(float x, float y);
|
||||
|
||||
/* Definitions for applet debugging */
|
||||
#define APPLET_DEBUG_LISTEN_PORT 8000
|
||||
#define BH_SOCKET_INVALID_SOCK -1
|
||||
#define BH_WAIT_FOREVER 0xFFFFFFFF
|
||||
typedef int bh_socket_t;
|
||||
|
||||
#ifndef NULL
|
||||
# define NULL ((void*) 0)
|
||||
#endif
|
||||
|
||||
extern int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
|
||||
unsigned int n);
|
||||
extern int strcat_s(char * s1, size_t s1max, const char * s2);
|
||||
extern int strcpy_s(char * s1, size_t s1max, const char * s2);
|
||||
#include <stdio.h>
|
||||
extern int fopen_s(FILE ** pFile, const char *filename, const char *mode);
|
||||
|
||||
#endif
|
||||
41
core/shared-lib/platform/android/bh_platform_log.c
Executable file
41
core/shared-lib/platform/android/bh_platform_log.c
Executable file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void bh_log_emit(const char *fmt, va_list ap)
|
||||
{
|
||||
vprintf(fmt, ap);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
int bh_fprintf(FILE *stream, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
va_start(ap, fmt);
|
||||
ret = vfprintf(stream ? stream : stdout, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bh_fflush(void *stream)
|
||||
{
|
||||
return fflush(stream ? stream : stdout);
|
||||
}
|
||||
150
core/shared-lib/platform/android/bh_thread.c
Executable file
150
core/shared-lib/platform/android/bh_thread.c
Executable file
@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_thread.h"
|
||||
#include "bh_assert.h"
|
||||
#include "bh_log.h"
|
||||
#include "bh_memory.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
static korp_mutex thread_list_lock;
|
||||
static pthread_key_t thread_local_storage_key[BH_MAX_TLS_NUM];
|
||||
|
||||
int _vm_thread_sys_init()
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < BH_MAX_TLS_NUM; i++)
|
||||
pthread_key_create(&thread_local_storage_key[i], NULL);
|
||||
|
||||
return vm_mutex_init(&thread_list_lock);
|
||||
}
|
||||
|
||||
korp_tid _vm_self_thread()
|
||||
{
|
||||
return (korp_tid) pthread_self();
|
||||
}
|
||||
|
||||
void *_vm_tls_get(unsigned idx)
|
||||
{
|
||||
bh_assert(idx < BH_MAX_TLS_NUM);
|
||||
return pthread_getspecific(thread_local_storage_key[idx]);
|
||||
}
|
||||
|
||||
int _vm_tls_put(unsigned idx, void * tls)
|
||||
{
|
||||
bh_assert(idx < BH_MAX_TLS_NUM);
|
||||
pthread_setspecific(thread_local_storage_key[idx], tls);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_mutex_init(korp_mutex *mutex)
|
||||
{
|
||||
return pthread_mutex_init(mutex, NULL) == 0 ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
int _vm_recursive_mutex_init(korp_mutex *mutex)
|
||||
{
|
||||
int ret;
|
||||
|
||||
pthread_mutexattr_t mattr;
|
||||
|
||||
bh_assert(mutex);
|
||||
ret = pthread_mutexattr_init(&mattr);
|
||||
if (ret)
|
||||
return BHT_ERROR;
|
||||
|
||||
pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE_NP);
|
||||
ret = pthread_mutex_init(mutex, &mattr);
|
||||
pthread_mutexattr_destroy(&mattr);
|
||||
|
||||
return ret == 0 ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
int _vm_mutex_destroy(korp_mutex *mutex)
|
||||
{
|
||||
int ret;
|
||||
|
||||
bh_assert(mutex);
|
||||
ret = pthread_mutex_destroy(mutex);
|
||||
|
||||
return ret == 0 ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
/* Returned error (EINVAL, EAGAIN and EDEADLK) from
|
||||
locking the mutex indicates some logic error present in
|
||||
the program somewhere.
|
||||
Don't try to recover error for an existing unknown error.*/
|
||||
void vm_mutex_lock(korp_mutex *mutex)
|
||||
{
|
||||
int ret;
|
||||
|
||||
bh_assert(mutex);
|
||||
ret = pthread_mutex_lock(mutex);
|
||||
if (0 != ret) {
|
||||
LOG_FATAL("vm mutex lock failed (ret=%d)!\n", ret);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
int vm_mutex_trylock(korp_mutex *mutex)
|
||||
{
|
||||
int ret;
|
||||
|
||||
bh_assert(mutex);
|
||||
ret = pthread_mutex_trylock(mutex);
|
||||
|
||||
return ret == 0 ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
/* Returned error (EINVAL, EAGAIN and EPERM) from
|
||||
unlocking the mutex indicates some logic error present
|
||||
in the program somewhere.
|
||||
Don't try to recover error for an existing unknown error.*/
|
||||
void vm_mutex_unlock(korp_mutex *mutex)
|
||||
{
|
||||
int ret;
|
||||
|
||||
bh_assert(mutex);
|
||||
ret = pthread_mutex_unlock(mutex);
|
||||
if (0 != ret) {
|
||||
LOG_FATAL("vm mutex unlock failed (ret=%d)!\n", ret);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
int _vm_cond_init(korp_cond *cond)
|
||||
{
|
||||
bh_assert(cond);
|
||||
|
||||
if (pthread_cond_init(cond, NULL) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_destroy(korp_cond *cond)
|
||||
{
|
||||
bh_assert(cond);
|
||||
|
||||
if (pthread_cond_destroy(cond) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
71
core/shared-lib/platform/android/bh_time.c
Executable file
71
core/shared-lib/platform/android/bh_time.c
Executable file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_time.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
//#include <sys/timeb.h>
|
||||
#include <time.h>
|
||||
|
||||
/*
|
||||
* This function returns milliseconds per tick.
|
||||
* @return milliseconds per tick.
|
||||
*/
|
||||
uint64 _bh_time_get_tick_millisecond()
|
||||
{
|
||||
return sysconf(_SC_CLK_TCK);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function returns milliseconds after boot.
|
||||
* @return milliseconds after boot.
|
||||
*/
|
||||
uint64 _bh_time_get_boot_millisecond()
|
||||
{
|
||||
struct timespec ts;
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function returns GMT time milliseconds since from 1970.1.1, AKA UNIX time.
|
||||
* @return milliseconds since from 1970.1.1.
|
||||
*/
|
||||
uint64 _bh_time_get_millisecond_from_1970()
|
||||
{
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
uint64 millisecondsSinceEpoch = (uint64_t)(tv.tv_sec) * 1000
|
||||
+ (uint64_t)(tv.tv_usec) / 1000;
|
||||
return millisecondsSinceEpoch;
|
||||
}
|
||||
|
||||
size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
|
||||
{
|
||||
time_t time_sec = time / 1000;
|
||||
struct tm *ltp;
|
||||
|
||||
ltp = localtime(&time_sec);
|
||||
if (ltp == NULL) {
|
||||
return 0;
|
||||
}
|
||||
return strftime(s, max, format, ltp);
|
||||
}
|
||||
|
||||
84
core/shared-lib/platform/include/bh_assert.h
Normal file
84
core/shared-lib/platform/include/bh_assert.h
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _BH_ASSERT_H
|
||||
#define _BH_ASSERT_H
|
||||
|
||||
#include "bh_config.h"
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef BH_TEST
|
||||
# ifndef BH_DEBUG
|
||||
# error "BH_TEST should be defined under BH_DEBUG"
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef BH_TEST
|
||||
# if defined(WIN32) || defined(__linux__)
|
||||
# else
|
||||
# error "Test case can not run on the current platform"
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef BH_DEBUG
|
||||
|
||||
extern void bh_assert_internal(int v, const char *file_name, int line_number, const char *expr_string);
|
||||
#define bh_assert(expr) bh_assert_internal((int)(expr), __FILE__, __LINE__, # expr)
|
||||
extern void bh_debug_internal(const char *file_name, int line_number, const char *fmt, ...);
|
||||
|
||||
#if defined(WIN32) || defined(EMU)
|
||||
# define bh_debug(fmt, ...) bh_debug_internal(__FILE__, __LINE__, fmt, __VA_ARGS__)
|
||||
#elif defined(__linux__)
|
||||
/*# define bh_debug(...) bh_debug_internal(__FILE__, __LINE__, ## __VA_ARGS__)*/
|
||||
# define bh_debug bh_debug_internal(__FILE__, __LINE__, "");printf
|
||||
#elif defined(PLATFORM_SEC)
|
||||
# define bh_debug(fmt, ...) bh_debug_internal(__FILE__, __LINE__, fmt, __VA_ARGS__)
|
||||
#else
|
||||
# error "Unsupported platform"
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#define bh_debug if(0)printf
|
||||
|
||||
#endif
|
||||
|
||||
#define bh_assert_abort(x) do { \
|
||||
if (!x) \
|
||||
abort(); \
|
||||
} while (0)
|
||||
|
||||
#ifdef BH_TEST
|
||||
# define BH_STATIC
|
||||
#else
|
||||
# define BH_STATIC static
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/* Local Variables: */
|
||||
/* mode:c */
|
||||
/* c-basic-offset: 4 */
|
||||
/* indent-tabs-mode: nil */
|
||||
/* End: */
|
||||
31
core/shared-lib/platform/include/bh_config.h
Normal file
31
core/shared-lib/platform/include/bh_config.h
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file bh_config.h
|
||||
* @date Tue Sep 13 14:53:17 2011
|
||||
*
|
||||
* @brief Configurations for different platforms and targets. Make
|
||||
* sure all source files in Beihai project include this header file
|
||||
* directly or indirectly.
|
||||
*/
|
||||
|
||||
#ifndef BH_CONFIG
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#endif /* end of BH_CONFIG */
|
||||
|
||||
41
core/shared-lib/platform/include/bh_definition.h
Executable file
41
core/shared-lib/platform/include/bh_definition.h
Executable file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _BH_DEFINITION_H
|
||||
#define _BH_DEFINITION_H
|
||||
|
||||
#include "bh_config.h"
|
||||
|
||||
typedef enum {
|
||||
BH_FAILED = -100,
|
||||
BH_UNKOWN = -99,
|
||||
BH_MAGIC_UNMATCH = -12,
|
||||
BH_UNIMPLEMENTED = -11,
|
||||
BH_INTR = -10,
|
||||
BH_CLOSED = -9,
|
||||
BH_BUFFER_OVERFLOW = -8, /* TODO: no used error, should remove*/
|
||||
BH_NOT_SUPPORTED = -7,
|
||||
BH_WEAR_OUT_VIOLATION = -6,
|
||||
BH_NOT_FOUND = -5,
|
||||
BH_INVALID_PARAMS = -4,
|
||||
BH_ACCESS_DENIED = -3,
|
||||
BH_OUT_OF_MEMORY = -2,
|
||||
BH_INVALID = -1,
|
||||
BH_SUCCESS = 0,
|
||||
BH_TIMEOUT = 2
|
||||
} bh_status;
|
||||
|
||||
#endif
|
||||
36
core/shared-lib/platform/include/bh_platform_log.h
Normal file
36
core/shared-lib/platform/include/bh_platform_log.h
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef BH_PLATFORM_LOG
|
||||
#define BH_PLATFORM_LOG
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void bh_log_emit(const char *fmt, va_list ap);
|
||||
|
||||
int bh_fprintf(void *stream, const char *fmt, ...);
|
||||
|
||||
int bh_fflush(void *stream);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
409
core/shared-lib/platform/include/bh_thread.h
Normal file
409
core/shared-lib/platform/include/bh_thread.h
Normal file
@ -0,0 +1,409 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _BH_THREAD_H
|
||||
#define _BH_THREAD_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "bh_config.h"
|
||||
#include "bh_platform.h"
|
||||
|
||||
#define BH_MAX_THREAD 32
|
||||
#define BH_MAX_TLS_NUM 2
|
||||
|
||||
#define BHT_ERROR (-1)
|
||||
#define BHT_TIMED_OUT (1)
|
||||
#define BHT_OK (0)
|
||||
|
||||
#define BHT_NO_WAIT 0x00000000
|
||||
#define BHT_WAIT_FOREVER 0xFFFFFFFF
|
||||
|
||||
/**
|
||||
* vm_thread_sys_init
|
||||
* initiation function for beihai thread system. Invoked at the beginning of beihai intiation.
|
||||
*
|
||||
* @return BH_SUCCESS if succuess.
|
||||
*/
|
||||
int _vm_thread_sys_init(void);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_thread_sys_init_instr(const char*func_name);
|
||||
#define vm_thread_sys_init(void) vm_thread_sys_init_instr(__FUNCTION__)
|
||||
#else
|
||||
#define vm_thread_sys_init _vm_thread_sys_init
|
||||
#endif
|
||||
|
||||
void vm_thread_sys_destroy(void);
|
||||
|
||||
/**
|
||||
* This function creates a thread
|
||||
*
|
||||
* @param p_tid [OUTPUT] the pointer of tid
|
||||
* @param start main routine of the thread
|
||||
* @param arg argument passed to main routine
|
||||
* @param stack_size bytes of stack size
|
||||
*
|
||||
* @return BH_SUCCESS if success.
|
||||
*/
|
||||
int _vm_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
|
||||
unsigned int stack_size);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_thread_create_instr(korp_tid *p_tid, thread_start_routine_t start, void *arg, unsigned int stack_size, const char*func_name);
|
||||
#define vm_thread_create(p_tid, start, arg, stack_size) vm_thread_create_instr(p_tid, start, arg, stack_size, __FUNCTION__)
|
||||
#else
|
||||
#define vm_thread_create _vm_thread_create
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function creates a thread
|
||||
*
|
||||
* @param p_tid [OUTPUT] the pointer of tid
|
||||
* @param start main routine of the thread
|
||||
* @param arg argument passed to main routine
|
||||
* @param stack_size bytes of stack size
|
||||
* @param prio the priority
|
||||
*
|
||||
* @return BH_SUCCESS if success.
|
||||
*/
|
||||
int _vm_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
|
||||
void *arg, unsigned int stack_size, int prio);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_thread_create_with_prio_instr(korp_tid *p_tid, thread_start_routine_t start, void *arg, unsigned int stack_size, int prio, const char*func_name);
|
||||
#define vm_thread_create_with_prio(p_tid, start, arg, stack_size) vm_thread_create_instr(p_tid, start, arg, stack_size, prio, __FUNCTION__)
|
||||
#else
|
||||
#define vm_thread_create_with_prio _vm_thread_create_with_prio
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function never returns.
|
||||
*
|
||||
* @param code not used
|
||||
*/
|
||||
void vm_thread_exit(void *code);
|
||||
|
||||
/**
|
||||
* This function gets current thread id
|
||||
*
|
||||
* @return current thread id
|
||||
*/
|
||||
korp_tid _vm_self_thread(void);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
korp_tid vm_self_thread_instr(const char*func_name);
|
||||
#define vm_self_thread(void) vm_self_thread_instr(__FUNCTION__)
|
||||
#else
|
||||
#define vm_self_thread _vm_self_thread
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function saves a pointer in thread local storage. One thread can only save one pointer.
|
||||
*
|
||||
* @param idx tls array index
|
||||
* @param ptr pointer need save as TLS
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int _vm_tls_put(unsigned idx, void *ptr);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_tls_put_instr(unsigned idx, void *ptr, const char*func_name);
|
||||
#define vm_tls_put(idx, ptr) vm_tls_put_instr(idx, ptr, __FUNCTION__)
|
||||
#else
|
||||
#define vm_tls_put _vm_tls_put
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function gets a pointer saved in TLS.
|
||||
*
|
||||
* @param idx tls array index
|
||||
*
|
||||
* @return the pointer saved in TLS.
|
||||
*/
|
||||
void *_vm_tls_get(unsigned idx);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
void *vm_tls_get_instr(unsigned idx, const char*func_name);
|
||||
#define vm_tls_get(idx) vm_tls_get_instr(idx, __FUNCTION__)
|
||||
#else
|
||||
#define vm_tls_get _vm_tls_get
|
||||
#endif
|
||||
|
||||
#define vm_thread_testcancel(void)
|
||||
|
||||
/**
|
||||
* This function creates a non-recursive mutex
|
||||
*
|
||||
* @param mutex [OUTPUT] pointer to mutex initialized.
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int _vm_mutex_init(korp_mutex *mutex);
|
||||
#ifdef INSTRUMENT_TEST_ENABLED
|
||||
int vm_mutex_init_instr(korp_mutex *mutex, const char*func_name);
|
||||
#define vm_mutex_init(mutex) vm_mutex_init_instr(mutex, __FUNCTION__)
|
||||
#else
|
||||
#define vm_mutex_init _vm_mutex_init
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function creates a recursive mutex
|
||||
*
|
||||
* @param mutex [OUTPUT] pointer to mutex initialized.
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int _vm_recursive_mutex_init(korp_mutex *mutex);
|
||||
#ifdef INSTRUMENT_TEST_ENABLED
|
||||
int vm_recursive_mutex_init_instr(korp_mutex *mutex, const char*func_name);
|
||||
#define vm_recursive_mutex_init(mutex) vm_recursive_mutex_init_instr(mutex, __FUNCTION__)
|
||||
#else
|
||||
#define vm_recursive_mutex_init _vm_recursive_mutex_init
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function destorys a mutex
|
||||
*
|
||||
* @param mutex pointer to mutex need destory
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int _vm_mutex_destroy(korp_mutex *mutex);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_mutex_destroy_instr(korp_mutex *mutex, const char*func_name);
|
||||
#define vm_mutex_destroy(mutex) vm_mutex_destroy_instr(mutex, __FUNCTION__)
|
||||
#else
|
||||
#define vm_mutex_destroy _vm_mutex_destroy
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function locks the mutex
|
||||
*
|
||||
* @param mutex pointer to mutex need lock
|
||||
*
|
||||
* @return Void
|
||||
*/
|
||||
void vm_mutex_lock(korp_mutex *mutex);
|
||||
|
||||
/**
|
||||
* This function locks the mutex without waiting
|
||||
*
|
||||
* @param mutex pointer to mutex need lock
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int vm_mutex_trylock(korp_mutex *mutex);
|
||||
|
||||
/**
|
||||
* This function unlocks the mutex
|
||||
*
|
||||
* @param mutex pointer to mutex need unlock
|
||||
*
|
||||
* @return Void
|
||||
*/
|
||||
void vm_mutex_unlock(korp_mutex *mutex);
|
||||
|
||||
/**
|
||||
* This function creates a semaphone
|
||||
*
|
||||
* @param sem [OUTPUT] pointer to semaphone
|
||||
* @param c counter of semaphone
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int _vm_sem_init(korp_sem *sem, unsigned int c);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_sem_init_instr(korp_sem *sem, unsigned int c, const char*func_name);
|
||||
#define vm_sem_init(sem, c) vm_sem_init_instr(sem, c, __FUNCTION__)
|
||||
#else
|
||||
#define vm_sem_init _vm_sem_init
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function destroys a semaphone
|
||||
*
|
||||
* @param sem pointer to semaphone need destroy
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int _vm_sem_destroy(korp_sem *sem);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_sem_destroy_instr(korp_sem *sem, const char*func_name);
|
||||
#define vm_sem_destroy(sem) vm_sem_destroy_instr(sem, __FUNCTION__)
|
||||
#else
|
||||
#define vm_sem_destroy _vm_sem_destroy
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function performs wait operation on semaphone
|
||||
*
|
||||
* @param sem pointer to semaphone need perform wait operation
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int _vm_sem_wait(korp_sem *sem);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_sem_wait_instr(korp_sem *sem, const char*func_name);
|
||||
#define vm_sem_wait(sem) vm_sem_wait_instr(sem, __FUNCTION__)
|
||||
#else
|
||||
#define vm_sem_wait _vm_sem_wait
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function performs wait operation on semaphone with a timeout
|
||||
*
|
||||
* @param sem pointer to semaphone need perform wait operation
|
||||
* @param mills wait milliseconds to return
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
* @return BH_TIMEOUT if time out
|
||||
*/
|
||||
int _vm_sem_reltimedwait(korp_sem *sem, int mills);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_sem_reltimedwait_instr(korp_sem *sem, int mills, const char*func_name);
|
||||
#define vm_sem_reltimedwait(sem, mills) vm_sem_reltimedwait_instr(sem, mills, __FUNCTION__)
|
||||
#else
|
||||
#define vm_sem_reltimedwait _vm_sem_reltimedwait
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function performs post operation on semaphone
|
||||
*
|
||||
* @param sem pointer to semaphone need perform post operation
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int _vm_sem_post(korp_sem *sem);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_sem_post_instr(korp_sem *sem, const char*func_name);
|
||||
#define vm_sem_post(sem) vm_sem_post_instr(sem, __FUNCTION__)
|
||||
#else
|
||||
#define vm_sem_post _vm_sem_post
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function creates a condition variable
|
||||
*
|
||||
* @param cond [OUTPUT] pointer to condition variable
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int _vm_cond_init(korp_cond *cond);
|
||||
#ifdef INSTRUMENT_TEST_ENABLED
|
||||
int vm_cond_init_instr(korp_cond *cond, const char*func_name);
|
||||
#define vm_cond_init(cond) vm_cond_init_instr(cond, __FUNCTION__)
|
||||
#else
|
||||
#define vm_cond_init _vm_cond_init
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function destorys condition variable
|
||||
*
|
||||
* @param cond pointer to condition variable
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int _vm_cond_destroy(korp_cond *cond);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_cond_destroy_instr(korp_cond *cond, const char*func_name);
|
||||
#define vm_cond_destroy(cond) vm_cond_destroy_instr(cond, __FUNCTION__)
|
||||
#else
|
||||
#define vm_cond_destroy _vm_cond_destroy
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function will block on a condition varible.
|
||||
*
|
||||
* @param cond pointer to condition variable
|
||||
* @param mutex pointer to mutex to protect the condition variable
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int _vm_cond_wait(korp_cond *cond, korp_mutex *mutex);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_cond_wait_instr(korp_cond *cond, korp_mutex *mutex, const char*func_name);
|
||||
#define vm_cond_wait(cond, mutex) vm_cond_wait_instr(cond, mutex, __FUNCTION__)
|
||||
#else
|
||||
#define vm_cond_wait _vm_cond_wait
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function will block on a condition varible or return if time specified passes.
|
||||
*
|
||||
* @param cond pointer to condition variable
|
||||
* @param mutex pointer to mutex to protect the condition variable
|
||||
* @param mills milliseconds to wait
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_cond_reltimedwait_instr(korp_cond *cond, korp_mutex *mutex, int mills, const char*func_name);
|
||||
#define vm_cond_reltimedwait(cond, mutex, mills) vm_cond_reltimedwait_instr(cond, mutex, mills, __FUNCTION__)
|
||||
#else
|
||||
#define vm_cond_reltimedwait _vm_cond_reltimedwait
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function signals the condition variable
|
||||
*
|
||||
* @param cond condition variable
|
||||
*
|
||||
* @return BH_SUCCESS if success
|
||||
*/
|
||||
int _vm_cond_signal(korp_cond *cond);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_cond_signal_instr(korp_cond *cond, const char*func_name);
|
||||
#define vm_cond_signal(cond) vm_cond_signal_instr(cond, __FUNCTION__)
|
||||
#else
|
||||
#define vm_cond_signal _vm_cond_signal
|
||||
#endif
|
||||
|
||||
int _vm_cond_broadcast(korp_cond *cond);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_cond_broadcast_instr(korp_cond *cond, const char*func_name);
|
||||
#define vm_cond_broadcast(cond) vm_cond_broadcast_instr(cond, __FUNCTION__)
|
||||
#else
|
||||
#define vm_cond_broadcast _vm_cond_broadcast
|
||||
#endif
|
||||
|
||||
int _vm_thread_cancel(korp_tid thread);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_thread_cancel_instr(korp_tid thread, const char*func_name);
|
||||
#define vm_thread_cancel(thread) vm_thread_cancel_instr(thread, __FUNCTION__)
|
||||
#else
|
||||
#define vm_thread_cancel _vm_thread_cancel
|
||||
#endif
|
||||
|
||||
int _vm_thread_join(korp_tid thread, void **value_ptr, int mills);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_thread_join_instr(korp_tid thread, void **value_ptr, int mills, const char*func_name);
|
||||
#define vm_thread_join(thread, value_ptr, mills) vm_thread_join_instr(thread, value_ptr, mills, __FUNCTION__)
|
||||
#else
|
||||
#define vm_thread_join _vm_thread_join
|
||||
#endif
|
||||
|
||||
int _vm_thread_detach(korp_tid thread);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
int vm_thread_detach_instr(korp_tid thread, const char*func_name);
|
||||
#define vm_thread_detach(thread) vm_thread_detach_instr(thread, __FUNCTION__)
|
||||
#else
|
||||
#define vm_thread_detach _vm_thread_detach
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* #ifndef _BH_THREAD_H */
|
||||
98
core/shared-lib/platform/include/bh_time.h
Normal file
98
core/shared-lib/platform/include/bh_time.h
Normal file
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _BH_TIME_H
|
||||
#define _BH_TIME_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "bh_config.h"
|
||||
#include "bh_definition.h"
|
||||
#include "bh_types.h"
|
||||
#include "bh_platform.h"
|
||||
|
||||
/*
|
||||
* This function returns milliseconds per tick.
|
||||
* @return milliseconds per tick.
|
||||
*/
|
||||
extern uint64 _bh_time_get_tick_millisecond(void);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
extern uint64 bh_time_get_tick_millisecond_instr(const char*func_name);
|
||||
#define bh_time_get_tick_millisecond() bh_time_get_tick_millisecond_instr(__FUNCTION__)
|
||||
#else
|
||||
#define bh_time_get_tick_millisecond _bh_time_get_tick_millisecond
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This function returns milliseconds after boot.
|
||||
* @return milliseconds after boot.
|
||||
*/
|
||||
extern uint64 _bh_time_get_boot_millisecond(void);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
extern uint64 bh_time_get_boot_millisecond_instr(const char*func_name);
|
||||
#define bh_time_get_boot_millisecond() bh_time_get_boot_millisecond_instr(__FUNCTION__)
|
||||
#else
|
||||
#define bh_time_get_boot_millisecond _bh_time_get_boot_millisecond
|
||||
#endif
|
||||
|
||||
extern uint32 bh_get_tick_sec();
|
||||
#define bh_get_tick_ms _bh_time_get_boot_millisecond
|
||||
|
||||
/*
|
||||
* This function returns GMT milliseconds since from 1970.1.1, AKA UNIX time.
|
||||
* @return milliseconds since from 1970.1.1.
|
||||
*/
|
||||
extern uint64 _bh_time_get_millisecond_from_1970(void);
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
extern uint64 bh_time_get_millisecond_from_1970_instr(const char*func_name);
|
||||
#define bh_time_get_millisecond_from_1970() bh_time_get_millisecond_from_1970_instr(__FUNCTION__)
|
||||
#else
|
||||
#define bh_time_get_millisecond_from_1970 _bh_time_get_millisecond_from_1970
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function sets timezone with specific hours.
|
||||
*
|
||||
* @param hours represents the deviation (in hours) of the local time from GMT (can be a positive or a negative number)
|
||||
* @param half_hour if true, adds half an hour to the local time calculation. For example, if hours=(+5) then the time will be GMT +5:30; if hours=(-5) then the time will be GMT -4:30.
|
||||
* @param daylight_save if true, applies the daylight saving scheme when calculating the local time (adds one hour to the local time calculation)
|
||||
*/
|
||||
extern void bh_set_timezone(int hours, int half_hour, int daylight_save);
|
||||
|
||||
/**
|
||||
* This functions returns the offset in seconds which needs to be added GMT to get the local time.
|
||||
*
|
||||
*
|
||||
* @return offset in secords which needs to be added GMT to get the local time.
|
||||
*/
|
||||
extern int bh_get_timezone_offset(void);
|
||||
|
||||
size_t bh_time_strftime(char *s, size_t max, const char *format, int64 time);
|
||||
|
||||
#ifdef _INSTRUMENT_TEST_ENABLED
|
||||
size_t bh_time_strftime_instr(char *s, size_t max, const char *format, int64 time, const char*func_name);
|
||||
#define bh_time_strftime(s, max, format, time) bh_time_strftime_instr(s, max, format, time, __FUNCTION__)
|
||||
#else
|
||||
#define bh_time_strftime _bh_time_strftime
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
51
core/shared-lib/platform/include/bh_types.h
Normal file
51
core/shared-lib/platform/include/bh_types.h
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _BH_TYPES_H
|
||||
#define _BH_TYPES_H
|
||||
|
||||
#include "bh_config.h"
|
||||
|
||||
typedef unsigned char uint8;
|
||||
typedef char int8;
|
||||
typedef unsigned short uint16;
|
||||
typedef short int16;
|
||||
typedef unsigned int uint32;
|
||||
typedef int int32;
|
||||
|
||||
#define BYTES_OF_UINT8 1
|
||||
#define BYTES_OF_UINT16 2
|
||||
#define BYTES_OF_UINT32 4
|
||||
#define BYTES_OF_UINT64 8
|
||||
|
||||
#define BYTES_OF_BOOLEAN 1
|
||||
#define BYTES_OF_BYTE 1
|
||||
#define BYTES_OF_CHAR 2
|
||||
#define BYTES_OF_SHORT 2
|
||||
#define BYTES_OF_INT 4
|
||||
#define BYTES_OF_FLOAT 4
|
||||
#define BYTES_OF_LONG 8
|
||||
#define BYTES_OF_DOUBLE 8
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifndef __cplusplus
|
||||
#define true 1
|
||||
#define false 0
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
69
core/shared-lib/platform/linux/bh_assert.c
Normal file
69
core/shared-lib/platform/linux/bh_assert.c
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "bh_assert.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef BH_TEST
|
||||
#include <setjmp.h>
|
||||
#endif
|
||||
|
||||
#ifdef BH_TEST
|
||||
/* for exception throwing */
|
||||
jmp_buf bh_test_jb;
|
||||
#endif
|
||||
|
||||
void bh_assert_internal(int v, const char *file_name, int line_number,
|
||||
const char *expr_string)
|
||||
{
|
||||
if (v)
|
||||
return;
|
||||
|
||||
if (!file_name)
|
||||
file_name = "NULL FILENAME";
|
||||
if (!expr_string)
|
||||
expr_string = "NULL EXPR_STRING";
|
||||
|
||||
printf("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
|
||||
file_name, line_number);
|
||||
|
||||
#ifdef BH_TEST
|
||||
longjmp(bh_test_jb, 1);
|
||||
#endif
|
||||
|
||||
abort();
|
||||
}
|
||||
|
||||
void bh_debug_internal(const char *file_name, int line_number, const char *fmt,
|
||||
...)
|
||||
{
|
||||
#ifndef JEFF_TEST_VERIFIER
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
bh_assert(file_name);
|
||||
|
||||
printf("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
|
||||
vprintf(fmt, args);
|
||||
|
||||
va_end(args);
|
||||
printf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
81
core/shared-lib/platform/linux/bh_definition.c
Normal file
81
core/shared-lib/platform/linux/bh_definition.c
Normal file
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_definition.h"
|
||||
#include "bh_platform.h"
|
||||
|
||||
int bh_return(int ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define RSIZE_MAX 0x7FFFFFFF
|
||||
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
|
||||
{
|
||||
char *dest = (char*) s1;
|
||||
char *src = (char*) s2;
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (s1 == NULL || s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
if (s2 == NULL || n > s1max) {
|
||||
memset(dest, 0, s1max);
|
||||
return -1;
|
||||
}
|
||||
memcpy(dest, src, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int b_strcat_s(char * s1, size_t s1max, const char * s2)
|
||||
{
|
||||
if (NULL
|
||||
== s1|| NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1) || s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
strcat(s1, s2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int b_strcpy_s(char * s1, size_t s1max, const char * s2)
|
||||
{
|
||||
if (NULL
|
||||
== s1|| NULL == s2 || s1max < (strlen(s2) + 1) || s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
strcpy(s1, s2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fopen_s(FILE ** pFile, const char *filename, const char *mode)
|
||||
{
|
||||
if (NULL == pFile || NULL == filename || NULL == mode) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
*pFile = fopen(filename, mode);
|
||||
|
||||
if (NULL == *pFile)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
27
core/shared-lib/platform/linux/bh_platform.c
Executable file
27
core/shared-lib/platform/linux/bh_platform.c
Executable file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *bh_strdup(const char *s)
|
||||
{
|
||||
char *s1 = NULL;
|
||||
if (s && (s1 = bh_malloc(strlen(s) + 1)))
|
||||
memcpy(s1, s, strlen(s) + 1);
|
||||
return s1;
|
||||
}
|
||||
129
core/shared-lib/platform/linux/bh_platform.h
Normal file
129
core/shared-lib/platform/linux/bh_platform.h
Normal file
@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _BH_PLATFORM_H
|
||||
#define _BH_PLATFORM_H
|
||||
|
||||
#include "bh_config.h"
|
||||
#include "bh_types.h"
|
||||
#include "bh_memory.h"
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifndef __cplusplus
|
||||
int snprintf(char *buffer, size_t count, const char *format, ...);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef uint64_t uint64;
|
||||
typedef int64_t int64;
|
||||
|
||||
extern void DEBUGME(void);
|
||||
|
||||
#define DIE do{bh_debug("Die here\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); DEBUGME(void); while(1);}while(0)
|
||||
|
||||
#define BH_PLATFORM "Linux"
|
||||
|
||||
/* NEED qsort */
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <ctype.h>
|
||||
#include <pthread.h>
|
||||
#include <limits.h>
|
||||
#include <semaphore.h>
|
||||
#include <errno.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#define _STACK_SIZE_ADJUSTMENT (32 * 1024)
|
||||
|
||||
/* Stack size of applet manager thread. */
|
||||
#define BH_APPLET_MANAGER_THREAD_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Stack size of HMC thread. */
|
||||
#define BH_HMC_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Stack size of watchdog thread. */
|
||||
#define BH_WATCHDOG_THREAD_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Stack size of applet threads's native part. */
|
||||
#define BH_APPLET_PRESERVED_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Stack size of remote invoke listen thread. */
|
||||
#define BH_REMOTE_INVOKE_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Stack size of remote post listen thread. */
|
||||
#define BH_REMOTE_POST_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Maximal recursion depth of interpreter. */
|
||||
#define BH_MAX_INTERP_RECURSION_DEPTH 8
|
||||
|
||||
/* Default thread priority */
|
||||
#define BH_THREAD_DEFAULT_PRIORITY 0
|
||||
|
||||
#define BH_ROUTINE_MODIFIER
|
||||
#define BHT_TIMEDOUT ETIMEDOUT
|
||||
|
||||
#define INVALID_THREAD_ID 0xFFffFFff
|
||||
#define INVALID_SEM_ID SEM_FAILED
|
||||
|
||||
typedef pthread_t korp_tid;
|
||||
typedef pthread_mutex_t korp_mutex;
|
||||
typedef sem_t korp_sem;
|
||||
typedef pthread_cond_t korp_cond;
|
||||
typedef pthread_t korp_thread;
|
||||
typedef void* (*thread_start_routine_t)(void*);
|
||||
|
||||
#define wa_malloc bh_malloc
|
||||
#define wa_free bh_free
|
||||
#define wa_strdup bh_strdup
|
||||
|
||||
double fmod(double x, double y);
|
||||
float fmodf(float x, float y);
|
||||
|
||||
/* Definitions for applet debugging */
|
||||
#define APPLET_DEBUG_LISTEN_PORT 8000
|
||||
#define BH_SOCKET_INVALID_SOCK -1
|
||||
#define BH_WAIT_FOREVER 0xFFFFFFFF
|
||||
typedef int bh_socket_t;
|
||||
|
||||
#ifndef NULL
|
||||
# define NULL ((void*) 0)
|
||||
#endif
|
||||
|
||||
#define bh_assert assert
|
||||
|
||||
extern int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
|
||||
unsigned int n);
|
||||
extern int b_strcat_s(char * s1, size_t s1max, const char * s2);
|
||||
extern int b_strcpy_s(char * s1, size_t s1max, const char * s2);
|
||||
extern int fopen_s(FILE ** pFile, const char *filename, const char *mode);
|
||||
|
||||
extern char *bh_strdup(const char *s);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
41
core/shared-lib/platform/linux/bh_platform_log.c
Normal file
41
core/shared-lib/platform/linux/bh_platform_log.c
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void bh_log_emit(const char *fmt, va_list ap)
|
||||
{
|
||||
vprintf(fmt, ap);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
int bh_fprintf(FILE *stream, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
va_start(ap, fmt);
|
||||
ret = vfprintf(stream ? stream : stdout, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bh_fflush(void *stream)
|
||||
{
|
||||
return fflush(stream ? stream : stdout);
|
||||
}
|
||||
404
core/shared-lib/platform/linux/bh_thread.c
Executable file
404
core/shared-lib/platform/linux/bh_thread.c
Executable file
@ -0,0 +1,404 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_thread.h"
|
||||
#include "bh_assert.h"
|
||||
#include "bh_log.h"
|
||||
#include "bh_memory.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
static bool is_thread_sys_inited = false;
|
||||
|
||||
static korp_mutex thread_list_lock;
|
||||
static pthread_key_t thread_local_storage_key[BH_MAX_TLS_NUM];
|
||||
|
||||
int _vm_thread_sys_init()
|
||||
{
|
||||
unsigned i, j;
|
||||
int ret;
|
||||
|
||||
if (is_thread_sys_inited)
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < BH_MAX_TLS_NUM; i++) {
|
||||
ret = pthread_key_create(&thread_local_storage_key[i], NULL);
|
||||
if (ret)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ret = vm_mutex_init(&thread_list_lock);
|
||||
if (ret)
|
||||
goto fail;
|
||||
|
||||
is_thread_sys_inited = true;
|
||||
return 0;
|
||||
|
||||
fail: for (j = 0; j < i; j++)
|
||||
pthread_key_delete(thread_local_storage_key[j]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void vm_thread_sys_destroy(void)
|
||||
{
|
||||
if (is_thread_sys_inited) {
|
||||
unsigned i;
|
||||
for (i = 0; i < BH_MAX_TLS_NUM; i++)
|
||||
pthread_key_delete(thread_local_storage_key[i]);
|
||||
vm_mutex_destroy(&thread_list_lock);
|
||||
is_thread_sys_inited = false;
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
thread_start_routine_t start;
|
||||
void* stack;
|
||||
int stack_size;
|
||||
void* arg;
|
||||
} thread_wrapper_arg;
|
||||
|
||||
static void *vm_thread_wrapper(void *arg)
|
||||
{
|
||||
thread_wrapper_arg * targ = arg;
|
||||
LOG_VERBOSE("THREAD CREATE 0x%08x\n", &targ);
|
||||
targ->stack = (void *) ((unsigned int) (&arg) & ~0xfff);
|
||||
_vm_tls_put(1, targ);
|
||||
targ->start(targ->arg);
|
||||
bh_free(targ);
|
||||
_vm_tls_put(1, NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
|
||||
void *arg, unsigned int stack_size, int prio)
|
||||
{
|
||||
pthread_attr_t tattr;
|
||||
thread_wrapper_arg *targ;
|
||||
|
||||
bh_assert(stack_size > 0);
|
||||
bh_assert(tid);
|
||||
bh_assert(start);
|
||||
|
||||
*tid = INVALID_THREAD_ID;
|
||||
|
||||
pthread_attr_init(&tattr);
|
||||
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
|
||||
if (pthread_attr_setstacksize(&tattr, stack_size) != 0) {
|
||||
bh_debug("Invalid thread stack size %u. Min stack size on Linux = %u",
|
||||
stack_size, PTHREAD_STACK_MIN);
|
||||
pthread_attr_destroy(&tattr);
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
targ = (thread_wrapper_arg*) bh_malloc(sizeof(*targ));
|
||||
if (!targ) {
|
||||
pthread_attr_destroy(&tattr);
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
targ->start = start;
|
||||
targ->arg = arg;
|
||||
targ->stack_size = stack_size;
|
||||
|
||||
if (pthread_create(tid, &tattr, vm_thread_wrapper, targ) != 0) {
|
||||
pthread_attr_destroy(&tattr);
|
||||
bh_free(targ);
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
pthread_attr_destroy(&tattr);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
|
||||
unsigned int stack_size)
|
||||
{
|
||||
return _vm_thread_create_with_prio(tid, start, arg, stack_size,
|
||||
BH_THREAD_DEFAULT_PRIORITY);
|
||||
}
|
||||
|
||||
korp_tid _vm_self_thread()
|
||||
{
|
||||
return (korp_tid) pthread_self();
|
||||
}
|
||||
|
||||
void vm_thread_exit(void * code)
|
||||
{
|
||||
bh_free(_vm_tls_get(1));
|
||||
_vm_tls_put(1, NULL);
|
||||
pthread_exit(code);
|
||||
}
|
||||
|
||||
void *_vm_tls_get(unsigned idx)
|
||||
{
|
||||
bh_assert(idx < BH_MAX_TLS_NUM);
|
||||
return pthread_getspecific(thread_local_storage_key[idx]);
|
||||
}
|
||||
|
||||
int _vm_tls_put(unsigned idx, void * tls)
|
||||
{
|
||||
bh_assert(idx < BH_MAX_TLS_NUM);
|
||||
pthread_setspecific(thread_local_storage_key[idx], tls);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_mutex_init(korp_mutex *mutex)
|
||||
{
|
||||
return pthread_mutex_init(mutex, NULL) == 0 ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
int _vm_recursive_mutex_init(korp_mutex *mutex)
|
||||
{
|
||||
int ret;
|
||||
|
||||
pthread_mutexattr_t mattr;
|
||||
|
||||
bh_assert(mutex);
|
||||
ret = pthread_mutexattr_init(&mattr);
|
||||
if (ret)
|
||||
return BHT_ERROR;
|
||||
|
||||
pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE_NP);
|
||||
ret = pthread_mutex_init(mutex, &mattr);
|
||||
pthread_mutexattr_destroy(&mattr);
|
||||
|
||||
return ret == 0 ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
int _vm_mutex_destroy(korp_mutex *mutex)
|
||||
{
|
||||
int ret;
|
||||
|
||||
bh_assert(mutex);
|
||||
ret = pthread_mutex_destroy(mutex);
|
||||
|
||||
return ret == 0 ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
/* Returned error (EINVAL, EAGAIN and EDEADLK) from
|
||||
locking the mutex indicates some logic error present in
|
||||
the program somewhere.
|
||||
Don't try to recover error for an existing unknown error.*/
|
||||
void vm_mutex_lock(korp_mutex *mutex)
|
||||
{
|
||||
int ret;
|
||||
|
||||
bh_assert(mutex);
|
||||
ret = pthread_mutex_lock(mutex);
|
||||
if (0 != ret) {
|
||||
printf("vm mutex lock failed (ret=%d)!\n", ret);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
int vm_mutex_trylock(korp_mutex *mutex)
|
||||
{
|
||||
int ret;
|
||||
|
||||
bh_assert(mutex);
|
||||
ret = pthread_mutex_trylock(mutex);
|
||||
|
||||
return ret == 0 ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
/* Returned error (EINVAL, EAGAIN and EPERM) from
|
||||
unlocking the mutex indicates some logic error present
|
||||
in the program somewhere.
|
||||
Don't try to recover error for an existing unknown error.*/
|
||||
void vm_mutex_unlock(korp_mutex *mutex)
|
||||
{
|
||||
int ret;
|
||||
|
||||
bh_assert(mutex);
|
||||
ret = pthread_mutex_unlock(mutex);
|
||||
if (0 != ret) {
|
||||
printf("vm mutex unlock failed (ret=%d)!\n", ret);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
int _vm_sem_init(korp_sem* sem, unsigned int c)
|
||||
{
|
||||
int ret;
|
||||
|
||||
bh_assert(sem);
|
||||
ret = sem_init(sem, 0, c);
|
||||
|
||||
return ret == 0 ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
int _vm_sem_destroy(korp_sem *sem)
|
||||
{
|
||||
int ret;
|
||||
|
||||
bh_assert(sem);
|
||||
ret = sem_destroy(sem);
|
||||
|
||||
return ret == 0 ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
int _vm_sem_wait(korp_sem *sem)
|
||||
{
|
||||
int ret;
|
||||
|
||||
bh_assert(sem);
|
||||
|
||||
ret = sem_wait(sem);
|
||||
|
||||
return ret == 0 ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
int _vm_sem_reltimedwait(korp_sem *sem, int mills)
|
||||
{
|
||||
int ret = BHT_OK;
|
||||
|
||||
struct timespec timeout;
|
||||
const int mills_per_sec = 1000;
|
||||
const int mills_to_nsec = 1E6;
|
||||
|
||||
bh_assert(sem);
|
||||
|
||||
if (mills == BHT_WAIT_FOREVER) {
|
||||
ret = sem_wait(sem);
|
||||
} else {
|
||||
|
||||
timeout.tv_sec = mills / mills_per_sec;
|
||||
timeout.tv_nsec = (mills % mills_per_sec) * mills_to_nsec;
|
||||
timeout.tv_sec += time(NULL);
|
||||
|
||||
ret = sem_timedwait(sem, &timeout);
|
||||
}
|
||||
|
||||
if (ret != BHT_OK) {
|
||||
if (errno == BHT_TIMEDOUT) {
|
||||
ret = BHT_TIMEDOUT;
|
||||
errno = 0;
|
||||
} else {
|
||||
bh_debug("Faliure happens when timed wait is called");
|
||||
bh_assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int _vm_sem_post(korp_sem *sem)
|
||||
{
|
||||
bh_assert(sem);
|
||||
|
||||
return sem_post(sem) == 0 ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
int _vm_cond_init(korp_cond *cond)
|
||||
{
|
||||
bh_assert(cond);
|
||||
|
||||
if (pthread_cond_init(cond, NULL) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_destroy(korp_cond *cond)
|
||||
{
|
||||
bh_assert(cond);
|
||||
|
||||
if (pthread_cond_destroy(cond) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_wait(korp_cond *cond, korp_mutex *mutex)
|
||||
{
|
||||
bh_assert(cond);
|
||||
bh_assert(mutex);
|
||||
|
||||
if (pthread_cond_wait(cond, mutex) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
static void msec_nsec_to_abstime(struct timespec *ts, int64 msec, int32 nsec)
|
||||
{
|
||||
struct timeval tv;
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
ts->tv_sec = tv.tv_sec + msec / 1000;
|
||||
ts->tv_nsec = tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec;
|
||||
|
||||
if (ts->tv_nsec >= 1000000000L) {
|
||||
ts->tv_sec++;
|
||||
ts->tv_nsec -= 1000000000L;
|
||||
}
|
||||
}
|
||||
|
||||
int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
|
||||
{
|
||||
int ret;
|
||||
struct timespec abstime;
|
||||
|
||||
if (mills == BHT_WAIT_FOREVER)
|
||||
ret = pthread_cond_wait(cond, mutex);
|
||||
else {
|
||||
msec_nsec_to_abstime(&abstime, mills, 0);
|
||||
ret = pthread_cond_timedwait(cond, mutex, &abstime);
|
||||
}
|
||||
|
||||
if (ret != BHT_OK && ret != BHT_TIMEDOUT)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_signal(korp_cond *cond)
|
||||
{
|
||||
bh_assert(cond);
|
||||
|
||||
if (pthread_cond_signal(cond) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_broadcast(korp_cond *cond)
|
||||
{
|
||||
bh_assert(cond);
|
||||
|
||||
if (pthread_cond_broadcast(cond) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_thread_cancel(korp_tid thread)
|
||||
{
|
||||
return pthread_cancel(thread);
|
||||
}
|
||||
|
||||
int _vm_thread_join(korp_tid thread, void **value_ptr, int mills)
|
||||
{
|
||||
return pthread_join(thread, value_ptr);
|
||||
}
|
||||
|
||||
int _vm_thread_detach(korp_tid thread)
|
||||
{
|
||||
return pthread_detach(thread);
|
||||
}
|
||||
|
||||
80
core/shared-lib/platform/linux/bh_time.c
Executable file
80
core/shared-lib/platform/linux/bh_time.c
Executable file
@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_time.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/timeb.h>
|
||||
#include <time.h>
|
||||
|
||||
/*
|
||||
* This function returns milliseconds per tick.
|
||||
* @return milliseconds per tick.
|
||||
*/
|
||||
uint64 _bh_time_get_tick_millisecond()
|
||||
{
|
||||
return sysconf(_SC_CLK_TCK);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function returns milliseconds after boot.
|
||||
* @return milliseconds after boot.
|
||||
*/
|
||||
uint64 _bh_time_get_boot_millisecond()
|
||||
{
|
||||
struct timespec ts;
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
|
||||
}
|
||||
|
||||
uint32 bh_get_tick_sec()
|
||||
{
|
||||
return _bh_time_get_boot_millisecond() / 1000;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function returns GMT time milliseconds since from 1970.1.1, AKA UNIX time.
|
||||
* @return milliseconds since from 1970.1.1.
|
||||
*/
|
||||
uint64 _bh_time_get_millisecond_from_1970()
|
||||
{
|
||||
struct timeb tp;
|
||||
ftime(&tp);
|
||||
|
||||
return ((uint64) tp.time) * 1000 + tp.millitm
|
||||
- (tp.dstflag == 0 ? 0 : 60 * 60 * 1000) + tp.timezone * 60 * 1000;
|
||||
}
|
||||
|
||||
size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
|
||||
{
|
||||
time_t time_sec = time / 1000;
|
||||
struct timeb tp;
|
||||
struct tm *ltp;
|
||||
|
||||
ftime(&tp);
|
||||
time_sec -= tp.timezone * 60;
|
||||
|
||||
ltp = localtime(&time_sec);
|
||||
if (ltp == NULL) {
|
||||
return 0;
|
||||
}
|
||||
return strftime(s, max, format, ltp);
|
||||
}
|
||||
|
||||
24
core/shared-lib/platform/linux/shared_platform.cmake
Normal file
24
core/shared-lib/platform/linux/shared_platform.cmake
Normal file
@ -0,0 +1,24 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${PLATFORM_SHARED_DIR})
|
||||
include_directories(${PLATFORM_SHARED_DIR}/../include)
|
||||
|
||||
|
||||
file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c)
|
||||
|
||||
set (PLATFORM_SHARED_SOURCE ${source_all})
|
||||
|
||||
67
core/shared-lib/platform/win32/bh_assert.c
Normal file
67
core/shared-lib/platform/win32/bh_assert.c
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "bh_assert.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef BH_TEST
|
||||
#include <setjmp.h>
|
||||
#endif
|
||||
|
||||
#ifdef BH_TEST
|
||||
/* for exception throwing */
|
||||
jmp_buf bh_test_jb;
|
||||
#endif
|
||||
|
||||
void bh_assert_internal(int v, const char *file_name, int line_number,
|
||||
const char *expr_string)
|
||||
{
|
||||
if (v)
|
||||
return;
|
||||
|
||||
if (!file_name)
|
||||
file_name = "NULL FILENAME";
|
||||
if (!expr_string)
|
||||
expr_string = "NULL EXPR_STRING";
|
||||
|
||||
printf("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
|
||||
file_name, line_number);
|
||||
|
||||
#ifdef BH_TEST
|
||||
longjmp(bh_test_jb, 1);
|
||||
#endif
|
||||
|
||||
abort();
|
||||
}
|
||||
|
||||
void bh_debug_internal(const char *file_name, int line_number, const char *fmt,
|
||||
...)
|
||||
{
|
||||
#ifndef JEFF_TEST_VERIFIER
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
bh_assert(file_name);
|
||||
|
||||
printf("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
|
||||
vprintf(fmt, args);
|
||||
|
||||
va_end(args);
|
||||
printf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
22
core/shared-lib/platform/win32/bh_definition.c
Normal file
22
core/shared-lib/platform/win32/bh_definition.c
Normal file
@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_definition.h"
|
||||
|
||||
int bh_return(int ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
93
core/shared-lib/platform/win32/bh_platform.h
Executable file
93
core/shared-lib/platform/win32/bh_platform.h
Executable file
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _BH_PLATFORM_H
|
||||
#define _BH_PLATFORM_H
|
||||
|
||||
#include "bh_config.h"
|
||||
#include "bh_types.h"
|
||||
|
||||
#ifndef NVALGRIND
|
||||
#define NVALGRIND
|
||||
#endif
|
||||
|
||||
/* Reserve bytes on applet stack for native functions called from
|
||||
* Java methods to avoid undetectable stack overflow.
|
||||
*/
|
||||
#ifndef APPLET_PRESERVED_STACK_SIZE
|
||||
#define APPLET_PRESERVED_STACK_SIZE (16 * BH_KB)
|
||||
#endif
|
||||
|
||||
typedef unsigned __int64 uint64;
|
||||
typedef __int64 int64;
|
||||
|
||||
extern void DEBUGME(void);
|
||||
|
||||
#define DIE do{bh_debug("Die here\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); DEBUGME(void); while(1);}while(0)
|
||||
|
||||
#ifndef BH_INVALID_HANDLE
|
||||
#define BH_INVALID_HANDLE NULL
|
||||
#endif
|
||||
|
||||
#define BH_PLATFORM "AMULET"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#define _STACK_SIZE_ADJUSTMENT (32 * 1024)
|
||||
|
||||
/* Stack size of applet manager thread. */
|
||||
#define BH_APPLET_MANAGER_THREAD_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Stack size of HMC thread. */
|
||||
#define BH_HMC_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Stack size of watchdog thread. */
|
||||
#define BH_WATCHDOG_THREAD_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Stack size of applet threads's native part. */
|
||||
#define BH_APPLET_PRESERVED_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
|
||||
|
||||
/* Maximal recursion depth of interpreter. */
|
||||
#define BH_MAX_INTERP_RECURSION_DEPTH 8
|
||||
|
||||
#define wa_malloc bh_malloc
|
||||
#define wa_free bh_free
|
||||
|
||||
#define snprintf _snprintf
|
||||
|
||||
#define BH_ROUTINE_MODIFIER __stdcall
|
||||
|
||||
typedef void *korp_tid;
|
||||
#define INVALID_THREAD_ID 0
|
||||
|
||||
typedef void *korp_mutex;
|
||||
typedef void *korp_sem;
|
||||
|
||||
typedef struct {
|
||||
korp_sem s;
|
||||
unsigned waiting_count;
|
||||
} korp_cond;
|
||||
|
||||
typedef void* (BH_ROUTINE_MODIFIER *thread_start_routine_t)(void*);
|
||||
|
||||
#endif
|
||||
|
||||
39
core/shared-lib/platform/win32/bh_platform_log.c
Executable file
39
core/shared-lib/platform/win32/bh_platform_log.c
Executable file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "bh_platform.h"
|
||||
|
||||
void bh_log_emit(const char *fmt, va_list ap)
|
||||
{
|
||||
vprintf(fmt, ap);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
int bh_fprintf(FILE *stream, const char *fmt, ...)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
ret = vfprintf(stream ? stream : stdout, fmt, ap);
|
||||
va_end(ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bh_fflush(void *stream)
|
||||
{
|
||||
return fflush(stream ? stream : stdout);
|
||||
}
|
||||
342
core/shared-lib/platform/win32/bh_thread.c
Executable file
342
core/shared-lib/platform/win32/bh_thread.c
Executable file
@ -0,0 +1,342 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_thread.h"
|
||||
#include "bh_assert.h"
|
||||
#include "bh_log.h"
|
||||
#include "bh_memory.h"
|
||||
|
||||
#include <windows.h>
|
||||
#include <process.h>
|
||||
|
||||
#ifdef _DEBUG
|
||||
#define THREAD_STACK_ADJUSTMENT (32 * 1024)
|
||||
#else
|
||||
#define THREAD_STACK_ADJUSTMENT 0
|
||||
#endif
|
||||
|
||||
static korp_mutex thread_list_lock;
|
||||
static DWORD tls_indexes[BH_MAX_TLS_NUM];
|
||||
|
||||
typedef struct {
|
||||
int zero_padding;
|
||||
thread_start_routine_t start;
|
||||
void* stack;
|
||||
void* args;
|
||||
int stack_size;
|
||||
} vm_thread_block;
|
||||
|
||||
static DWORD tb_index;
|
||||
|
||||
int _vm_thread_sys_init()
|
||||
{
|
||||
unsigned int i;
|
||||
for (i = 0; i < BH_MAX_TLS_NUM; i++) {
|
||||
tls_indexes[i] = TlsAlloc();
|
||||
if (tls_indexes[i] == TLS_OUT_OF_INDEXES)
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
tb_index = TlsAlloc();
|
||||
if (tb_index == TLS_OUT_OF_INDEXES)
|
||||
return BHT_ERROR;
|
||||
|
||||
return vm_mutex_init(&thread_list_lock);
|
||||
}
|
||||
|
||||
static unsigned int BH_ROUTINE_MODIFIER beihai_starter(void* arg)
|
||||
{
|
||||
vm_thread_block* tb = (vm_thread_block*) arg;
|
||||
TlsSetValue(tb_index, tb);
|
||||
tb->stack = (void *) &arg;
|
||||
tb->start(tb->args);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
|
||||
unsigned int stack_size)
|
||||
{
|
||||
unsigned int default_stack_size = 20 * 1024;
|
||||
vm_thread_block* tb;
|
||||
bh_assert(tid);
|
||||
bh_assert(start);
|
||||
|
||||
if (stack_size == 0)
|
||||
stack_size = default_stack_size;
|
||||
|
||||
#ifdef _DEBUG
|
||||
stack_size = THREAD_STACK_ADJUSTMENT + stack_size*3;
|
||||
#endif
|
||||
|
||||
tb = (vm_thread_block*) bh_malloc(sizeof(*tb));
|
||||
if (tb == NULL)
|
||||
return BHT_ERROR;
|
||||
|
||||
memset(tb, 0, sizeof(*tb));
|
||||
|
||||
tb->start = start;
|
||||
tb->stack_size = stack_size;
|
||||
tb->args = arg;
|
||||
|
||||
*tid = (korp_tid) _beginthreadex(NULL, stack_size, beihai_starter,
|
||||
(void*) tb, 0, NULL);
|
||||
|
||||
/* TODO: to deal with the handle; how to close it? */
|
||||
return (*tid == INVALID_THREAD_ID) ? BHT_ERROR : BHT_OK;
|
||||
}
|
||||
|
||||
korp_tid _vm_self_thread()
|
||||
{
|
||||
return (korp_tid) GetCurrentThread();
|
||||
}
|
||||
|
||||
void vm_thread_exit(void *code)
|
||||
{
|
||||
vm_thread_block *tb = (vm_thread_block*) TlsGetValue(tb_index);
|
||||
bh_free(tb);
|
||||
_endthreadex((unsigned int) code);
|
||||
}
|
||||
|
||||
void* vm_get_stackaddr()
|
||||
{
|
||||
vm_thread_block *tb = (vm_thread_block*) TlsGetValue(tb_index);
|
||||
return (char *) tb->stack + THREAD_STACK_ADJUSTMENT - tb->stack_size;
|
||||
}
|
||||
|
||||
void *_vm_tls_get(unsigned idx)
|
||||
{
|
||||
bh_assert(idx < BH_MAX_TLS_NUM);
|
||||
return TlsGetValue(tls_indexes[idx]);
|
||||
}
|
||||
|
||||
int _vm_tls_put(unsigned idx, void *tls)
|
||||
{
|
||||
BOOL r;
|
||||
|
||||
bh_assert(idx < BH_MAX_TLS_NUM);
|
||||
r = TlsSetValue(tls_indexes[idx], tls);
|
||||
return (r == FALSE) ? BHT_ERROR : BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_mutex_init(korp_mutex *mutex)
|
||||
{
|
||||
bh_assert(mutex);
|
||||
*mutex = CreateMutex(NULL, FALSE, NULL);
|
||||
return (*mutex == 0) ? BHT_ERROR : BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_mutex_destroy(korp_mutex *mutex)
|
||||
{
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
/* Returned error (e.g. ERROR_INVALID_HANDLE) from
|
||||
locking the mutex indicates some logic error present in
|
||||
the program somewhere.
|
||||
Don't try to recover error for an existing unknown error.*/
|
||||
void vm_mutex_lock(korp_mutex *mutex)
|
||||
{
|
||||
DWORD ret;
|
||||
|
||||
bh_assert(mutex);
|
||||
ret = WaitForSingleObject(*mutex, INFINITE);
|
||||
if (WAIT_FAILED == ret) {
|
||||
LOG_FATAL("vm mutex lock failed (ret=%d)!\n", GetLastError());
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
int vm_mutex_trylock(korp_mutex *mutex)
|
||||
{
|
||||
DWORD ret;
|
||||
|
||||
bh_assert(mutex);
|
||||
ret = WaitForSingleObject(*mutex, 0);
|
||||
if (WAIT_FAILED == ret) {
|
||||
LOG_FATAL("vm mutex lock failed (ret=%d)!\n", GetLastError());
|
||||
exit(-1);
|
||||
}
|
||||
return ret == WAIT_OBJECT_0 ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
/* Returned error (e.g. ERROR_INVALID_HANDLE) from
|
||||
unlocking the mutex indicates some logic error present
|
||||
in the program somewhere.
|
||||
Don't try to recover error for an existing unknown error.*/
|
||||
void vm_mutex_unlock(korp_mutex *mutex)
|
||||
{
|
||||
BOOL ret;
|
||||
|
||||
bh_assert(mutex);
|
||||
ret = ReleaseMutex(*mutex);
|
||||
if (FALSE == ret) {
|
||||
LOG_FATAL("vm mutex unlock failed (ret=%d)!\n", GetLastError());
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
#define BH_SEM_COUNT_MAX 0xFFFF
|
||||
|
||||
int _vm_sem_init(korp_sem *sem, unsigned int count)
|
||||
{
|
||||
bh_assert(sem);
|
||||
bh_assert(count <= BH_SEM_COUNT_MAX);
|
||||
*sem = CreateSemaphore(NULL, count, BH_SEM_COUNT_MAX, NULL);
|
||||
|
||||
return (*sem == NULL) ? BHT_ERROR : BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_sem_destroy(korp_sem *sem)
|
||||
{
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_sem_P(korp_sem *sem)
|
||||
{
|
||||
DWORD r;
|
||||
bh_assert(sem);
|
||||
r = WaitForSingleObject(*sem, INFINITE);
|
||||
|
||||
return (r == WAIT_FAILED) ? BHT_ERROR : BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_sem_reltimedP(korp_sem *sem, int mills)
|
||||
{
|
||||
DWORD r;
|
||||
bh_assert(sem);
|
||||
|
||||
if (mills == BHT_WAIT_FOREVER)
|
||||
mills = INFINITE;
|
||||
|
||||
r = WaitForSingleObject(*sem, (unsigned int) mills);
|
||||
|
||||
switch (r) {
|
||||
case WAIT_OBJECT_0:
|
||||
return BHT_OK;
|
||||
case WAIT_TIMEOUT:
|
||||
return BHT_TIMEDOUT;
|
||||
default:
|
||||
return BHT_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
int _vm_sem_V(korp_sem *sem)
|
||||
{
|
||||
BOOL r;
|
||||
bh_assert(sem);
|
||||
r = ReleaseSemaphore(*sem, 1, NULL);
|
||||
return (r == FALSE) ? BHT_ERROR : BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_init(korp_cond *cond)
|
||||
{
|
||||
bh_assert(cond);
|
||||
cond->waiting_count = 0;
|
||||
return vm_sem_init(&cond->s, 0);
|
||||
}
|
||||
|
||||
int _vm_cond_destroy(korp_cond *cond)
|
||||
{
|
||||
bh_assert(cond);
|
||||
return vm_sem_destroy(&cond->s);
|
||||
}
|
||||
|
||||
int _vm_cond_wait(korp_cond *cond, korp_mutex *mutex)
|
||||
{
|
||||
bh_assert(cond);
|
||||
bh_assert(mutex);
|
||||
|
||||
cond->waiting_count++;
|
||||
|
||||
vm_mutex_unlock(mutex);
|
||||
|
||||
if (vm_sem_P(&cond->s) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
vm_mutex_lock(mutex);
|
||||
|
||||
cond->waiting_count--;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
|
||||
{
|
||||
int r;
|
||||
|
||||
bh_assert(cond);
|
||||
bh_assert(mutex);
|
||||
|
||||
cond->waiting_count++;
|
||||
|
||||
vm_mutex_unlock(mutex);
|
||||
|
||||
r = vm_sem_reltimedP(&cond->s, mills);
|
||||
|
||||
if ((r != BHT_OK) && (r != BHT_TIMEDOUT))
|
||||
return BHT_ERROR;
|
||||
|
||||
vm_mutex_lock(mutex);
|
||||
|
||||
cond->waiting_count--;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int _vm_cond_signal(korp_cond *cond)
|
||||
{
|
||||
bh_assert(cond);
|
||||
|
||||
if (cond->waiting_count == 0)
|
||||
return BHT_OK;
|
||||
|
||||
if (vm_sem_V(&cond->s) != BHT_OK)
|
||||
return BHT_ERROR;
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_broadcast(korp_cond *cond)
|
||||
{
|
||||
/* FIXME: use pthread's API to implement this and above
|
||||
functions. */
|
||||
|
||||
unsigned count = cond->waiting_count;
|
||||
|
||||
for (; count > 0; count--)
|
||||
vm_sem_V(&cond->s);
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_thread_cancel(korp_tid thread)
|
||||
{
|
||||
/* FIXME: implement this with Windows API. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _vm_thread_join(korp_tid thread, void **value_ptr)
|
||||
{
|
||||
/* FIXME: implement this with Windows API. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _vm_thread_detach(korp_tid thread)
|
||||
{
|
||||
/* FIXME: implement this with Windows API. */
|
||||
return 0;
|
||||
}
|
||||
101
core/shared-lib/platform/win32/bh_time.c
Executable file
101
core/shared-lib/platform/win32/bh_time.c
Executable file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "bh_time.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <sys/timeb.h>
|
||||
|
||||
/* Since GetTickCount64 is not supported on Windows XP, use a temporary method
|
||||
* to solve the issue. However, GetTickCount return a DWORD value and overflow
|
||||
* may happen (http://msdn.microsoft.com/en-us/library/ms724408(v=vs.85).aspx).
|
||||
*
|
||||
* TODO: Implement GetTickCount64 on Windows XP by self or check overflow issues.
|
||||
*/
|
||||
#if (WINVER >= 0x0600)
|
||||
extern uint64 __stdcall GetTickCount64(void);
|
||||
#else
|
||||
extern uint32 __stdcall GetTickCount(void);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This function returns milliseconds per tick.
|
||||
* @return milliseconds per tick.
|
||||
*/
|
||||
uint64 _bh_time_get_tick_millisecond()
|
||||
{
|
||||
return 5;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function returns milliseconds after boot.
|
||||
* @return milliseconds after boot.
|
||||
*/
|
||||
uint64 _bh_time_get_boot_millisecond()
|
||||
{
|
||||
/* Since GetTickCount64 is not supported on Windows XP, use a temporary method
|
||||
* to solve the issue. However, GetTickCount return a DWORD value and overflow
|
||||
* may happen (http://msdn.microsoft.com/en-us/library/ms724408(v=vs.85).aspx).
|
||||
*
|
||||
* TODO: Implement GetTickCount64 on Windows XP by self or check overflow issues.
|
||||
*/
|
||||
#if (WINVER >= 0x0600)
|
||||
return GetTickCount64();
|
||||
#else
|
||||
return GetTickCount();
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* This function returns GMT time milliseconds since from 1970.1.1, AKA UNIX time.
|
||||
* @return milliseconds since from 1970.1.1.
|
||||
*/
|
||||
uint64 _bh_time_get_millisecond_from_1970()
|
||||
{
|
||||
struct timeb tp;
|
||||
ftime(&tp);
|
||||
|
||||
return ((uint64) tp.time) * 1000 + tp.millitm
|
||||
- (tp.dstflag == 0 ? 0 : 60 * 60 * 1000) + tp.timezone * 60 * 1000;
|
||||
}
|
||||
|
||||
size_t bh_time_strftime(char *s, size_t max, const char *format, int64 time)
|
||||
{
|
||||
time_t time_sec = time / 1000;
|
||||
struct timeb tp;
|
||||
struct tm local_time;
|
||||
|
||||
if (NULL == s)
|
||||
return 0;
|
||||
|
||||
ftime(&tp);
|
||||
time_sec -= tp.timezone * 60;
|
||||
if (localtime_s(&local_time, &time_sec) != 0)
|
||||
return 0;
|
||||
|
||||
return strftime(s, max, format, &local_time);
|
||||
}
|
||||
|
||||
int bh_time_get(uint8 *timeoff_info, int16 timeoff_info_len, uint32* time)
|
||||
{
|
||||
return BH_UNIMPLEMENTED;
|
||||
}
|
||||
|
||||
int bh_time_set(uint32 ntp, uint8 *timeoff_info)
|
||||
{
|
||||
return BH_UNIMPLEMENTED;
|
||||
}
|
||||
15
core/shared-lib/platform/zephyr/Makefile
Normal file
15
core/shared-lib/platform/zephyr/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
obj-y += bh_assert.o bh_definition.o bh_memory.o bh_platform_log.o bh_thread.o bh_time.o
|
||||
69
core/shared-lib/platform/zephyr/bh_assert.c
Normal file
69
core/shared-lib/platform/zephyr/bh_assert.c
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "bh_assert.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef BH_TEST
|
||||
#include <setjmp.h>
|
||||
#endif
|
||||
|
||||
#ifdef BH_TEST
|
||||
/* for exception throwing */
|
||||
jmp_buf bh_test_jb;
|
||||
#endif
|
||||
extern void abort(void);
|
||||
void bh_assert_internal(int v, const char *file_name, int line_number,
|
||||
const char *expr_string)
|
||||
{
|
||||
if (v)
|
||||
return;
|
||||
|
||||
if (!file_name)
|
||||
file_name = "NULL FILENAME";
|
||||
if (!expr_string)
|
||||
expr_string = "NULL EXPR_STRING";
|
||||
|
||||
printk("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
|
||||
file_name, line_number);
|
||||
|
||||
#ifdef BH_TEST
|
||||
longjmp(bh_test_jb, 1);
|
||||
#endif
|
||||
|
||||
abort();
|
||||
}
|
||||
|
||||
void bh_debug_internal(const char *file_name, int line_number, const char *fmt,
|
||||
...)
|
||||
{
|
||||
#ifndef JEFF_TEST_VERIFIER
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
bh_assert(file_name);
|
||||
|
||||
printf("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
|
||||
vprintf(fmt, args);
|
||||
|
||||
va_end(args);
|
||||
printf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
68
core/shared-lib/platform/zephyr/bh_definition.c
Normal file
68
core/shared-lib/platform/zephyr/bh_definition.c
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_definition.h"
|
||||
#include "bh_platform.h"
|
||||
|
||||
int bh_return(int ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define RSIZE_MAX 0x7FFFFFFF
|
||||
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
|
||||
{
|
||||
char *dest = (char*) s1;
|
||||
char *src = (char*) s2;
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (s1 == NULL || s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
if (s2 == NULL || n > s1max) {
|
||||
memset(dest, 0, s1max);
|
||||
return -1;
|
||||
}
|
||||
memcpy(dest, src, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int b_strcat_s(char * s1, size_t s1max, const char * s2)
|
||||
{
|
||||
if (NULL
|
||||
== s1|| NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1) || s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
strcat(s1, s2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int b_strcpy_s(char * s1, size_t s1max, const char * s2)
|
||||
{
|
||||
if (NULL
|
||||
== s1|| NULL == s2 || s1max < (strlen(s2) + 1) || s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
strcpy(s1, s2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
27
core/shared-lib/platform/zephyr/bh_platform.c
Executable file
27
core/shared-lib/platform/zephyr/bh_platform.c
Executable file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *bh_strdup(const char *s)
|
||||
{
|
||||
char *s1 = NULL;
|
||||
if (s && (s1 = bh_malloc(strlen(s) + 1)))
|
||||
memcpy(s1, s, strlen(s) + 1);
|
||||
return s1;
|
||||
}
|
||||
104
core/shared-lib/platform/zephyr/bh_platform.h
Normal file
104
core/shared-lib/platform/zephyr/bh_platform.h
Normal file
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _BH_PLATFORM_H
|
||||
#define _BH_PLATFORM_H
|
||||
|
||||
#include "bh_config.h"
|
||||
#include "bh_types.h"
|
||||
#include "bh_memory.h"
|
||||
#include <autoconf.h>
|
||||
#include <zephyr.h>
|
||||
#include <kernel.h>
|
||||
#include <misc/printk.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdarg.h>
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#ifndef CONFIG_NET_BUF_USER_DATA_SIZE
|
||||
#define CONFIG_NET_BUF_USER_DATA_SIZE 0
|
||||
#endif
|
||||
#include <net/net_pkt.h>
|
||||
#include <net/net_if.h>
|
||||
#include <net/net_core.h>
|
||||
#include <net/net_context.h>
|
||||
|
||||
/* Platform name */
|
||||
#define BH_PLATFORM "Zephyr"
|
||||
|
||||
#define BH_APPLET_PRESERVED_STACK_SIZE (2 * BH_KB)
|
||||
|
||||
/* Default thread priority */
|
||||
#define BH_THREAD_DEFAULT_PRIORITY 7
|
||||
|
||||
#define BH_ROUTINE_MODIFIER
|
||||
|
||||
/* Invalid thread tid */
|
||||
#define INVALID_THREAD_ID NULL
|
||||
|
||||
#define INVALID_SEM_ID SEM_FAILED
|
||||
#define BH_WAIT_FOREVER K_FOREVER
|
||||
|
||||
typedef uint64_t uint64;
|
||||
typedef int64_t int64;
|
||||
|
||||
typedef struct k_thread korp_thread;
|
||||
typedef korp_thread *korp_tid;
|
||||
typedef struct k_mutex korp_mutex;
|
||||
typedef struct k_sem korp_sem;
|
||||
|
||||
#define wa_malloc bh_malloc
|
||||
#define wa_free bh_free
|
||||
#define wa_strdup bh_strdup
|
||||
|
||||
struct bh_thread_wait_node;
|
||||
typedef struct bh_thread_wait_node *bh_thread_wait_list;
|
||||
typedef struct korp_cond {
|
||||
struct k_mutex wait_list_lock;
|
||||
bh_thread_wait_list thread_wait_list;
|
||||
} korp_cond;
|
||||
|
||||
typedef void* (*thread_start_routine_t)(void*);
|
||||
|
||||
#define wa_malloc bh_malloc
|
||||
#define wa_free bh_free
|
||||
|
||||
/* Unit test framework is based on C++, where the declaration of
|
||||
snprintf is different. */
|
||||
#ifndef __cplusplus
|
||||
int snprintf(char *buffer, size_t count, const char *format, ...);
|
||||
#endif
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL ((void*)0)
|
||||
#endif
|
||||
|
||||
#define bh_assert(x) \
|
||||
do { \
|
||||
if (!(x)) { \
|
||||
printk("bh_assert(%s, %d)\n", __func__, __LINE__);\
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
extern int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
|
||||
unsigned int n);
|
||||
extern int b_strcat_s(char * s1, size_t s1max, const char * s2);
|
||||
extern int b_strcpy_s(char * s1, size_t s1max, const char * s2);
|
||||
|
||||
#endif
|
||||
68
core/shared-lib/platform/zephyr/bh_platform_log.c
Normal file
68
core/shared-lib/platform/zephyr/bh_platform_log.c
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include <stdio.h>
|
||||
|
||||
struct out_context {
|
||||
int count;
|
||||
};
|
||||
|
||||
typedef int (*out_func_t)(int c, void *ctx);
|
||||
|
||||
extern void *__printk_get_hook(void);
|
||||
static int (*_char_out)(int) = NULL;
|
||||
|
||||
static int char_out(int c, struct out_context *ctx)
|
||||
{
|
||||
ctx->count++;
|
||||
if (_char_out == NULL) {
|
||||
_char_out = __printk_get_hook();
|
||||
}
|
||||
return _char_out(c);
|
||||
}
|
||||
|
||||
static int bh_vprintk(const char *fmt, va_list ap)
|
||||
{
|
||||
struct out_context ctx = { 0 };
|
||||
_vprintk((out_func_t) char_out, &ctx, fmt, ap);
|
||||
return ctx.count;
|
||||
}
|
||||
|
||||
void bh_log_emit(const char *fmt, va_list ap)
|
||||
{
|
||||
bh_vprintk(fmt, ap);
|
||||
}
|
||||
|
||||
int bh_fprintf(FILE *stream, const char *fmt, ...)
|
||||
{
|
||||
(void) stream;
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
va_start(ap, fmt);
|
||||
ret = bh_vprintk(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bh_fflush(void *stream)
|
||||
{
|
||||
(void) stream;
|
||||
return 0;
|
||||
}
|
||||
|
||||
537
core/shared-lib/platform/zephyr/bh_thread.c
Executable file
537
core/shared-lib/platform/zephyr/bh_thread.c
Executable file
@ -0,0 +1,537 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_thread.h"
|
||||
#include "bh_assert.h"
|
||||
#include "bh_log.h"
|
||||
#include "bh_memory.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct bh_thread_wait_node {
|
||||
struct k_sem sem;
|
||||
bh_thread_wait_list next;
|
||||
} bh_thread_wait_node;
|
||||
|
||||
typedef struct bh_thread_data {
|
||||
/* Next thread data */
|
||||
struct bh_thread_data *next;
|
||||
/* Zephyr thread handle */
|
||||
korp_tid tid;
|
||||
/* Jeff thread local root */
|
||||
void *tlr;
|
||||
/* Lock for waiting list */
|
||||
struct k_mutex wait_list_lock;
|
||||
/* Waiting list of other threads who are joining this thread */
|
||||
bh_thread_wait_list thread_wait_list;
|
||||
/* Thread stack size */
|
||||
unsigned stack_size;
|
||||
/* Thread stack */
|
||||
char stack[1];
|
||||
} bh_thread_data;
|
||||
|
||||
typedef struct bh_thread_obj {
|
||||
struct k_thread thread;
|
||||
/* Whether the thread is terminated and this thread object is to
|
||||
be freed in the future. */
|
||||
bool to_be_freed;
|
||||
struct bh_thread_obj *next;
|
||||
} bh_thread_obj;
|
||||
|
||||
static bool is_thread_sys_inited = false;
|
||||
|
||||
/* Thread data of supervisor thread */
|
||||
static bh_thread_data supervisor_thread_data;
|
||||
|
||||
/* Lock for thread data list */
|
||||
static struct k_mutex thread_data_lock;
|
||||
|
||||
/* Thread data list */
|
||||
static bh_thread_data *thread_data_list = NULL;
|
||||
|
||||
/* Lock for thread object list */
|
||||
static struct k_mutex thread_obj_lock;
|
||||
|
||||
/* Thread object list */
|
||||
static bh_thread_obj *thread_obj_list = NULL;
|
||||
|
||||
static void thread_data_list_add(bh_thread_data *thread_data)
|
||||
{
|
||||
k_mutex_lock(&thread_data_lock, K_FOREVER);
|
||||
if (!thread_data_list)
|
||||
thread_data_list = thread_data;
|
||||
else {
|
||||
/* If already in list, just return */
|
||||
bh_thread_data *p = thread_data_list;
|
||||
while (p) {
|
||||
if (p == thread_data) {
|
||||
k_mutex_unlock(&thread_data_lock);
|
||||
return;
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
|
||||
/* Set as head of list */
|
||||
thread_data->next = thread_data_list;
|
||||
thread_data_list = thread_data;
|
||||
}
|
||||
k_mutex_unlock(&thread_data_lock);
|
||||
}
|
||||
|
||||
static void thread_data_list_remove(bh_thread_data *thread_data)
|
||||
{
|
||||
k_mutex_lock(&thread_data_lock, K_FOREVER);
|
||||
if (thread_data_list) {
|
||||
if (thread_data_list == thread_data)
|
||||
thread_data_list = thread_data_list->next;
|
||||
else {
|
||||
/* Search and remove it from list */
|
||||
bh_thread_data *p = thread_data_list;
|
||||
while (p && p->next != thread_data)
|
||||
p = p->next;
|
||||
if (p && p->next == thread_data)
|
||||
p->next = p->next->next;
|
||||
}
|
||||
}
|
||||
k_mutex_unlock(&thread_data_lock);
|
||||
}
|
||||
|
||||
static bh_thread_data *
|
||||
thread_data_list_lookup(k_tid_t tid)
|
||||
{
|
||||
k_mutex_lock(&thread_data_lock, K_FOREVER);
|
||||
if (thread_data_list) {
|
||||
bh_thread_data *p = thread_data_list;
|
||||
while (p) {
|
||||
if (p->tid == tid) {
|
||||
/* Found */
|
||||
k_mutex_unlock(&thread_data_lock);
|
||||
return p;
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
k_mutex_unlock(&thread_data_lock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void thread_obj_list_add(bh_thread_obj *thread_obj)
|
||||
{
|
||||
k_mutex_lock(&thread_obj_lock, K_FOREVER);
|
||||
if (!thread_obj_list)
|
||||
thread_obj_list = thread_obj;
|
||||
else {
|
||||
/* Set as head of list */
|
||||
thread_obj->next = thread_obj_list;
|
||||
thread_obj_list = thread_obj;
|
||||
}
|
||||
k_mutex_unlock(&thread_obj_lock);
|
||||
}
|
||||
|
||||
static void thread_obj_list_reclaim()
|
||||
{
|
||||
bh_thread_obj *p, *p_prev;
|
||||
k_mutex_lock(&thread_obj_lock, K_FOREVER);
|
||||
p_prev = NULL;
|
||||
p = thread_obj_list;
|
||||
while (p) {
|
||||
if (p->to_be_freed) {
|
||||
if (p_prev == NULL) { /* p is the head of list */
|
||||
thread_obj_list = p->next;
|
||||
bh_free(p);
|
||||
p = thread_obj_list;
|
||||
} else { /* p is not the head of list */
|
||||
p_prev->next = p->next;
|
||||
bh_free(p);
|
||||
p = p_prev->next;
|
||||
}
|
||||
} else {
|
||||
p_prev = p;
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
k_mutex_unlock(&thread_obj_lock);
|
||||
}
|
||||
|
||||
int _vm_thread_sys_init()
|
||||
{
|
||||
if (is_thread_sys_inited)
|
||||
return BHT_OK;
|
||||
|
||||
k_mutex_init(&thread_data_lock);
|
||||
k_mutex_init(&thread_obj_lock);
|
||||
|
||||
/* Initialize supervisor thread data */
|
||||
memset(&supervisor_thread_data, 0, sizeof(supervisor_thread_data));
|
||||
supervisor_thread_data.tid = k_current_get();
|
||||
/* Set as head of thread data list */
|
||||
thread_data_list = &supervisor_thread_data;
|
||||
|
||||
is_thread_sys_inited = true;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
void vm_thread_sys_destroy(void)
|
||||
{
|
||||
if (is_thread_sys_inited) {
|
||||
is_thread_sys_inited = false;
|
||||
}
|
||||
}
|
||||
|
||||
static bh_thread_data *
|
||||
thread_data_current()
|
||||
{
|
||||
k_tid_t tid = k_current_get();
|
||||
return thread_data_list_lookup(tid);
|
||||
}
|
||||
|
||||
static void vm_thread_cleanup(void)
|
||||
{
|
||||
bh_thread_data *thread_data = thread_data_current();
|
||||
|
||||
bh_assert(thread_data != NULL);
|
||||
k_mutex_lock(&thread_data->wait_list_lock, K_FOREVER);
|
||||
if (thread_data->thread_wait_list) {
|
||||
/* Signal each joining thread */
|
||||
bh_thread_wait_list head = thread_data->thread_wait_list;
|
||||
while (head) {
|
||||
bh_thread_wait_list next = head->next;
|
||||
k_sem_give(&head->sem);
|
||||
bh_free(head);
|
||||
head = next;
|
||||
}
|
||||
thread_data->thread_wait_list = NULL;
|
||||
}
|
||||
k_mutex_unlock(&thread_data->wait_list_lock);
|
||||
|
||||
thread_data_list_remove(thread_data);
|
||||
/* Set flag to true for the next thread creating to
|
||||
free the thread object */
|
||||
((bh_thread_obj*) thread_data->tid)->to_be_freed = true;
|
||||
bh_free(thread_data);
|
||||
}
|
||||
|
||||
static void vm_thread_wrapper(void *start, void *arg, void *thread_data)
|
||||
{
|
||||
/* Set thread custom data */
|
||||
((bh_thread_data*) thread_data)->tid = k_current_get();
|
||||
thread_data_list_add(thread_data);
|
||||
|
||||
((thread_start_routine_t) start)(arg);
|
||||
vm_thread_cleanup();
|
||||
}
|
||||
|
||||
int _vm_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
|
||||
unsigned int stack_size)
|
||||
{
|
||||
return _vm_thread_create_with_prio(p_tid, start, arg, stack_size,
|
||||
BH_THREAD_DEFAULT_PRIORITY);
|
||||
}
|
||||
|
||||
int _vm_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
|
||||
void *arg, unsigned int stack_size, int prio)
|
||||
{
|
||||
korp_tid tid;
|
||||
bh_thread_data *thread_data;
|
||||
unsigned thread_data_size;
|
||||
|
||||
if (!p_tid || !stack_size)
|
||||
return BHT_ERROR;
|
||||
|
||||
/* Free the thread objects of terminated threads */
|
||||
thread_obj_list_reclaim();
|
||||
|
||||
/* Create and initialize thread object */
|
||||
if (!(tid = bh_malloc(sizeof(bh_thread_obj))))
|
||||
return BHT_ERROR;
|
||||
|
||||
memset(tid, 0, sizeof(bh_thread_obj));
|
||||
|
||||
/* Create and initialize thread data */
|
||||
thread_data_size = offsetof(bh_thread_data, stack) + stack_size;
|
||||
if (!(thread_data = bh_malloc(thread_data_size))) {
|
||||
bh_free(tid);
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
memset(thread_data, 0, thread_data_size);
|
||||
k_mutex_init(&thread_data->wait_list_lock);
|
||||
thread_data->stack_size = stack_size;
|
||||
thread_data->tid = tid;
|
||||
|
||||
/* Create the thread */
|
||||
if (!((tid = k_thread_create(tid, (k_thread_stack_t *) thread_data->stack,
|
||||
stack_size, vm_thread_wrapper, start, arg, thread_data, prio, 0,
|
||||
K_NO_WAIT)))) {
|
||||
bh_free(tid);
|
||||
bh_free(thread_data);
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
bh_assert(tid == thread_data->tid);
|
||||
|
||||
/* Set thread custom data */
|
||||
thread_data_list_add(thread_data);
|
||||
thread_obj_list_add((bh_thread_obj*) tid);
|
||||
*p_tid = tid;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
korp_tid _vm_self_thread()
|
||||
{
|
||||
return (korp_tid) k_current_get();
|
||||
}
|
||||
|
||||
void vm_thread_exit(void * code)
|
||||
{
|
||||
(void) code;
|
||||
korp_tid self = vm_self_thread();
|
||||
vm_thread_cleanup();
|
||||
k_thread_abort((k_tid_t) self);
|
||||
}
|
||||
|
||||
int _vm_thread_cancel(korp_tid thread)
|
||||
{
|
||||
k_thread_abort((k_tid_t) thread);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _vm_thread_join(korp_tid thread, void **value_ptr, int mills)
|
||||
{
|
||||
(void) value_ptr;
|
||||
bh_thread_data *thread_data;
|
||||
bh_thread_wait_node *node;
|
||||
|
||||
/* Create wait node and append it to wait list */
|
||||
if (!(node = bh_malloc(sizeof(bh_thread_wait_node))))
|
||||
return BHT_ERROR;
|
||||
|
||||
k_sem_init(&node->sem, 0, 1);
|
||||
node->next = NULL;
|
||||
|
||||
/* Get thread data */
|
||||
thread_data = thread_data_list_lookup(thread);
|
||||
bh_assert(thread_data != NULL);
|
||||
|
||||
k_mutex_lock(&thread_data->wait_list_lock, K_FOREVER);
|
||||
if (!thread_data->thread_wait_list)
|
||||
thread_data->thread_wait_list = node;
|
||||
else {
|
||||
/* Add to end of waiting list */
|
||||
bh_thread_wait_node *p = thread_data->thread_wait_list;
|
||||
while (p->next)
|
||||
p = p->next;
|
||||
p->next = node;
|
||||
}
|
||||
k_mutex_unlock(&thread_data->wait_list_lock);
|
||||
|
||||
/* Wait the sem */
|
||||
k_sem_take(&node->sem, mills);
|
||||
|
||||
/* Wait some time for the thread to be actually terminated */
|
||||
k_sleep(100);
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_thread_detach(korp_tid thread)
|
||||
{
|
||||
(void) thread;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
void *_vm_tls_get(unsigned idx)
|
||||
{
|
||||
(void) idx;
|
||||
bh_thread_data *thread_data;
|
||||
|
||||
bh_assert(idx == 0);
|
||||
thread_data = thread_data_current();
|
||||
|
||||
return thread_data ? thread_data->tlr : NULL;
|
||||
}
|
||||
|
||||
int _vm_tls_put(unsigned idx, void * tls)
|
||||
{
|
||||
bh_thread_data *thread_data;
|
||||
|
||||
(void) idx;
|
||||
bh_assert(idx == 0);
|
||||
thread_data = thread_data_current();
|
||||
bh_assert(thread_data != NULL);
|
||||
|
||||
thread_data->tlr = tls;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_mutex_init(korp_mutex *mutex)
|
||||
{
|
||||
(void) mutex;
|
||||
k_mutex_init(mutex);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_recursive_mutex_init(korp_mutex *mutex)
|
||||
{
|
||||
k_mutex_init(mutex);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_mutex_destroy(korp_mutex *mutex)
|
||||
{
|
||||
(void) mutex;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
void vm_mutex_lock(korp_mutex *mutex)
|
||||
{
|
||||
k_mutex_lock(mutex, K_FOREVER);
|
||||
}
|
||||
|
||||
int vm_mutex_trylock(korp_mutex *mutex)
|
||||
{
|
||||
return k_mutex_lock(mutex, K_NO_WAIT);
|
||||
}
|
||||
|
||||
void vm_mutex_unlock(korp_mutex *mutex)
|
||||
{
|
||||
k_mutex_unlock(mutex);
|
||||
}
|
||||
|
||||
int _vm_sem_init(korp_sem* sem, unsigned int c)
|
||||
{
|
||||
k_sem_init(sem, 0, c);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_sem_destroy(korp_sem *sem)
|
||||
{
|
||||
(void) sem;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_sem_wait(korp_sem *sem)
|
||||
{
|
||||
return k_sem_take(sem, K_FOREVER);
|
||||
}
|
||||
|
||||
int _vm_sem_reltimedwait(korp_sem *sem, int mills)
|
||||
{
|
||||
return k_sem_take(sem, mills);
|
||||
}
|
||||
|
||||
int _vm_sem_post(korp_sem *sem)
|
||||
{
|
||||
k_sem_give(sem);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_init(korp_cond *cond)
|
||||
{
|
||||
k_mutex_init(&cond->wait_list_lock);
|
||||
cond->thread_wait_list = NULL;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_destroy(korp_cond *cond)
|
||||
{
|
||||
(void) cond;
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
static int _vm_cond_wait_internal(korp_cond *cond, korp_mutex *mutex,
|
||||
bool timed, int mills)
|
||||
{
|
||||
bh_thread_wait_node *node;
|
||||
|
||||
/* Create wait node and append it to wait list */
|
||||
if (!(node = bh_malloc(sizeof(bh_thread_wait_node))))
|
||||
return BHT_ERROR;
|
||||
|
||||
k_sem_init(&node->sem, 0, 1);
|
||||
node->next = NULL;
|
||||
|
||||
k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
|
||||
if (!cond->thread_wait_list)
|
||||
cond->thread_wait_list = node;
|
||||
else {
|
||||
/* Add to end of wait list */
|
||||
bh_thread_wait_node *p = cond->thread_wait_list;
|
||||
while (p->next)
|
||||
p = p->next;
|
||||
p->next = node;
|
||||
}
|
||||
k_mutex_unlock(&cond->wait_list_lock);
|
||||
|
||||
/* Unlock mutex, wait sem and lock mutex again */
|
||||
k_mutex_unlock(mutex);
|
||||
k_sem_take(&node->sem, timed ? mills : K_FOREVER);
|
||||
k_mutex_lock(mutex, K_FOREVER);
|
||||
|
||||
/* Remove wait node from wait list */
|
||||
k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
|
||||
if (cond->thread_wait_list == node)
|
||||
cond->thread_wait_list = node->next;
|
||||
else {
|
||||
/* Remove from the wait list */
|
||||
bh_thread_wait_node *p = cond->thread_wait_list;
|
||||
while (p->next != node)
|
||||
p = p->next;
|
||||
p->next = node->next;
|
||||
}
|
||||
bh_free(node);
|
||||
k_mutex_unlock(&cond->wait_list_lock);
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_wait(korp_cond *cond, korp_mutex *mutex)
|
||||
{
|
||||
return _vm_cond_wait_internal(cond, mutex, false, 0);
|
||||
}
|
||||
|
||||
int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
|
||||
{
|
||||
return _vm_cond_wait_internal(cond, mutex, true, mills);
|
||||
}
|
||||
|
||||
int _vm_cond_signal(korp_cond *cond)
|
||||
{
|
||||
/* Signal the head wait node of wait list */
|
||||
k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
|
||||
if (cond->thread_wait_list)
|
||||
k_sem_give(&cond->thread_wait_list->sem);
|
||||
k_mutex_unlock(&cond->wait_list_lock);
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
int _vm_cond_broadcast(korp_cond *cond)
|
||||
{
|
||||
/* Signal each wait node of wait list */
|
||||
k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
|
||||
if (cond->thread_wait_list) {
|
||||
bh_thread_wait_node *p = cond->thread_wait_list;
|
||||
while (p) {
|
||||
k_sem_give(&p->sem);
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
k_mutex_unlock(&cond->wait_list_lock);
|
||||
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
57
core/shared-lib/platform/zephyr/bh_time.c
Executable file
57
core/shared-lib/platform/zephyr/bh_time.c
Executable file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bh_time.h"
|
||||
|
||||
/*
|
||||
* This function returns milliseconds per tick.
|
||||
* @return milliseconds per tick.
|
||||
*/
|
||||
uint64 _bh_time_get_tick_millisecond()
|
||||
{
|
||||
return k_uptime_get_32();
|
||||
}
|
||||
|
||||
/*
|
||||
* This function returns milliseconds after boot.
|
||||
* @return milliseconds after boot.
|
||||
*/
|
||||
uint64 _bh_time_get_boot_millisecond()
|
||||
{
|
||||
return k_uptime_get_32();
|
||||
}
|
||||
|
||||
uint64 _bh_time_get_millisecond_from_1970()
|
||||
{
|
||||
return k_uptime_get();
|
||||
}
|
||||
|
||||
size_t _bh_time_strftime(char *str, size_t max, const char *format, int64 time)
|
||||
{
|
||||
(void) format;
|
||||
(void) time;
|
||||
uint32 t = k_uptime_get_32();
|
||||
int h, m, s;
|
||||
|
||||
t = t % (24 * 60 * 60);
|
||||
h = t / (60 * 60);
|
||||
t = t % (60 * 60);
|
||||
m = t / 60;
|
||||
s = t % 60;
|
||||
|
||||
return snprintf(str, max, "%02d:%02d:%02d", h, m, s);
|
||||
}
|
||||
|
||||
24
core/shared-lib/platform/zephyr/shared_platform.cmake
Normal file
24
core/shared-lib/platform/zephyr/shared_platform.cmake
Normal file
@ -0,0 +1,24 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${PLATFORM_SHARED_DIR})
|
||||
include_directories(${PLATFORM_SHARED_DIR}/../include)
|
||||
|
||||
|
||||
file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c)
|
||||
|
||||
set (PLATFORM_SHARED_SOURCE ${source_all})
|
||||
|
||||
Reference in New Issue
Block a user