re-org platform APIs, simplify porting process (#201)

Co-authored-by: Xu Jun <jun1.xu@intel.com>
This commit is contained in:
Xu Jun
2020-03-16 16:43:57 +08:00
committed by GitHub
parent ef5ceffe71
commit f1a0e75ab7
177 changed files with 2954 additions and 7904 deletions

View File

@ -4,8 +4,6 @@
*/
#include "runtime_timer.h"
#include "bh_thread.h"
#include "bh_time.h"
#define PRINT(...)
//#define PRINT printf
@ -34,13 +32,18 @@ struct _timer_ctx {
check_timer_expiry_f refresh_checker;
};
uint64 bh_get_tick_ms()
{
return os_time_get_boot_microsecond() / 1000;
}
uint32 bh_get_elpased_ms(uint32 * last_system_clock)
{
uint32 elpased_ms;
// attention: the bh_get_tick_ms() return 64 bits integer.
// but the bh_get_elpased_ms() is designed to use 32 bits clock count.
uint32 now = (uint32) bh_get_tick_ms();
uint32 now = (uint32)bh_get_tick_ms();
// system clock overrun
if (now < *last_system_clock) {
@ -57,7 +60,7 @@ uint32 bh_get_elpased_ms(uint32 * last_system_clock)
static app_timer_t * remove_timer_from(timer_ctx_t ctx, uint32 timer_id,
bool active_list)
{
vm_mutex_lock(&ctx->mutex);
os_mutex_lock(&ctx->mutex);
app_timer_t ** head;
if (active_list)
head = &ctx->g_app_timers;
@ -76,7 +79,7 @@ static app_timer_t * remove_timer_from(timer_ctx_t ctx, uint32 timer_id,
prev->next = t->next;
PRINT("removed timer [%d] after [%d] from list %d\n", t->id, prev->id, active_list);
}
vm_mutex_unlock(&ctx->mutex);
os_mutex_unlock(&ctx->mutex);
if (active_list && prev == NULL && ctx->refresh_checker)
ctx->refresh_checker(ctx);
@ -88,7 +91,7 @@ static app_timer_t * remove_timer_from(timer_ctx_t ctx, uint32 timer_id,
}
}
vm_mutex_unlock(&ctx->mutex);
os_mutex_unlock(&ctx->mutex);
return NULL;
}
@ -111,7 +114,7 @@ static app_timer_t * remove_timer(timer_ctx_t ctx, uint32 timer_id,
static void reschedule_timer(timer_ctx_t ctx, app_timer_t * timer)
{
vm_mutex_lock(&ctx->mutex);
os_mutex_lock(&ctx->mutex);
app_timer_t * t = ctx->g_app_timers;
app_timer_t * prev = NULL;
@ -130,7 +133,7 @@ static void reschedule_timer(timer_ctx_t ctx, app_timer_t * timer)
PRINT("rescheduled timer [%d] after [%d]\n", timer->id, prev->id);
}
vm_mutex_unlock(&ctx->mutex);
os_mutex_unlock(&ctx->mutex);
// ensure the refresh_checker() is called out of the lock
if (prev == NULL && ctx->refresh_checker)
@ -154,7 +157,7 @@ static void reschedule_timer(timer_ctx_t ctx, app_timer_t * timer)
PRINT("rescheduled timer [%d] as first\n", timer->id);
}
vm_mutex_unlock(&ctx->mutex);
os_mutex_unlock(&ctx->mutex);
// ensure the refresh_checker() is called out of the lock
if (prev == NULL && ctx->refresh_checker)
@ -165,11 +168,11 @@ static void reschedule_timer(timer_ctx_t ctx, app_timer_t * timer)
static void release_timer(timer_ctx_t ctx, app_timer_t * t)
{
if (ctx->pre_allocated) {
vm_mutex_lock(&ctx->mutex);
os_mutex_lock(&ctx->mutex);
t->next = ctx->free_timers;
ctx->free_timers = t;
PRINT("recycle timer :%d\n", t->id);
vm_mutex_unlock(&ctx->mutex);
os_mutex_unlock(&ctx->mutex);
} else {
PRINT("destroy timer :%d\n", t->id);
BH_FREE(t);
@ -220,8 +223,8 @@ timer_ctx_t create_timer_ctx(timer_callback_f timer_handler,
prealloc_num--;
}
vm_cond_init(&ctx->cond);
vm_mutex_init(&ctx->mutex);
os_cond_init(&ctx->cond);
os_mutex_init(&ctx->mutex);
PRINT("timer ctx created. pre-alloc: %d\n", ctx->pre_allocated);
@ -247,8 +250,8 @@ void destroy_timer_ctx(timer_ctx_t ctx)
cleanup_app_timers(ctx);
vm_cond_destroy(&ctx->cond);
vm_mutex_destroy(&ctx->mutex);
os_cond_destroy(&ctx->cond);
os_mutex_destroy(&ctx->mutex);
BH_FREE(ctx);
}
@ -259,10 +262,10 @@ unsigned int timer_ctx_get_owner(timer_ctx_t ctx)
void add_idle_timer(timer_ctx_t ctx, app_timer_t * timer)
{
vm_mutex_lock(&ctx->mutex);
os_mutex_lock(&ctx->mutex);
timer->next = ctx->idle_timers;
ctx->idle_timers = timer;
vm_mutex_unlock(&ctx->mutex);
os_mutex_unlock(&ctx->mutex);
}
uint32 sys_create_timer(timer_ctx_t ctx, int interval, bool is_period,
@ -376,21 +379,21 @@ int get_expiry_ms(timer_ctx_t ctx)
int ms_to_next_expiry;
uint64 now = bh_get_tick_ms();
vm_mutex_lock(&ctx->mutex);
os_mutex_lock(&ctx->mutex);
if (ctx->g_app_timers == NULL)
ms_to_next_expiry = 7 * 24 * 60 * 60 * 1000; // 1 week
else if (ctx->g_app_timers->expiry >= now)
ms_to_next_expiry = (int)(ctx->g_app_timers->expiry - now);
else
ms_to_next_expiry = 0;
vm_mutex_unlock(&ctx->mutex);
os_mutex_unlock(&ctx->mutex);
return ms_to_next_expiry;
}
int check_app_timers(timer_ctx_t ctx)
{
vm_mutex_lock(&ctx->mutex);
os_mutex_lock(&ctx->mutex);
app_timer_t * t = ctx->g_app_timers;
app_timer_t * expired = NULL;
@ -409,7 +412,7 @@ int check_app_timers(timer_ctx_t ctx)
break;
}
}
vm_mutex_unlock(&ctx->mutex);
os_mutex_unlock(&ctx->mutex);
handle_expired_timers(ctx, expired);
@ -418,11 +421,11 @@ int check_app_timers(timer_ctx_t ctx)
void cleanup_app_timers(timer_ctx_t ctx)
{
vm_mutex_lock(&ctx->mutex);
os_mutex_lock(&ctx->mutex);
release_timer_list(&ctx->g_app_timers);
release_timer_list(&ctx->idle_timers);
vm_mutex_unlock(&ctx->mutex);
os_mutex_unlock(&ctx->mutex);
}