From f170adb2762f80fd9805d55f5f728f7f5dceed43 Mon Sep 17 00:00:00 2001 From: Gavin1012 Date: Mon, 19 Jul 2021 19:20:06 +0800 Subject: [PATCH] enable dynamic global context for multi-external-context Signed-off-by: Gavin1012 --- jerry-core/api/external-context-helpers.c | 4 +- jerry-core/api/generate-bytecode.c | 6 +- jerry-core/api/generate-bytecode.h | 5 +- jerry-core/jcontext/jcontext.c | 2 + jerry-core/jcontext/jcontext.h | 3 +- jerry-port/default/default-external-context.c | 71 +++++-------------- 6 files changed, 30 insertions(+), 61 deletions(-) diff --git a/jerry-core/api/external-context-helpers.c b/jerry-core/api/external-context-helpers.c index 15b7258e..6c840dc4 100755 --- a/jerry-core/api/external-context-helpers.c +++ b/jerry-core/api/external-context-helpers.c @@ -4,7 +4,7 @@ * Create: 2020/11/17 */ -#ifdef JERRY_IAR_JUPITER +#if defined (JERRY_IAR_JUPITER) && (JERRY_EXTERNAL_CONTEXT == 1) #include "jcontext.h" #include "config-jupiter.h" @@ -78,4 +78,4 @@ jerry_external_context_init (uint32_t heap_size, /**< the size of heap */ jerry_port_default_set_current_context (external_context); } -#endif // JERRY_IAR_JUPITER +#endif // defined (JERRY_IAR_JUPITER) && (JERRY_EXTERNAL_CONTEXT == 1) diff --git a/jerry-core/api/generate-bytecode.c b/jerry-core/api/generate-bytecode.c index 90a01cb1..064e9a72 100755 --- a/jerry-core/api/generate-bytecode.c +++ b/jerry-core/api/generate-bytecode.c @@ -287,7 +287,7 @@ EXECRES walk_directory(char* filefolder) { return EXCE_ACE_JERRY_INPUT_PATH_ERROR; } if ((start_folder = (char*)OhosMalloc(MEM_TYPE_JERRY, filefolder_len)) == NULL) { - return EXEC_ACE_JERRY_MALLOC_ERROR; + return EXCE_ACE_JERRY_MALLOC_ERROR; } if (strcpy_s(start_folder, filefolder_len, filefolder) != 0) { OhosFree(start_folder); @@ -427,7 +427,7 @@ EXECRES walk_del_bytecode(char* filefolder) { return EXCE_ACE_JERRY_INPUT_PATH_ERROR; } if ((start_folder = (char*)OhosMalloc(MEM_TYPE_JERRY, filefolder_len)) == NULL) { - return EXEC_ACE_JERRY_MALLOC_ERROR; + return EXCE_ACE_JERRY_MALLOC_ERROR; } if (strcpy_s(start_folder, filefolder_len, filefolder) != 0) { OhosFree(start_folder); @@ -506,7 +506,7 @@ EXECRES walk_del_bytecode(char* filefolder) { free_link(head); curr = NULL; end = NULL; - return EXEC_ACE_JERRY_UNLINKFILE_ERROR; + return EXCE_ACE_JERRY_UNLINKFILE_ERROR; } else { OhosFree(input_file_path); input_file_path = NULL; diff --git a/jerry-core/api/generate-bytecode.h b/jerry-core/api/generate-bytecode.h index 42399298..566ad2bf 100755 --- a/jerry-core/api/generate-bytecode.h +++ b/jerry-core/api/generate-bytecode.h @@ -14,7 +14,7 @@ typedef enum { EXCE_ACE_JERRY_EXEC_OK = 0, // program function OK EXCE_ACE_JERRY_NULL_PATH, // null path for generateing snapshot - EXEC_ACE_JERRY_MALLOC_ERROR, // error when malloc + EXCE_ACE_JERRY_MALLOC_ERROR, // error when malloc EXCE_ACE_JERRY_INPUT_PATH_ERROR, // passed input path is NULL OR open failed EXCE_ACE_JERRY_INPUT_PATH_NOT_DIR, // passed input path is not a directory EXCE_ACE_JERRY_OPEN_DIR_FAILED, // open directory failed @@ -28,12 +28,11 @@ typedef enum { EXCE_ACE_JERRY_SPRINTFS_VERSION_ERROR, // error when sprintf_s for jerry_version EXCE_ACE_JERRY_GET_FILE_STAT_ERROR, // error when getting file stat EXCE_ACE_JERRY_LINKLIST_ERROR, // error when malloc for list node - EXEC_ACE_JERRY_UNLINKFILE_ERROR, // error when unlink bytecode file + EXCE_ACE_JERRY_UNLINKFILE_ERROR, // error when unlink bytecode file } EXECRES; typedef struct { - uint32_t task_id; jerry_context_t *context_p; } ContextRecord; diff --git a/jerry-core/jcontext/jcontext.c b/jerry-core/jcontext/jcontext.c index 30123fed..f6ce5383 100755 --- a/jerry-core/jcontext/jcontext.c +++ b/jerry-core/jcontext/jcontext.c @@ -46,6 +46,8 @@ jmem_heap_t jerry_global_heap JERRY_ATTR_ALIGNED (JMEM_ALIGNMENT) JERRY_ATTR_GLO #endif /* !ENABLED (JERRY_SYSTEM_ALLOCATOR) */ +#else /* ENABLED (JERRY_EXTERNAL_CONTEXT) */ +jerry_context_t *jerry_dynamic_global_context_p = NULL; #endif /* !ENABLED (JERRY_EXTERNAL_CONTEXT) */ /** diff --git a/jerry-core/jcontext/jcontext.h b/jerry-core/jcontext/jcontext.h index b95d013b..494a4310 100755 --- a/jerry-core/jcontext/jcontext.h +++ b/jerry-core/jcontext/jcontext.h @@ -233,8 +233,9 @@ struct jerry_context_t /* * This part is for JerryScript which uses external context. */ +extern jerry_context_t *jerry_dynamic_global_context_p; -#define JERRY_CONTEXT(field) (jerry_port_get_current_context ()->field) +#define JERRY_CONTEXT(field) (jerry_dynamic_global_context_p->field) #if !ENABLED (JERRY_SYSTEM_ALLOCATOR) diff --git a/jerry-port/default/default-external-context.c b/jerry-port/default/default-external-context.c index c0253302..817f0d18 100755 --- a/jerry-port/default/default-external-context.c +++ b/jerry-port/default/default-external-context.c @@ -16,27 +16,20 @@ #include "jerryscript-port.h" #include "jerryscript-port-default.h" -#ifdef JERRY_FOR_IAR_CONFIG +#if defined (JERRY_FOR_IAR_CONFIG) && (JERRY_EXTERNAL_CONTEXT == 1) #include "generate-bytecode.h" #include "los_task.h" +#include "target_config.h" /** - * use static Array to record the correspondence between task id and jerry-heap/context + * use array to record the correspondence between task id and jerry-heap/context */ -#define MAX_CONTEXT_NUM 8 -#define CONTEXT_NO_EXIST -1 +#define MAX_CONTEXT_NUM (LOSCFG_BASE_CORE_TSK_LIMIT+1) +ContextRecord g_contextRecords[MAX_CONTEXT_NUM] = {0}; -static uint8_t g_contextRecordCount = 0; -static ContextRecord g_contextRecords[MAX_CONTEXT_NUM] = {0}; - -uint8_t getContextRecordCount() { - return g_contextRecordCount; -} - -ContextRecord* getContextRecord() { - return g_contextRecords; -} +void jerry_switch_context(); +extern jerry_context_t *jerry_dynamic_global_context_p; /** * set context function: store task id and context @@ -44,63 +37,37 @@ ContextRecord* getContextRecord() { void jerry_port_default_set_current_context (jerry_context_t *context_p) /**< store created context */ { - LOS_TaskLock(); uint32_t curTaskId = LOS_CurTaskIDGet(); - g_contextRecordCount++; - g_contextRecords[g_contextRecordCount-1].task_id = curTaskId; - g_contextRecords[g_contextRecordCount-1].context_p = context_p; - LOS_TaskUnlock(); + g_contextRecords[curTaskId].context_p = context_p; + jerry_dynamic_global_context_p = context_p; } -int get_context_record_index(void) +void jerry_switch_context() { - uint32_t curTaskId = LOS_CurTaskIDGet(); - - for (int i = 0; i < g_contextRecordCount; i++) { - if (g_contextRecords[i].task_id == curTaskId) { - return i; - } - } - return CONTEXT_NO_EXIST; + jerry_dynamic_global_context_p = g_contextRecords[LOS_NextTaskIDGet()].context_p; } /** - * when task over, delete its record in array + * when task ends, the context_pointer point to NULL */ void jerry_port_default_remove_current_context_record () /**< remove current task's context record in Array */ { - LOS_TaskLock(); - - int index = get_context_record_index(); - - for (int i = index; i < g_contextRecordCount-1; i++) { - g_contextRecords[i].task_id = g_contextRecords[i+1].task_id; - g_contextRecords[i].context_p = g_contextRecords[i+1].context_p; - } - memset_s(&g_contextRecords[g_contextRecordCount-1], 0, sizeof(g_contextRecords[g_contextRecordCount-1])); - g_contextRecordCount--; - LOS_TaskUnlock(); + uint32_t curTaskId = LOS_CurTaskIDGet(); + g_contextRecords[curTaskId].context_p = NULL; + jerry_dynamic_global_context_p = NULL; } /** - * key: rewrite get_current_context function; get the context pointer according to task id + * key: global dynamic context_p for current context */ jerry_context_t * jerry_port_get_current_context (void) /**< points to current task's context */ { - jerry_context_t * context = NULL; - LOS_TaskLock(); - - int index = get_context_record_index(); - - context = g_contextRecords[index].context_p; - - LOS_TaskUnlock(); - return context; + return jerry_dynamic_global_context_p; } -#else // not defined JERRY_FOR_IAR_CONFIG +#else // not defined JERRY_FOR_IAR_CONFIG || not enabled JERRY_EXTERNAL_CONTEXT /** * Pointer to the current context. @@ -128,4 +95,4 @@ jerry_port_get_current_context (void) return current_context_p; } /* jerry_port_get_current_context */ -#endif +#endif // defined (JERRY_FOR_IAR_CONFIG) && (JERRY_EXTERNAL_CONTEXT == 1) -- Gitee