Memcache   发布时间:2019-11-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Memcached source code analysis (threading model)--reference大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

 Ting multiple libevent,are a main thread and n workers thread is the main thread or workers thread all through the the libevent management network event,in fact,each thread is a separate libevent instance request and accept connections    (thread.C): 

     
  1.   
  2.  conn_queue_item {
  3.  sfd out of the;
  4.  init_state;
  5. E int event_flags;
  6.  read_buffer_size;
  7.  is_udp;

 to establish a connection fd 

     
  1.    conn_queue CQ;
  2.  conn_queue {

 

       {
  1.  * / 
  2.  evenT_Base  base;  *  *  
  3.     event  
  4. int notify_receive_fd;  
  5. int notify_send_fd;  
  6.  

 T_Base and a libevent  

       {
  1.  sfd out of the;
  2.  State;
  3.  event event;
  4.  which;
  5.  * RBUF;
  6. Ting the status flag and read-write buf information 

Tings / conversion connected to different states to handle the event (core function drive_machinE)  thread  process:  

     
  1. );

 

     worker threads if MT mode * / 
  1. Tings.num_threads,main_basE);

p) 

    Tings.port,0)

 encapsulates create listening socket bound address,set the non-blocking mode and register the listening socket   

     
  1. T_Base_loop (main_base,0);

request,the entire start-up process is completed  thread_init all workers thread,look at the core code thread_init 

      nthreads,  evenT_Base * E) {
  1.     
  2.  (LIBEVENT_THREAD) * nthreads);
  3.  (threads) {
  4. String" data-iceapw="4">("Can't allocate thread descriptors");
  5. (1);
  6.   
  7. [0]. base = main_base;
  8. [0]. thread_id = pthread_self ();
  9.   
  10.  (i = 0; i {
  11.  fds [2];
  12.  (pipe (fds)) {
  13. String" data-iceapw="4">("Can't create notify pipe");
  14. (1);
  15.   
  16. [0];
  17.   
  18. );
  19.   
  20.  
  21.  (i = 1; i {
  22. );

  and then the first threads as the main thread,the rest are workers thread is then created for each thread a pipe,this pipe is used as the main thread to inform the workers thread a new connection arrives  

       * mE) {
  1.  (! me-> basE) {
  2. base = event_init ();
  3.  (! me-> basE) {
  4. String" data-iceapw="6">"Can't allocate event base \ n");
  5. (1);
  6.   
  7.  
  8. notify_event,me-> notify_receive_fd,
  9. E);
  10. base,& me-> notify_event);
  11.   
  12.  (event_add (& me->  0) == -1) {
  13. String" data-iceapw="7">"Can't monitor libevent notify pipe \ n");
  14. (1);
  15.   
  16. new_conn_queuE);

 [0] base = main_base; first thread (the main thread) will not be here execution event_init ()  all workers thread pipe read end the libevent of read events,wait for the main thread last all workers CQ initialization T_Base_loop start the thread libevent trigger,and call   

        fd,  which,  * arg) {
  1.  buf [1];
  2.   
  3.  (read (fd,buf,1)! = 1)
  4.  (setTings.verbose,> 0)
  5. String" data-iceapw="7">"Can't read from libevent pipe \ n");
  6.   
  7. new_conn_queuE);
  8.   
  9.  (NULL! = item) {
  10. sfd,item-> init_state,item-> event_flags,
  11. read_buffer_size,item-> is_udp,me-> basE);
  12.     

trigger mode,if Does not handle the event,it will be loop notification know the event to be treated)  SFD is already established connection descriptor,by conn_new function of the descriptor registration the libevent read event,me-> Base on behalf of a thread structure,that is the descriptor event processing the to this workersThreading,the most important elements of conn_new method is: 

       SFD,    init_state,    event_flags
  1.    read_buffer_size,     evenT_Base * basE) {
  2.   
  3. event,sfd,event_flags,event_handler,  *) c);
  4. event);
  5. ev_flags = event_flags;
  6.  (event_add (& c-> event,0) == -1) {
  7.  (conn_add_to_FREELIst (C)) {
  8. );
  9. String" data-iceapw="1">("event_add");
  10.  NULL;
  11.   

T_Base here the workers thread) BACk event_handler function,actually event_handler in the main call memcached core method drive_machine of request,the main thread will handle the callBACk function is also the event_handler readable event (in fact,the main thread is initialized by conn_new listening socket libevent)  and the workers will executive drive_machine, 

       drive_machine (conn * C) {
  1.  stop = false;
  2.  SFD,flags = 1;
  3.  sockaddr_storage addr;
  4.  res;
  5.   
  6. null);
  7.   
  8.  (! stop) {
  9.   
  10.  (c-> statE) {
  11.  (addr);
  12.  ((sfd = accept (c-> sfd,  sockaddr *) & addr,& addrlen)) == -1) {
  13.  
  14.  ((flags = fcntl (sfd,  0)) <0 | |
  15. {
  16. String" data-iceapw="2">("setTing O_NONBLOCK");
  17. );
  18. falsE);
  19.   
  20.  (try_read_command,(C) = 0) {
  21. Tinue;
  22.  

students will immediately think of a cycle of the loop) while usually to meet a trigger mode,you must read the error of EWOULDBLOCK what,by to libevent registered callBACk after the read and write time are the core function,so we registered libevent event,while the event state is written to the conn structure libevent callBACk will the conn structure as an argument over the method parameter  

     conn_states to {
  1. ** the socket which listens for connections * / 
  2. ** reading in a command line * / 
  3. ** wriTing out a simple response * / 
  4. ** reading in a fixed number of bytes * / 
  5. ** swallowing unnecessary bytes w / o storing * / 
  6. ** closing this connection * / 
  7. ** wriTing out many items sequentially * / 

their own Workers threads never do this branch we see the main thread to accept calls falsE);  

      sfd,    
  1.  read_buffer_size, p) {
  2. );
  3.    =  + 1)% Tings.num_threads;
  4.   
  5.   
  6. sfd = sfd;
  7. init_state = init_state;
  8. event_flags = event_flags;
  9. read_buffer_size = read_buffer_size;
  10. is_udp = is_udp;
  11.   
  12.  new_conn_queue,item);
  13.   
  14.  Thread_id);
  15.  (write (threads  String" data-iceapw="1">"", 1)! = 1) {
  16. String" data-iceapw="5">("WriTing to thread notify pipe");

SELEct a thread through the round robin strategy    BACk the thread_libevent_process method (already described abovE) BACk drive_machine method,  source code,the latest version 1.2.6,the province went to a lot of optimization such as,each CQ_ITEM is malloc will malloc a lot to reduce debris generation and so the details.

http://www.cprogramdevelop.com/408519/

大佬总结

以上是大佬教程为你收集整理的Memcached source code analysis (threading model)--reference全部内容,希望文章能够帮你解决Memcached source code analysis (threading model)--reference所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:
猜你在找的Memcache相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap