static struct per_thread_struct *per_thread_create() { struct per_thread_struct *pts = calloc(1,sizeof(*pts)); pts->local_push_q = LINK_LIST_CREATE(); pts->local_pop_q = LINK_LIST_CREATE(); pts->cond = condition_create(); return pts; }
mq_t create_mq(uint32_t push_size,item_destroyer _item_destroyer) { mq_t m = calloc(1,sizeof(*m)); m->mtx = mutex_create(); pthread_key_create(&m->t_key,0); m->share_list = LINK_LIST_CREATE(); m->local_lists = LINK_LIST_CREATE(); double_link_clear(&m->blocks); m->push_size = push_size; m->_item_destroyer = _item_destroyer; return m; }
socket_t create_socket() { socket_t s = malloc(sizeof(*s)); if(s) { s->pending_send = LINK_LIST_CREATE(); s->pending_recv = LINK_LIST_CREATE(); s->status = 0; s->engine = 0; s->isactived = 0; } return s; }
void init_mq_system() { g_mq_system = (struct mq_system *)calloc(1,sizeof(*g_mq_system)); g_mq_system->mtx = mutex_create(); g_mq_system->thread_mqs = LINK_LIST_CREATE(); pthread_key_create(&g_mq_system->t_key,0); }
/*与void mq_push(mq_t m,struct list_node *msg) * 的区别,无论如何也不会调用mq_sync_push */ struct per_thread_struct* mq_push_local(mq_t m,struct list_node *msg) { struct per_thread_struct *pts = (struct per_thread_struct*)pthread_getspecific(m->t_key); if(!pts) { pts = per_thread_create(); LINK_LIST_PUSH_BACK(m->local_lists,pts); pthread_setspecific(m->t_key,(void*)pts); } if(0 == pts->is_associate) { struct thread_associate_mqs *tmq = (struct thread_associate_mqs*)pthread_getspecific(g_mq_system->t_key); if(!tmq) { tmq = (struct thread_associate_mqs*)calloc(1,sizeof(tmq)); tmq->mqs = LINK_LIST_CREATE(); mutex_lock(g_mq_system->mtx); LINK_LIST_PUSH_BACK(g_mq_system->thread_mqs,tmq); mutex_unlock(g_mq_system->mtx); pthread_setspecific(g_mq_system->t_key,(void*)tmq); } struct thread_mq_element *ele = calloc(1,sizeof(*ele)); ele->_mq = m; LINK_LIST_PUSH_BACK(tmq->mqs,ele); pts->is_associate = 1; } LINK_LIST_PUSH_BACK(pts->local_push_q,msg); return pts; }
sche_t sche_create(int32_t max_coro,int32_t stack_size,void (*idel)(void*),void *idel_arg) { init_system_time(10); sche_t s = calloc(1,sizeof(*s)); s->stack_size = stack_size; s->max_coro = max_coro; s->active_list_1 = LINK_LIST_CREATE(); s->active_list_2 = LINK_LIST_CREATE(); s->_minheap = minheap_create(max_coro,_less); s->next_check_timeout = GetSystemMs() + 200; s->co = coro_create(s,0,NULL); s->idel = idel; s->idel_arg = idel_arg; double_link_clear(&s->coros); set_current_coro(s->co); return s; }
acceptor_t create_acceptor() { acceptor_t a = (acceptor_t)calloc(1,sizeof(*a)); a->poller_fd = TEMP_FAILURE_RETRY(epoll_create(MAX_LISTENER)); if(a->poller_fd < 0) { free(a); a = NULL; } a->st_listens = LINK_LIST_CREATE(); return a; }
struct connection *connection_create(HANDLE s,uint8_t is_raw,uint8_t mt,process_packet _process_packet,on_disconnect _on_disconnect) { struct connection *c = calloc(1,sizeof(*c)); c->socket = s; c->send_list = LINK_LIST_CREATE(); c->_process_packet = _process_packet; c->_on_disconnect = _on_disconnect; c->next_recv_buf = NULL; c->next_recv_pos = 0; c->unpack_buf = NULL; c->unpack_pos = 0; c->unpack_size = 0; c->recv_overlap.c = c; c->send_overlap.c = c; c->raw = is_raw; c->mt = mt; c->is_close = 0; return c; }
void connector_run(connector_t c,uint32_t ms) { int32_t i = 0; uint32_t tick,_timeout,_ms; int32_t size; int32_t total; struct pending_connect *pc; struct timeval timeout; tick = GetSystemMs(); _timeout = tick + ms; struct link_list *_l = LINK_LIST_CREATE(); mutex_lock(c->lock); link_list_swap(_l,c->extern_pending_connect); mutex_unlock(c->lock); while(pc = LINK_LIST_POP(struct pending_connect*,_l)) { if(c->fd_seisize >= FD_SETSIZE) { pc->call_back(-1,pc->ip,pc->port,pc->ud); free(pc); } else { FD_SET(pc->real_fd,&c->Set); LINK_LIST_PUSH_BACK(c->_pending_connect,pc); ++c->fd_seisize; } } LINK_LIST_DESTROY(&_l); do{ _ms = _timeout - tick; timeout.tv_sec = 0; timeout.tv_usec = 1000*_ms; size = list_size(c->_pending_connect); if(size == 0) return; if((total = select(1024,0,&c->Set,0, &timeout)) >0 ) { for(; i < size; ++i) { pc = LINK_LIST_POP(struct pending_connect*,c->_pending_connect); if(pc) { if(FD_ISSET(pc->real_fd, &c->Set)) { pc->call_back(pc->sock,pc->ip,pc->port,pc->ud); free(pc); --c->fd_seisize; } else LINK_LIST_PUSH_BACK(c->_pending_connect,pc); } } } FD_ZERO(&c->Set); tick = GetSystemMs(); size = list_size(c->_pending_connect); i = 0; for(; i < (int32_t)size; ++i) { pc = LINK_LIST_POP(struct pending_connect*,c->_pending_connect); if(tick >= pc->timeout) { pc->call_back(-1,pc->ip,pc->port,pc->ud); free(pc); --c->fd_seisize; } else { LINK_LIST_PUSH_BACK(c->_pending_connect,pc); FD_SET(pc->real_fd,&c->Set); } } tick = GetSystemMs(); }while(tick < _timeout); }