AMInt32 AMLooperAddTimeMessage(struct AMLooper* looper, struct AMMessage* message) { //AMLogForDebug("AMLooper", "AMLooperAddTimeMessage"); AMInt32 ret_code = AME_LOOPER_SCUESS; if(NULL == looper) return AME_LOOPER_LOOPER_NULL; if( NULL == message) return AME_LOOPER_MESSAGE_NULL; if(LOOPER_END_TRUE == looper->end_run_flag) return AME_LOOPER_LOOPER_INVALID; if(looper->start_run_falg != LOOPER_RUNNING_TRUE) return AME_LOOPER_LOOPER_NOT_STARTED; //添加到消息队列尾部, 注意要保持列表线程安全. { AMInt32 err_code; err_code = AMThreadMutexLock(&looper->queue_mutex); insert_List(&looper->time_message_queue, message, sizeof(struct AMMessage), STATIC); //print_List(&looper->time_message_queue); //AMPrintf("\n"); err_code = AMThreadMutexUnlock(&looper->queue_mutex); //添加了一个消息, 条件变量通知 AMThreadMutexLock(&looper->loop_cond_mutex); looper->isHasRecord = 1; AMThreadMutexUnlock(&looper->loop_cond_mutex); AMThreadCondSignal(&looper->loop_cond); } return AME_LOOPER_SCUESS; }
/* return 0: success, return -1: fail, e.g., duplicated id */ int insert_StudentList (StudentList* slist, int id, char* name, double score) { StudentItem *stitem; stitem = new_StudentItem(id, name, score); if (slist -> len != 0){ if (!getNode_List(slist ->list, stitem -> id)){ insert_List(slist -> list, &stitem -> id, stitem); slist -> len ++; return 0; } else return -1; } else{ insert_List(slist -> list, &stitem -> id, stitem); slist -> len ++; return 0; } slist -> avg = ((slist -> avg * slist -> len) + score) / (slist -> len + 1); }
/* change key of (*node_p) into (keyval) */ void setKey_List (List* list_p, Node* node_p, int keyval) { Node *p; if (!(*list_p -> head -> key > *node_p -> key) || (*list_p -> tail -> key < *node_p -> key)){ p = search_List(list_p, *node_p -> key); if (p -> key == node_p -> key){ remove_List(list_p, node_p); *p -> key = keyval; insert_List(list_p, p-> key, p -> item); } } }