尼玛 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 = ;
}
#endif #if EV_USE_MONOTONIC
if (!have_monotonic)
{
struct timespec ts; if (!clock_gettime (CLOCK_MONOTONIC, &ts))
have_monotonic = ;
}
#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 = .;
timeout_blocktime = .;
backend = ;
backend_fd = -;
sig_pending = ;
#if EV_ASYNC_ENABLE
async_pending = ;
#endif
pipe_write_skipped = ;
pipe_write_wanted = ;
evpipe [] = -;
evpipe [] = -;
#if EV_USE_INOTIFY
fs_fd = flags & EVFLAG_NOINOTIFY ? - : -;
#endif
#if EV_USE_SIGNALFD
sigfd = flags & EVFLAG_SIGNALFD ? - : -;
#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,第一步完成!

05-11 09:39