• libev loop_init分析


    尼玛 C语言学不好真是桑心呐!

    看了libev的代码有一种想死的感觉,但是还是要硬着头皮看下去,一定看完!

    /* initialise a loop structure, must be zero-initialised */
    static void loop_init ( unsigned int flags)
    {
      if (!loop->backend)
        {
          origflags = flags;
    
    #if EV_USE_REALTIME
          if (!have_realtime)
            {
              struct timespec ts;
    
              if (!clock_gettime (CLOCK_REALTIME, &ts))
                have_realtime = 1;
            }
    #endif
    
    #if EV_USE_MONOTONIC
          if (!have_monotonic)
            {
              struct timespec ts;
    
              if (!clock_gettime (CLOCK_MONOTONIC, &ts))
                have_monotonic = 1;
            }
    #endif
    
          /* pid check not overridable via env */
    #ifndef _WIN32
          if (flags & EVFLAG_FORKCHECK)
            curpid = getpid ();
    #endif
    
          if (!(flags & EVFLAG_NOENV)
              && !enable_secure ()
              && getenv ("LIBEV_FLAGS"))
            flags = atoi (getenv ("LIBEV_FLAGS"));
    
          ev_rt_now          = ev_time ();
          mn_now             = get_clock ();
          now_floor          = mn_now;
          rtmn_diff          = ev_rt_now - mn_now;
    #if EV_FEATURE_API
          invoke_cb          = ev_invoke_pending;
    #endif
    
          io_blocktime       = 0.;
          timeout_blocktime  = 0.;
          backend            = 0;
          backend_fd         = -1;
          sig_pending        = 0;
    #if EV_ASYNC_ENABLE
          async_pending      = 0;
    #endif
          pipe_write_skipped = 0;
          pipe_write_wanted  = 0;
          evpipe [0]         = -1;
          evpipe [1]         = -1;
    #if EV_USE_INOTIFY
          fs_fd              = flags & EVFLAG_NOINOTIFY ? -1 : -2;
    #endif
    #if EV_USE_SIGNALFD
          sigfd              = flags & EVFLAG_SIGNALFD  ? -2 : -1;
    #endif
    
          if (!(flags & EVBACKEND_MASK))
            flags |= ev_recommended_backends ();
    
    #if EV_USE_IOCP
          if (!backend && (flags & EVBACKEND_IOCP  )) backend = iocp_init   (EV_A_ flags);
    #endif
    #if EV_USE_PORT
          if (!backend && (flags & EVBACKEND_PORT  )) backend = port_init   (EV_A_ flags);
    #endif
    #if EV_USE_KQUEUE
          if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
    #endif
    #if EV_USE_EPOLL
          if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init  (EV_A_ flags);
    #endif
    #if EV_USE_POLL
          if (!backend && (flags & EVBACKEND_POLL  )) backend = poll_init   (EV_A_ flags);
    #endif
    #if EV_USE_SELECT
          if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
    #endif
    
          ev_prepare_init (&pending_w, pendingcb);
    
    #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
          ev_init (&pipe_w, pipecb);
          ev_set_priority (&pipe_w, EV_MAXPRI);
    #endif
        }
    }

    主要就是初始化loop中的数据成员,其中最重要的是backend的初始化,比如win32选择IOCP,Select等,获取当前时间等。

    void *event_init (void)
    {
    #if EV_MULTIPLICITY //定义多线程,就是可以有多个loop
      if (ev_x_cur)
        ev_x_cur = (struct event_base *)ev_loop_new (EVFLAG_AUTO);
      else
        ev_x_cur = (struct event_base *)ev_default_loop (EVFLAG_AUTO);
    #else
      assert (("libev: multiple event bases not supported when not compiled with EV_MULTIPLICITY", !ev_x_cur));
    
      ev_x_cur = (struct event_base *)(long)ev_default_loop (EVFLAG_AUTO);
    #endif
    
      return ev_x_cur;
    }

    其中ev_default_loop就是把一个全局的static loop赋给ev_default_loop_ptr,然后返回。这样就可以全局用define的东东调来调去,好不羞涩。

    ev_loop_new就new出来一个loop并对loop进行初始化。

    ok,第一步完成!

  • 相关阅读:
    Findbugs初探-使用idea获取findbugs插件
    idea 14运行java工程报错-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
    Linux定时任务
    模块和包
    流程控制&&函数
    Python 变量与数据类型
    使用代码上传文件示例
    好用的代码示例
    JedisCluster API 整理
    springboot实现转发和重定向
  • 原文地址:https://www.cnblogs.com/xiangshancuizhu/p/3250101.html
Copyright © 2020-2023  润新知