void simple_printf_lock(void) { raw_mutex_get(&mutex_printf_obj, RAW_WAIT_FOREVER); }
void test_mutex3(void * pParam) { while(1) { raw_mutex_get(&mutext1, RAW_WAIT_FOREVER); raw_mutex_get(&mutext1, RAW_WAIT_FOREVER); vc_port_printf("test_mutex3\n"); raw_sleep(RAW_TICKS_PER_SECOND*2); vc_port_printf("test_mutex333333\n"); raw_mutex_put(&mutext1); raw_mutex_put(&mutext1); } }
/* ************************************************************************************************************************ * Activate the specified timer * * Description: This function is called to activate the specified timer * * Arguments :timer_ptr is the address of this timer object * ----- * * * * Returns RAW_TIMER_STATE_INVALID: means timer is already active or timer is deleted.. * RAW_SUCCESS: means timer activate success * Note(s) :RAW_STATE_UNKNOWN wrong task state, probally sysytem error. * * ************************************************************************************************************************ */ RAW_U16 raw_timer_activate(RAW_TIMER *timer_ptr, RAW_VOID *expiration_input) { RAW_U16 position; RAW_U16 mutex_ret; #if (RAW_TIMER_FUNCTION_CHECK > 0) if (timer_ptr == 0) { return RAW_NULL_OBJECT; } if (raw_int_nesting) { return RAW_NOT_CALLED_BY_ISR; } #endif if (timer_ptr->object_type != RAW_TIMER_OBJ_TYPE) { return RAW_ERROR_OBJECT_TYPE; } /*Timer state TIMER_ACTIVE TIMER_DELETED is not allowed to delete*/ if (timer_ptr->timer_state == TIMER_ACTIVE) { return RAW_TIMER_STATE_INVALID; } if (timer_ptr->timer_state == TIMER_DELETED) { return RAW_TIMER_STATE_INVALID; } timer_ptr->raw_timeout_param = expiration_input; mutex_ret = raw_mutex_get(&timer_mutex, RAW_WAIT_FOREVER); RAW_ASSERT(mutex_ret == RAW_SUCCESS); timer_ptr->match = raw_timer_count + timer_ptr->init_count; position = (RAW_U16)(timer_ptr->match & (TIMER_HEAD_NUMBERS - 1) ); /*Sort by remain time*/ timer_ptr->remain = timer_ptr->init_count; /*Used by timer delete*/ timer_ptr->to_head = &timer_head[position]; timer_list_priority_insert(&timer_head[position], timer_ptr); timer_ptr->timer_state = TIMER_ACTIVE; raw_mutex_put(&timer_mutex); return RAW_SUCCESS; }
RAW_VOID raw_free(void *ptr) { MACB *macb = &malloc_macb; if (raw_int_nesting) { return; } raw_mutex_get(&macb->mem_lock, RAW_WAIT_FOREVER); _mem_free(ptr, macb); raw_mutex_put(&macb->mem_lock); }
RAW_VOID *raw_malloc(RAW_U32 size) { RAW_VOID *addr; MACB *macb = &malloc_macb; if (raw_int_nesting) { return 0; } raw_mutex_get(&macb->mem_lock, RAW_WAIT_FOREVER); addr = _mem_malloc(size, macb); raw_mutex_put(&macb->mem_lock); return addr; }
RAW_OS_ERROR raw_timer_deactivate(RAW_TIMER *timer_ptr) { RAW_OS_ERROR mutex_ret; #if (RAW_TIMER_FUNCTION_CHECK > 0) if (timer_ptr == 0) { return RAW_NULL_OBJECT; } if (raw_int_nesting) { return RAW_NOT_CALLED_BY_ISR; } #endif if (timer_ptr->object_type != RAW_TIMER_OBJ_TYPE) { return RAW_ERROR_OBJECT_TYPE; } /*Timer state TIMER_DEACTIVE TIMER_DELETED is not allowed to delete*/ if (timer_ptr->timer_state == TIMER_DEACTIVE) { return RAW_TIMER_STATE_INVALID; } if (timer_ptr->timer_state == TIMER_DELETED) { return RAW_TIMER_STATE_INVALID; } mutex_ret = raw_mutex_get(&timer_mutex, RAW_WAIT_FOREVER); RAW_ASSERT(mutex_ret == RAW_SUCCESS); timer_list_remove(timer_ptr); timer_ptr->timer_state = TIMER_DEACTIVE; raw_mutex_put(&timer_mutex); return RAW_SUCCESS; }
RAW_OS_ERROR raw_timer_change(RAW_TIMER *timer_ptr, RAW_TICK_TYPE initial_ticks, RAW_TICK_TYPE reschedule_ticks) { RAW_OS_ERROR mutex_ret; #if (RAW_TIMER_FUNCTION_CHECK > 0) if (timer_ptr == 0) { return RAW_NULL_OBJECT; } if (raw_int_nesting) { return RAW_NOT_CALLED_BY_ISR; } #endif if (timer_ptr->object_type != RAW_TIMER_OBJ_TYPE) { return RAW_ERROR_OBJECT_TYPE; } /*Only timer state TIMER_DEACTIVE is allowed here*/ if (timer_ptr->timer_state != TIMER_DEACTIVE) { return RAW_TIMER_STATE_INVALID; } if (timer_ptr->timer_state == TIMER_DELETED) { return RAW_TIMER_STATE_INVALID; } mutex_ret = raw_mutex_get(&timer_mutex, RAW_WAIT_FOREVER); RAW_ASSERT(mutex_ret == RAW_SUCCESS); timer_ptr->init_count = initial_ticks; timer_ptr->reschedule_ticks = reschedule_ticks; raw_mutex_put(&timer_mutex); return RAW_SUCCESS; }
void test_mutex1(void * pParam) { raw_mutex_create(&mutext1, (RAW_U8 *)"mutex1", RAW_MUTEX_INHERIT_POLICY, 0); //raw_mutex_create(&mutext1, (RAW_U8 *)"mutex1", RAW_MUTEX_CEILING_POLICY, 9); //raw_mutex_create(&mutext1, (RAW_U8 *)"mutex1", RAW_MUTEX_NONE_POLICY, 9); while(1) { raw_sleep(RAW_TICKS_PER_SECOND); vc_port_printf("..............................\n"); raw_mutex_get(&mutext1, 2); vc_port_printf("test_mutex1\n"); raw_mutex_put(&mutext1); } }
/* ************************************************************************************************************************ * Timer task * * Description: This function is called to start a timer task. * * Arguments :pa is the parameters to task. * ----- * * * * Returns * * Note(s) :This function is called by internal, users shoud not touch this function. * * ************************************************************************************************************************ */ void timer_task(void *pa) { LIST *timer_head_ptr; LIST *iter; LIST *iter_temp; RAW_TIMER *timer_ptr; RAW_OS_ERROR mutex_ret; RAW_U16 callback_ret; /*reset the timer_sem count since it may not be 0 at this point, make it start here*/ raw_semaphore_set(&timer_sem, 0); pa = pa; while (1) { /*timer task will be blocked after call this function*/ raw_semaphore_get(&timer_sem, RAW_WAIT_FOREVER); mutex_ret = raw_mutex_get(&timer_mutex, RAW_WAIT_FOREVER); RAW_ASSERT(mutex_ret == RAW_SUCCESS); /*calculate which timer_head*/ raw_timer_count++; timer_head_ptr = &timer_head; iter = timer_head_ptr->next; while (RAW_TRUE) { /*if timer exits*/ if (iter != timer_head_ptr) { /*Must use iter_temp because iter may be remove later.*/ iter_temp = iter->next; timer_ptr = raw_list_entry(iter, RAW_TIMER, timer_list); /*if timeout*/ if (raw_timer_count == timer_ptr->match) { /*remove from timer list*/ timer_list_remove(timer_ptr); /*if timer is reschedulable*/ if (timer_ptr->reschedule_ticks) { /*Sort by remain time*/ timer_ptr->remain = timer_ptr->reschedule_ticks; timer_ptr->match = raw_timer_count + timer_ptr->remain; timer_ptr->to_head = &timer_head; timer_list_priority_insert(&timer_head, timer_ptr); } else { timer_ptr->timer_state = TIMER_DEACTIVE; } /*Any way both condition need to call registered timer function*/ /*the registered timer function should not touch any timer related API,otherwise system will be crashed*/ if (timer_ptr->raw_timeout_function) { callback_ret = timer_ptr->raw_timeout_function(timer_ptr->raw_timeout_param); if ((callback_ret == TIMER_CALLBACK_STOP) && (timer_ptr->timer_state != TIMER_DEACTIVE)) { /*remove from timer list*/ timer_list_remove(timer_ptr); timer_ptr->timer_state = TIMER_DEACTIVE; } } iter = iter_temp; } else { break; } } /*exit because timer is not exit*/ else { break; } } raw_mutex_put(&timer_mutex); } }