memcached conn thread

每个thread一个连接队列,conn很庞大,你可以理解它囊括了连接包含的所有信息,小宇宙。

typedef struct { pthread_t thread_id; /* unique ID of this thread */ struct event_base *base; /* libevent handle this thread uses */ struct event notify_event; /* listen event for notify pipe */ int notify_receive_fd; /* receiving end of notify pipe */ int notify_send_fd; /* sending end of notify pipe */

    CQ  new_conn_queue;         
/* queue of new connections to handle */

} LIBEVENT_THREAD;

 

typedef struct conn_queue CQ; struct conn_queue { CQ_ITEM *head; CQ_ITEM *tail; pthread_mutex_t lock
;
    pthread_cond_t  cond;
};

 

typedef struct conn_queue_item CQ_ITEM; struct conn_queue_item { int sfd; int init_state; int event_flags; int read_buffer_size; int is_udp; CQ_ITEM *
next;
};
  • thread_init
    • 分配LIBEVENT_THREAD *threads;
    • setup_thread注册回调函数thread_libevent_process
    • create_worker->worker_libevent->event_base_loop
  • thread_libevent_process
    • 取出LIBEVENT_THREAD里边连接队列的一个元素CQ_ITEM
    • CQ_ITEM的成员来初始化一个conn
  • accept_new_conns
    • update_event:event_set(&c->event, c->sfd, new_flags, event_handler, (void *)c);
  • event_handler
    • drive_machine
      • conn_listening
        • accept_new_conns
      • conn_read
        • try_read_network
      • conn_write
      • conn_nread
      • conn_swallow
      • conn_closing
      • conn_mwrite
posted @ 2014-11-13 00:38  stevinwang  阅读(162)  评论(0编辑  收藏  举报