From b19004817c68da56a757bc5f68dbbd3e8d96ab39 Mon Sep 17 00:00:00 2001 From: Natie van Rooyen Date: Sun, 18 Nov 2012 11:29:32 +0100 Subject: [PATCH] Added thread exit routine. --- ports/atomvm/atomport.c | 6 ++--- ports/atomvm/atomvm.c | 59 +++++++++++++++++++++++++++++++---------- ports/atomvm/atomvm.h | 4 ++- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/ports/atomvm/atomport.c b/ports/atomvm/atomport.c index 55d6d69..8daac41 100644 --- a/ports/atomvm/atomport.c +++ b/ports/atomvm/atomport.c @@ -32,7 +32,6 @@ #include "windows.h" /** Forward declarations */ -static void thread_shell (void); DWORD WINAPI cntrl_thread_proc (LPVOID lpParameter) ; /* Global data */ @@ -73,7 +72,7 @@ cntrl_thread_proc (LPVOID lpParameter) * */ void -thread_shell (void) +thread_shell (uint32_t arg) { ATOM_TCB *curr_tcb; @@ -111,7 +110,8 @@ archThreadContextInit (ATOM_TCB *tcb_ptr, void *stack_top, void (*entry_point)(u tcb_ptr->entry_param = entry_param ; tcb_ptr->entry_point = entry_point ; - atomvmContextCreate (&tcb_ptr->context, (unsigned int )stack_top, (unsigned int )thread_shell) ; + tcb_ptr->context = atomvmContextCreate (1) ; + atomvmContextInit (tcb_ptr->context, (unsigned int *)stack_top, thread_shell, entry_param, 0) ; } diff --git a/ports/atomvm/atomvm.c b/ports/atomvm/atomvm.c index 960b13e..3785212 100644 --- a/ports/atomvm/atomvm.c +++ b/ports/atomvm/atomvm.c @@ -550,15 +550,12 @@ callbackContextCreate (PATOMVM patomvm, PATOMVM_CALLBACK callback) * This function creates a atomvm thread context that can be scheduled * by atomvmContextSwitch. * -* @param[out] context Handle to the context of the thread that are allocated -* by the caller. -* @param[in] stack Stack top. -* @param[in] entry Entry point using the default caling convention of the compiler. +* @param[in] interrupt_mask initial interrupt mask of the thread. * -* @return Zero on failure, try to call GetLastError(). +* @return Handle to the context of the thread created. */ -uint32_t -atomvmContextCreate (HATOMVM_CONTEXT* atomvm_context, uint32_t stack, uint32_t entry) +HATOMVM_CONTEXT +atomvmContextCreate (uint32_t interrupt_mask) { uint32_t res ; PATOMVM patomvm = getAtomvm () ; @@ -567,21 +564,55 @@ atomvmContextCreate (HATOMVM_CONTEXT* atomvm_context, uint32_t stack, uint32_t e ATOMVM_CALLBACK_CONTEXT context_init ; context_init.pcontext = new_context ; - new_context->interrupt_mask = 1 ; - + new_context->interrupt_mask = interrupt_mask ; + new_context->thread_id = (uint32_t) -1 ; res = invokeCallback (patomvm, callbackContextCreate, (PATOMVM_CALLBACK)&context_init) ; if (res) { - pcontext->Ebp = stack ; - pcontext->Esp = stack ; - pcontext->Eip = entry ; - *atomvm_context = (HATOMVM_CONTEXT)new_context ; + return (HATOMVM_CONTEXT)new_context ; + } else { + free (new_context) ; } + return 0 ; +} + +/** +* \ingroup atomvm +* \b atomvmContextInit +* +* This function is to be used by the atom virtual machine. +* +* This function initialize a atomvm thread context that can be scheduled +* by atomvmContextSwitch. +* +* @param[out] context Handle to the context of the thread that are allocated +* by the caller. +* @param[in] stack Stack top. +* @param[in] entry Entry point of the thread. +* @param[in] arg argument passed on the stack as first parameter. +* @param[in] exit exit function to return to. +* @param[in] status status for exit function. +* +* @return Zero on failure, try to call GetLastError(). +*/ +uint32_t +atomvmContextInit (HATOMVM_CONTEXT context, uint32_t* stack, void (*entry)(uint32_t), uint32_t arg, void (*exit)(uint32_t)) +{ + uint32_t res = 0 ; + PATOMVM_CONTEXT new_context = (PATOMVM_CONTEXT)context ; + CONTEXT* pcontext = &new_context->context ; + + *stack-- = arg; + *stack = (uint32_t)exit ; + + pcontext->Ebp = (uint32_t)stack ; + pcontext->Esp = (uint32_t)stack ; + pcontext->Eip = (uint32_t)entry ; + return res ; } - /** * \b callbackContextSwitch * diff --git a/ports/atomvm/atomvm.h b/ports/atomvm/atomvm.h index b4ae55d..6d69058 100644 --- a/ports/atomvm/atomvm.h +++ b/ports/atomvm/atomvm.h @@ -86,7 +86,9 @@ extern void atomvmCtrlClose (HATOMVM atomvm) ; /* Function prototypes for use by the atom virtual machine from within the call to __atomvmReset(). */ extern int32_t atomvmInterruptMask (uint32_t mask) ; -extern uint32_t atomvmContextCreate (HATOMVM_CONTEXT* context, uint32_t stack, uint32_t entry) ; +extern HATOMVM_CONTEXT atomvmContextCreate (uint32_t interrupt_mask) ; +extern uint32_t atomvmContextInit (HATOMVM_CONTEXT context, uint32_t* stack, + void (*entry)(uint32_t), uint32_t arg, void (*exit)(uint32_t)) ; extern uint32_t atomvmContextSwitch (HATOMVM_CONTEXT old_context, HATOMVM_CONTEXT new_context) ; extern void atomvmContextDesrtroy (HATOMVM_CONTEXT context) ; extern void atomvmWriteThreadId (uint32_t thread_id) ;