void *Routine4(void *arg) { msgque_open_read(mq1); uint64_t count = 0; uint64_t total_count = 0; uint32_t tick = GetSystemMs(); for( ; ; ) { list_node *n; if(0 != msgque_get(mq1,&n,50)) break; if(n) { ++count; ++total_count; } uint32_t now = GetSystemMs(); if(now - tick > 1000) { printf("recv:%lld\n",(count*1000)/(now-tick)); tick = now; count = 0; } } printf("Routine4 end\n"); return NULL; }
void msg_loop(msgdisp_t disp,uint32_t ms) { uint64_t nowtick = GetSystemMs64(); uint64_t timeout = nowtick+(uint64_t)ms; do{ msg_t _msg = NULL; uint32_t sleeptime = (uint32_t)(timeout - nowtick); msgque_get(disp->mq,(lnode**)&_msg,sleeptime); if(_msg) dispatch_msg(disp,_msg); nowtick = GetSystemMs(); }while(nowtick < timeout); }
static void* log_routine(void *arg){ printf("log_routine\n"); while(1){ uint32_t ms = stop ? 0:100; struct log_item *item = NULL; msgque_get(pending_log,(lnode**)&item,ms); if(item){ if(item->_logfile->file == NULL || item->_logfile->total_size > MAX_FILE_SIZE) { if(item->_logfile->total_size){ fclose(item->_logfile->file); item->_logfile->total_size = 0; } //还没创建文件 char filename[128]; struct timespec tv; clock_gettime(CLOCK_REALTIME, &tv); struct tm _tm; localtime_r(&tv.tv_sec, &_tm); snprintf(filename,128,"%s-%04d-%02d-%02d %02d.%02d.%02d.%03d.log",to_cstr(item->_logfile->filename), _tm.tm_year+1900,_tm.tm_mon+1,_tm.tm_mday,_tm.tm_hour,_tm.tm_min,_tm.tm_sec,(int32_t)tv.tv_nsec/1000000); item->_logfile->file = fopen(filename,"w+"); if(!item->_logfile->file){ printf("%d\n",errno); free(item); continue; } } fprintf(item->_logfile->file,"%s\n",item->content); //fflush(item->_logfile->file); item->_logfile->total_size += strlen(item->content); free(item); }else if(stop){ break; } } //向所有打开的日志文件写入"log close success" struct logfile *l = NULL; char buf[128]; mutex_lock(g_mtx_log_file_list); while((l = LLIST_POP(struct logfile*,&g_log_file_list)) != NULL) { int32_t size = write_prefix(buf,LOG_INFO); snprintf(&buf[size],128-size,"log close success"); fprintf(l->file,"%s\n",buf); } mutex_unlock(g_mtx_log_file_list); printf("log_routine end\n"); return NULL; }
static void *mainloop(void *arg) { printf("start io thread\n"); struct poller_st *n = (struct poller_st *)arg; while(0 == n->flag) { uint64_t tick = GetSystemMs64(); uint64_t timeout = tick + 10; int8_t is_empty = 0; for(;tick < timeout;){ lnode *node = NULL; msgque_get(n->mq_in,&node,0); if(node) { msg_t msg = (msg_t)node; if(msg->type == MSG_WPACKET) { //发送数据包 process_send(n,(wpacket_t)msg); }else { process_msg(n,msg); } } else{ is_empty = 1; break; } tick = GetSystemMs64(); } if(is_empty){ //注册中断器,如果阻塞在loop里时mq_in收到消息会调用唤醒函数唤醒loop msgque_putinterrupt(n->mq_in,(void*)n->netpoller,notify_function); n->netpoller->loop(n->netpoller,10); msgque_removeinterrupt(n->mq_in); } else n->netpoller->loop(n->netpoller,0); } return NULL; }