libev简介

libev是一个时间循环;你可以注册感兴趣的某一事件,比入一个可读或者有时限的文件描述符,libev将会管理这些事件源并提供事件给你的程序。

为了做到这些,它将通过执行event loop handle多少地控制你的进程或线程,并通过回调机制来与事件交互。

你课题通过注册叫做 event watchers的事件,它是相对小的C结构,你来初始化它,然后开始watcher通过libev来处理它。

特点:

libev支持select,poll和epool,kqueue....

libev是可配置的。支持多个event loop.

全局函数

These functions can be called anytime, even before initialising the library in any way.

ev_tstamp ev_time ()

返回当前事件。

ev_sleep (ev_tstamp interval) int ev_version_major () int ev_version_minor ()

You can find out the major and minor ABI version numbers of the library you linked against by calling the functionsev_version_major and ev_version_minor. If you want, you can compare against the global symbols EV_VERSION_MAJOR andEV_VERSION_MINOR, which specify the version of the library your program was compiled against.

These version numbers refer to the ABI version of the library, not the release version.

Usually, it's a good idea to terminate if the major versions mismatch, as this indicates an incompatible change. Minor versions are usually compatible to older versions, so a larger minor version alone is usually not a problem.

Example: Make sure we haven't accidentally been linked against the wrong version (note, however, that this will not detect other ABI mismatches, such as LFS or reentrancy).

   assert (("libev version mismatch",
            ev_version_major () == EV_VERSION_MAJOR
            && ev_version_minor () >= EV_VERSION_MINOR));

unsigned int ev_supported_backends ()

Return the set of all backends (i.e. their corresponding EV_BACKEND_* value) compiled into this binary of libev (independent of their availability on the system you are running on). See ev_default_loop for a description of the set values.

Example: make sure we have the epoll method, because yeah this is cool and a must have and can we have a torrent of it please!!!11

   assert (("sorry, no epoll, no sex",
            ev_supported_backends () & EVBACKEND_EPOLL));

unsigned int ev_recommended_backends ()

Return the set of all backends compiled into this binary of libev and also recommended for this platform, meaning it will work for most file descriptor types. This set is often smaller than the one returned by ev_supported_backends, as for example kqueue is broken on most BSDs and will not be auto-detected unless you explicitly request it (assuming you know what you are doing). This is the set of backends that libev will probe for if you specify no backends explicitly.

unsigned int ev_embeddable_backends ()

Returns the set of backends that are embeddable in other event loops. This value is platform-specific but can include backends not available on the current system. To find which embeddable backends might be supported on the current system, you would need to look at ev_embeddable_backends () & ev_supported_backends (), likewise for recommended ones.

See the description of ev_embed watchers for more info.

ev_set_allocator (void *(*cb)(void *ptr, long size) throw ())

Sets the allocation function to use (the prototype is similar - the semantics are identical to the realloc C89/SuS/POSIX function). It is used to allocate and free memory (no surprises here). If it returns zero when memory needs to be allocated (size != 0), the library might abort or take some potentially destructive action.

Since some systems (at least OpenBSD and Darwin) fail to implement correct realloc semantics, libev will use a wrapper around the system realloc and free functions by default.

You could override this function in high-availability programs to, say, free some memory if it cannot allocate memory, to use a special allocator, or even to sleep a while and retry until some memory is available.

Example: Replace the libev allocator with one that waits a bit and then retries (example requires a standards-compliantrealloc).

   static void *
   persistent_realloc (void *ptr, size_t size)
   {
     for (;;)
       {
         void *newptr = realloc (ptr, size);

         if (newptr)
           return newptr;

         sleep (60);
       }
   }

   ...
   ev_set_allocator (persistent_realloc);

ev_set_syserr_cb (void (*cb)(const char *msg) throw ())

Set the callback function to call on a retryable system call error (such as failed select, poll, epoll_wait). The message is a printable string indicating the system call or subsystem causing the problem. If this callback is set, then libev will expect it to remedy the situation, no matter what, when it returns. That is, libev will generally retry the requested operation, or, if the condition doesn't go away, do bad stuff (such as abort).

Example: This is basically the same thing that libev does internally, too.

   static void
   fatal_error (const char *msg)
   {
     perror (msg);
     abort ();
   }

   ...
   ev_set_syserr_cb (fatal_error);

ev_feed_signal (int signum)

This function can be used to "simulate" a signal receive. It is completely safe to call this function at any time, from any context, including signal handlers or random threads.

Its main use is to customise signal handling in your process, especially in the presence of threads. For example, you could block signals by default in all threads (and specifying EVFLAG_NOSIGMASK when creating any loops), and in one thread, use sigwait or any other mechanism to wait for signals, then "deliver" them to libev by calling ev_feed_signal.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值