BOOL Thread_setValue(MVALUE** stack_ptr, MVALUE* lvar, sVMInfo* info, CLObject vm_type, sCLClass* klass) { CLObject self; CLObject value; vm_mutex_lock(); self = lvar->mObjectValue.mValue; if(!check_type_with_class_name(self, "Thread", info)) { vm_mutex_unlock(); return FALSE; } value = (lvar+1)->mObjectValue.mValue; if(!check_type_with_class_name(value, "Thread", info)) { vm_mutex_unlock(); return FALSE; } CLTHREAD(self)->mThread = CLTHREAD(value)->mThread; vm_mutex_unlock(); return TRUE; }
BOOL bool_setValue(MVALUE** stack_ptr, MVALUE* lvar, sVMInfo* info, CLObject vm_type, sCLClass* klass) { CLObject self, value; CLObject new_obj; vm_mutex_lock(); self = lvar->mObjectValue.mValue; value = (lvar+1)->mObjectValue.mValue; if(!check_type(self, gBoolTypeObject, info)) { vm_mutex_unlock(); return FALSE; } if(!check_type(value, gBoolTypeObject, info)) { vm_mutex_unlock(); return FALSE; } CLBOOL(self)->mValue = CLBOOL(value)->mValue; new_obj = create_bool_object(CLBOOL(self)->mValue); (*stack_ptr)->mObjectValue.mValue = new_obj; // push result (*stack_ptr)++; vm_mutex_unlock(); return TRUE; }
BOOL Bytes_cmp(MVALUE** stack_ptr, MVALUE* lvar, sVMInfo* info, CLObject vm_type, sCLClass* klass) { CLObject self, right; char* chars; char* chars2; vm_mutex_lock(); self = lvar->mObjectValue.mValue; if(!check_type(self, gBytesTypeObject, info)) { vm_mutex_unlock(); return FALSE; } chars = CLBYTES_DATA(self)->mChars; right = (lvar+1)->mObjectValue.mValue; if(!check_type(right, gBytesTypeObject, info)) { vm_mutex_unlock(); return FALSE; } chars2 = CLBYTES_DATA(right)->mChars; (*stack_ptr)->mObjectValue.mValue = create_int_object(strcmp(chars, chars)); (*stack_ptr)++; vm_mutex_unlock(); return TRUE; }
static void new_task(handle_details* details, hx711_op op, int delay, void (*callback)(void*, int), void* callback_env) { vm_mutex_lock(&details->mutex); details->task.op = op; details->task.delay = delay; details->task.callback = callback; details->task.callback_env = callback_env; vm_mutex_unlock(&details->mutex); }
/* set thread priority, return 1 if successful */ Ipp32s vm_thread_set_priority(vm_thread *thread, vm_thread_priority priority) { Ipp32s i_res = 1; Ipp32s policy, pmin, pmax, pmean; struct sched_param param; /* check error(s) */ if (NULL == thread) return 0; if (thread->is_valid) { vm_mutex_lock(&thread->access_mut); pthread_getschedparam(thread->handle,&policy,¶m); pmin = sched_get_priority_min(policy); pmax = sched_get_priority_max(policy); pmean = (pmin + pmax) / 2; switch (priority) { case VM_THREAD_PRIORITY_HIGHEST: param.sched_priority = pmax; break; case VM_THREAD_PRIORITY_LOWEST: param.sched_priority = pmin; break; case VM_THREAD_PRIORITY_NORMAL: param.sched_priority = pmean; break; case VM_THREAD_PRIORITY_HIGH: param.sched_priority = (pmax + pmean) / 2; break; case VM_THREAD_PRIORITY_LOW: param.sched_priority = (pmin + pmean) / 2; break; default: i_res = 0; break; } if (i_res) i_res = !pthread_setschedparam(thread->handle, policy, ¶m); vm_mutex_unlock(&thread->access_mut); } return i_res; } /* Ipp32s vm_thread_set_priority(vm_thread *thread, vm_thread_priority priority) */
BOOL Bytes_replace(MVALUE** stack_ptr, MVALUE* lvar, sVMInfo* info, CLObject vm_type, sCLClass* klass) { CLObject self; int index; unsigned char character; CLObject ovalue1, ovalue2; unsigned char* chars; vm_mutex_lock(); self = lvar->mObjectValue.mValue; if(!check_type(self, gBytesTypeObject, info)) { vm_mutex_unlock(); return FALSE; } ovalue1 = (lvar+1)->mObjectValue.mValue; if(!check_type(ovalue1, gIntTypeObject, info)) { vm_mutex_unlock(); return FALSE; } index = CLINT(ovalue1)->mValue; ovalue2 = (lvar+2)->mObjectValue.mValue; if(!check_type(ovalue2, gByteTypeObject, info)) { vm_mutex_unlock(); return FALSE; } character = CLBYTE(ovalue2)->mValue; if(index < 0) index += CLBYTES(self)->mLen; if(index < 0 || index >= CLBYTES(self)->mLen) { entry_exception_object(info, gExRangeClass, "rage exception"); vm_mutex_unlock(); return FALSE; } chars = CLBYTES_DATA(self)->mChars; chars[index] = character; (*stack_ptr)->mObjectValue.mValue = create_byte_object(character); (*stack_ptr)++; vm_mutex_unlock(); return TRUE; }
/* wait until a thread exists */ void vm_thread_wait(vm_thread *thread) { /* check error(s) */ if (NULL == thread) return; if (thread->is_valid) { vm_mutex_lock(&thread->access_mut); thread->i_wait_count++; vm_mutex_unlock(&thread->access_mut); vm_event_wait(&thread->exit_event); vm_mutex_lock(&thread->access_mut); thread->i_wait_count--; if (0 == thread->i_wait_count) { pthread_join(thread->handle, NULL); thread->is_valid = 0; } vm_mutex_unlock(&thread->access_mut); } } /* void vm_thread_wait(vm_thread *thread) */
static VMINT32 measurement(VM_THREAD_HANDLE thread_handle, void* user_data) { VMINT measured_result; handle_details* details; hx711_task current_task; write_console("thread enter\n"); details = (handle_details*) user_data; get_task(details, ¤t_task); enter_standby(¤t_task); while (1) { get_task(details, ¤t_task); if (current_task.op == TERMINATE) { break; } if (current_task.op == WAIT) { enter_standby(¤t_task); continue; } if (current_task.op != A128) { wait_result(¤t_task); read_result(¤t_task); clock_setup_pulses(¤t_task); } wait_result(¤t_task); measured_result = read_result(¤t_task); current_task.callback(current_task.callback_env, measured_result); enter_standby(¤t_task); } write_console("thread exit\n"); enter_standby(¤t_task); vm_mutex_lock(&details->mutex); vm_dcl_close(current_task.sda_handle); vm_dcl_close(current_task.scl_handle); current_task.callback(current_task.callback_env, 0); vm_free(details); return 0; }
BOOL Bytes_setValue(MVALUE** stack_ptr, MVALUE* lvar, sVMInfo* info, CLObject vm_type, sCLClass* klass) { CLObject self, value; unsigned int chars_size; CLObject obj2; CLObject new_obj; unsigned char* chars; unsigned char* chars2; int i; vm_mutex_lock(); self = lvar->mObjectValue.mValue; // self if(!check_type(self, gBytesTypeObject, info)) { vm_mutex_unlock(); return FALSE; } value = (lvar+1)->mObjectValue.mValue; if(!check_type(value, gBytesTypeObject, info)) { vm_mutex_unlock(); return FALSE; } chars_size = chars_object_size(CLBYTES(value)->mLen+1); obj2 = alloc_heap_mem(chars_size, 0); CLBYTES(self)->mData = obj2; CLBYTES(self)->mLen = CLBYTES(value)->mLen; chars = CLBYTES_DATA(self)->mChars; chars2 = CLBYTES_DATA(value)->mChars; for(i=0; i<CLBYTES(value)->mLen; i++) { chars[i] = chars2[i]; } chars[i] = 0; new_obj = create_bytes_object(CLBYTES_DATA(value)->mChars, CLBYTES(value)->mLen, gBytesTypeObject, info); (*stack_ptr)->mObjectValue.mValue = new_obj; // push result (*stack_ptr)++; vm_mutex_unlock(); return TRUE; }
static int bt_server_spp_read(void* data) { VMINT ret = 0; VMINT i = 0; if(g_serverContext.conn_id < 0) { //not connected yet APP_LOG((char*)"[BTC]bt_server_spp_read : not connected yet"); return true; } char *readdata = new char[LBT_SERIAL_BUFFER_SIZE]; if(readdata == NULL) { APP_LOG((char*)"[BTC]bt_server_spp_read : allocate memory fail"); return false; } ret = vm_btspp_read(g_serverContext.conn_id, (void*)readdata, LBT_SERIAL_BUFFER_SIZE); APP_LOG("[BTC]bt_server_spp_read, ret: %d", ret); if (ret < 0) { APP_LOG((char*)"[BTC]bt_server_spp_read : read data fail"); } else { if(server_mutex.guard == 0) { vm_mutex_create(&server_mutex); } vm_mutex_lock(&server_mutex); for(i=0;i<ret;i++) { LBTServer._rx_buffer->store_char(readdata[i]); } vm_mutex_unlock(&server_mutex); APP_LOG((char*)"[BTC]btServerRead : read data success"); } delete[] readdata; return ret; }
int LBTServerClass::read(void) { if(_rx_buffer->_iHead == _rx_buffer->_iTail) { return -1; } if(server_mutex.guard == 0) { vm_mutex_create(&server_mutex); } vm_mutex_lock(&server_mutex); uint8_t uc = _rx_buffer->_aucBuffer[_rx_buffer->_iTail]; _rx_buffer->_iTail = (unsigned int)(_rx_buffer->_iTail + 1) % LBT_SERIAL_BUFFER_SIZE; vm_mutex_unlock(&server_mutex); return uc; }
int32_t vm_thread_set_scheduling(vm_thread* thread, void* params) { int32_t i_res = 1; struct sched_param param; vm_thread_linux_schedparams* linux_params = (vm_thread_linux_schedparams*)params; /* check error(s) */ if (!thread || !linux_params) return 0; if (thread->is_valid) { vm_mutex_lock(&thread->access_mut); param.sched_priority = linux_params->priority; i_res = !pthread_setschedparam(thread->handle, linux_params->schedtype, ¶m); vm_mutex_unlock(&thread->access_mut); } return i_res; }
/* create a thread. return 1 if success */ Ipp32s vm_thread_create(vm_thread *thread, Ipp32u (*vm_thread_func)(void *), void *arg) { Ipp32s i_res = 1; pthread_attr_t attr; /* check error(s) */ if ((NULL == thread) || (NULL == vm_thread_func)) return 0; if (0 != i_res) { if (VM_OK != vm_event_init(&thread->exit_event, 1, 0)) i_res = 0; } if ((0 != i_res) && (VM_OK != vm_mutex_init(&thread->access_mut))) i_res = 0; if (0 != i_res) { vm_mutex_lock(&thread->access_mut); thread->p_thread_func = vm_thread_func; thread->p_arg = arg; pthread_attr_init(&attr); pthread_attr_setschedpolicy(&attr, geteuid() ? SCHED_OTHER : SCHED_RR); thread->is_valid =! pthread_create(&thread->handle, &attr, vm_thread_proc, (void*)thread); i_res = (thread->is_valid) ? 1 : 0; vm_mutex_unlock(&thread->access_mut); pthread_attr_destroy(&attr); } vm_thread_set_priority(thread, VM_THREAD_PRIORITY_LOWEST); return i_res; } /* Ipp32s vm_thread_create(vm_thread *thread, */
BOOL Thread_join(MVALUE** stack_ptr, MVALUE* lvar, sVMInfo* info, CLObject vm_type, sCLClass* klass) { CLObject self; pthread_t thread; vm_mutex_lock(); self = lvar->mObjectValue.mValue; if(!check_type_with_class_name(self, "Thread", info)) { vm_mutex_unlock(); return FALSE; } thread = CLTHREAD(self)->mThread; vm_mutex_unlock(); pthread_join(thread, NULL); return TRUE; }
/* verify if the thread handler is valid */ Ipp32s vm_thread_is_valid(vm_thread *thread) { /* check error(s) */ if (NULL == thread) return 0; if (thread->is_valid) { vm_mutex_lock(&thread->access_mut); if (VM_OK == vm_event_timed_wait(&thread->exit_event, 0)) { vm_mutex_unlock(&thread->access_mut); vm_thread_wait(thread); } else vm_mutex_unlock(&thread->access_mut); } return thread->is_valid; } /* Ipp32s vm_thread_is_valid(vm_thread *thread) */
void* thread_func(void* param) { MVALUE result; struct sThreadFuncArg* arg; vm_mutex_lock(); arg = param; if(!cl_excute_block_with_new_stack(&result, arg->mBlock, arg->mResultExistance, arg->mNewVMInfo, 0)) // 1 --> block { FREE(arg->mNewVMInfo); FREE(arg); exit(1); } FREE(arg->mNewVMInfo); FREE(arg); return NULL; }
/* create a thread. return 1 if success */ int32_t vm_thread_create(vm_thread *thread, uint32_t (*vm_thread_func)(void *), void *arg) { int32_t i_res = 1; // pthread_attr_t attr; /* check error(s) */ if ((NULL == thread) || (NULL == vm_thread_func)) return 0; if (0 != i_res) { if (VM_OK != vm_event_init(&thread->exit_event, 1, 0)) i_res = 0; } if ((0 != i_res) && (VM_OK != vm_mutex_init(&thread->access_mut))) i_res = 0; if (0 != i_res) { vm_mutex_lock(&thread->access_mut); thread->p_thread_func = vm_thread_func; thread->p_arg = arg; thread->is_valid =! pthread_create(&thread->handle, NULL, vm_thread_proc, (void*)thread); i_res = (thread->is_valid) ? 1 : 0; vm_mutex_unlock(&thread->access_mut); // pthread_attr_destroy(&attr); } return i_res; } /* int32_t vm_thread_create(vm_thread *thread, */
BOOL Bytes_toString(MVALUE** stack_ptr, MVALUE* lvar, sVMInfo* info, CLObject vm_type, sCLClass* klass) { CLObject self; char* buf; int len; wchar_t* wstr; CLObject new_obj; vm_mutex_lock(); self = lvar->mObjectValue.mValue; // self if(!check_type(self, gBytesTypeObject, info)) { vm_mutex_unlock(); return FALSE; } buf = CLBYTES_DATA(self)->mChars; len = CLBYTES(self)->mLen; wstr = CALLOC(1, sizeof(wchar_t)*(len+1)); if((int)mbstowcs(wstr, buf, len+1) < 0) { entry_exception_object(info, gExConvertingStringCodeClass, "failed to mbstowcs"); FREE(wstr); vm_mutex_unlock(); return FALSE; } new_obj = create_string_object(wstr, len, gStringTypeObject, info); (*stack_ptr)->mObjectValue.mValue = new_obj; // push result (*stack_ptr)++; FREE(wstr); vm_mutex_unlock(); return TRUE; }
BOOL Bytes_getValue(MVALUE** stack_ptr, MVALUE* lvar, sVMInfo* info, CLObject vm_type, sCLClass* klass) { CLObject self; CLObject new_obj; vm_mutex_lock(); self = lvar->mObjectValue.mValue; // self if(!check_type(self, gBytesTypeObject, info)) { vm_mutex_unlock(); return FALSE; } new_obj = create_bytes_object(CLBYTES_DATA(self)->mChars, CLBYTES(self)->mLen, gBytesTypeObject, info); (*stack_ptr)->mObjectValue.mValue = new_obj; // push result (*stack_ptr)++; vm_mutex_unlock(); return TRUE; }
BOOL Thread_Thread(MVALUE** stack_ptr, MVALUE* lvar, sVMInfo* info, CLObject vm_type, sCLClass* klass) { CLObject self; CLObject block; BOOL result_existance; struct sThreadFuncArg* arg; sVMInfo* new_info; MVALUE* lvar2; pthread_t thread_id; int real_param_num; vm_mutex_lock(); result_existance = FALSE; self = lvar->mObjectValue.mValue; block = (lvar+1)->mObjectValue.mValue; /// make new stack to the sub thread on the main thread because of GC /// /// create new stack /// new_info = CALLOC(1, sizeof(sVMInfo)); new_info->stack = CALLOC(1, sizeof(MVALUE)*CL_STACK_SIZE); new_info->stack_size = CL_STACK_SIZE; new_info->stack_ptr = new_info->stack; //real_param_num = 2; // this param is Thread and Block lvar2 = new_info->stack; new_info->thread_obj = (info->stack_ptr-2)->mObjectValue.mValue; new_info->thread_block_obj = (info->stack_ptr-1)->mObjectValue.mValue; /// copy params to current local vars /// //memmove(lvar2 + CLBLOCK(block)->mNumParentVar, info->stack_ptr-real_param_num, sizeof(MVALUE)*real_param_num); /// copy caller local vars to current local vars /// memmove(lvar2, CLBLOCK(block)->mParentLocalVar, sizeof(MVALUE)*CLBLOCK(block)->mNumParentVar); new_info->stack_ptr += CLBLOCK(block)->mNumParentVar; //new_info->stack_ptr += CLBLOCK(block)->mNumParentVar + real_param_num; /* if(CLBLOCK(block)->mNumLocals - real_param_num > 0) { new_info->stack_ptr += (CLBLOCK(block)->mNumLocals - real_param_num); // forwarded stack pointer for local variable } */ new_info->stack_ptr += CLBLOCK(block)->mNumLocals; push_vminfo(new_info); /// make arg for thread /// arg = CALLOC(1, sizeof(struct sThreadFuncArg)); arg->mBlock = block; arg->mResultExistance = result_existance; arg->mNewVMInfo = MANAGED new_info; vm_mutex_unlock(); if(pthread_create(&thread_id, NULL, thread_func, MANAGED arg) != 0) { pthread_detach(thread_id); entry_exception_object(info, gExceptionClass, "error pthread_create", info); return FALSE; } vm_mutex_lock(); (*stack_ptr)->mObjectValue.mValue = self; (*stack_ptr)++; CLTHREAD(self)->mThread = thread_id; vm_mutex_unlock(); return TRUE; }
static void get_task(handle_details* details, hx711_task* task) { vm_mutex_lock(&details->mutex); *task = details->task; vm_mutex_unlock(&details->mutex); }
BOOL RegularFile_RegularFile(MVALUE** stack_ptr, MVALUE* lvar, sVMInfo* info, CLObject vm_type, sCLClass* klass) { CLObject self; CLObject file_name; CLObject mode; CLObject ovalue1; int permission; char file_name_mbs[PATH_MAX]; char mode_mbs[128]; int oflag; char* p; int fd; vm_mutex_lock(); self = lvar->mObjectValue.mValue; file_name = (lvar+1)->mObjectValue.mValue; // String mode = (lvar+2)->mObjectValue.mValue; // String ovalue1 = (lvar+3)->mObjectValue.mValue; permission = CLINT(ovalue1)->mValue; // int if((int)wcstombs(file_name_mbs, CLSTRING_DATA(file_name)->mChars, PATH_MAX) < 0) { entry_exception_object(info, gExConvertingStringCodeClass, "error wcstombs on file_name"); vm_mutex_unlock(); return FALSE; } if((int)wcstombs(mode_mbs, CLSTRING_DATA(mode)->mChars, 128) < 0) { entry_exception_object(info, gExConvertingStringCodeClass, "error wcstombs on mode"); vm_mutex_unlock(); return FALSE; } oflag = 0; p = mode_mbs; if(*p == 'r') { oflag = O_RDONLY; } else if(*p == 'w') { oflag = O_WRONLY|O_CREAT|O_TRUNC; } else if(*p == 'a') { oflag = O_WRONLY|O_CREAT|O_APPEND; } else { entry_exception_object(info, gExceptionClass, "ivalid file mode"); vm_mutex_unlock(); return FALSE; } p++; if(*p == '+') { p++; oflag |= O_RDWR; } if(*p != 0) { entry_exception_object(info, gExceptionClass, "ivalid file mode"); vm_mutex_unlock(); return FALSE; } fd = open(file_name_mbs, oflag, permission); if(fd < 0) { entry_exception_object(info, gExIOClass, "can't open file"); vm_mutex_unlock(); return FALSE; } CLFILE(self)->mFD = fd; (*stack_ptr)->mObjectValue.mValue = self; (*stack_ptr)++; vm_mutex_unlock(); return TRUE; }