static guint add_idle_full (MilterEventLoop *loop, gint priority, GSourceFunc function, gpointer data, GDestroyNotify notify) { guint id; ev_idle *watcher; IdleWatcherPrivate *watcher_priv; MilterLibevEventLoopPrivate *priv; priv = MILTER_LIBEV_EVENT_LOOP_GET_PRIVATE(loop); watcher_priv = g_new0(IdleWatcherPrivate, 1); watcher_priv->function = function; watcher = g_new0(ev_idle, 1); watcher->data = watcher_priv; id = add_watcher(MILTER_LIBEV_EVENT_LOOP(loop), (ev_watcher *)watcher, WATCHER_STOP_FUNC(ev_idle_stop), NULL, notify, data); ev_idle_init(watcher, idle_func); ev_idle_start(priv->ev_loop, watcher); return id; }
/** * Create a new idle object. Arguments: * 1 - callback function. * * @see watcher_new() * * [+1, -0, ?] */ static int idle_new(lua_State* L) { ev_idle* idle; idle = watcher_new(L, sizeof(ev_idle), IDLE_MT); ev_idle_init(idle, &idle_cb); return 1; }
/*-----------------------------EventLoop------------------------------*/ static int EventLoop () { struct ev_loop *loop; struct ev_idle *eidle; ev_io *standard_input; ev_io *socket_input; LogDebug("%s EventLoop Started", global_argv[0]); loop = ev_default_loop(EVBACKEND_SELECT); /* Create an idle event to stop the program */ eidle = malloc(sizeof(ev_idle)); ev_idle_init(eidle, Idle); ev_idle_start(loop, eidle); /* Create the standard_input event */ standard_input = malloc(sizeof(ev_io)); ev_io_init(standard_input, ReadStdin, STDIN_FILENO, EV_READ); ev_io_start(loop, standard_input); /* Create the socket_input event */ socket_input = malloc(sizeof(ev_io)); ev_io_init(socket_input, ReadSocket, sockfd, EV_READ); ev_io_start(loop, socket_input); ev_loop(loop, 0); LogDebug("%s EventLoop finished", global_argv[0]); return 0; }
int main (int argc, char**argv) { if ( !argv[1] ) { printf("need dir parameter\n"); exit(1); } char * argvp = NULL; argvp = realpath(argv[1], pwd); dp = opendir(pwd); if ( errno ) { printf("%s, %s is not valid directory\n", strerror(errno), pwd); exit(1); } int dfd = dirfd(dp); loop = ev_loop_new (EVBACKEND_KQUEUE); ev_timer_init (&timeout_watcher, timeout_cb, 1, 0.); ev_timer_start (loop, &timeout_watcher); ev_io_init (&dir_watcher, dir_cb, dfd, EV_LIBUV_KQUEUE_HACK); ev_io_start (loop, &dir_watcher); ev_io_init (&cmd_watcher, cmd_cb, 0, EV_READ); ev_io_start (loop, &cmd_watcher); ev_idle_init (&repeat_watcher, repeat); ev_async_init (&ready_watcher, ready); ev_async_start (loop, &ready_watcher); if (eio_init (want_poll, 0)) { abort (); }; ev_run (loop, 0); if ( root ) { free(root); } printf("count: %d\n", (int)freelist_len); /* free all allocated path */ int i; if ( freelist ) { for (i = 0; i < freelist_len; ++i ) { if (freelist[i]) { free(freelist[i]); } } free(freelist); } if ( dp ) { closedir(dp); } return 0; }
int uv_idle_init(uv_loop_t* loop, uv_idle_t* idle) { uv__handle_init(loop, (uv_handle_t*)idle, UV_IDLE); loop->counters.idle_init++; ev_idle_init(&idle->idle_watcher, uv__idle); idle->idle_cb = NULL; return 0; }
eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB) { SetEVState(IO, eQDBNext); IO->NextDBOperation = CB; ev_idle_init(&IO->db_unwind_stack, DB_PerformNext); IO->db_unwind_stack.data = IO; ev_idle_start(event_db, &IO->db_unwind_stack); return eDBQuery; }
int uv_idle_init(uv_handle_t* handle, uv_close_cb close_cb, void* data) { uv__handle_init(handle, UV_IDLE, close_cb, data); ev_idle_init(&handle->idle_watcher, uv__idle); handle->idle_watcher.data = handle; handle->idle_cb = NULL; return 0; }
void * work(void *p) { signal(SIGPIPE, SIG_IGN); ev_idle idle_watcher; ev_idle_init (&idle_watcher, idle_cb); ev_idle_start(work_loop, &idle_watcher); ev_async_init(&async_watcher, async_cb); ev_async_start(work_loop, &async_watcher); ev_loop(work_loop, 0); return (void *)0; }
int uv_idle_init(uv_idle_t* idle) { uv__handle_init((uv_handle_t*)idle, UV_IDLE); uv_counters()->idle_init++; ev_idle_init(&idle->idle_watcher, uv__idle); idle->idle_watcher.data = idle; idle->idle_cb = NULL; return 0; }
int ev_flux_init (struct ev_flux *w, ev_flux_f cb, flux_t *h, int events) { w->cb = cb; w->h = h; w->events = events; if ((w->pollfd = flux_pollfd (h)) < 0) return -1; ev_prepare_init (&w->prepare_w, prepare_cb); ev_check_init (&w->check_w, check_cb); ev_idle_init (&w->idle_w, NULL); ev_io_init (&w->io_w, NULL, w->pollfd, EV_READ); return 0; }
static struct pa_defer_event * defer_new(pa_mainloop_api *a, pa_defer_event_cb_t cb, void *userdata) { struct pa_defer_event *ev = lem_xmalloc(sizeof(struct pa_defer_event)); (void)a; lem_debug("new defer %p", ev); ev_idle_init(&ev->w, defer_handler); ev->w.data = userdata; ev->cb = cb; ev->destroy = NULL; ev_idle_start(LEM_ &ev->w); return ev; }
int main(int argc, char *argv[]) { if (0 != parse_options(argc, argv)) { usage(); return 1; } if (!verbose) log_setlevel(LOGLEVEL_CRITICAL); memset(&g_srvctx, 0, sizeof(g_srvctx)); g_srvctx.loop = ev_default_loop(0); ev_set_userdata(g_srvctx.loop, &g_srvctx); if (NULL == (g_srvctx.timer_1hz = (struct ev_timer *)malloc(sizeof(struct ev_timer)))) LOG_CRITICAL("out of mem"); if (NULL == (g_srvctx.timer_10hz = (struct ev_timer *)malloc(sizeof(struct ev_timer)))) LOG_CRITICAL("out of mem"); if (NULL == (g_srvctx.idle_watcher = (struct ev_idle *)malloc(sizeof(struct ev_idle)))) LOG_CRITICAL("out of mem"); ev_timer_init(g_srvctx.timer_1hz, (void *)timeout_1hz_cb, 0, 1); ev_set_priority(g_srvctx.timer_1hz, EV_MAXPRI); ev_timer_start(g_srvctx.loop, g_srvctx.timer_1hz); ev_timer_init(g_srvctx.timer_10hz, (void *)timeout_10hz_cb, 0, 0.1); ev_set_priority(g_srvctx.timer_10hz, EV_MAXPRI); ev_timer_start(g_srvctx.loop, g_srvctx.timer_10hz); ev_idle_init(g_srvctx.idle_watcher, idle_cb); if (0 != serial_init(g_srvctx.loop, &(g_srvctx.serialctx), device_name)) { LOG_CRITICAL("Failed to open %s", device_name); } if (0 != mqtt_connect(&(g_srvctx.mqttctx), CLIENT_NAME, server, port)) { LOG_CRITICAL("failed to connect to server"); } while(1) { ev_loop(g_srvctx.loop, 0); } return 0; }
flux_watcher_t *flux_idle_watcher_create (flux_reactor_t *r, flux_watcher_f cb, void *arg) { struct watcher_ops ops = { .start = idle_start, .stop = idle_stop, .destroy = idle_destroy, }; ev_idle *iw = xzmalloc (sizeof (*iw)); flux_watcher_t *w; ev_idle_init (iw, idle_cb); w = flux_watcher_create (r, iw, ops, IDLE_SIG, cb, arg); iw->data = w; return w; }
/* Procedure exposed in Python will generate and start the event loop */ static PyObject *py_run_loop(PyObject *self, PyObject *args) { char *backend=""; int i; ev_io accept_watcher; ev_signal signal_watcher, signal_watcher2, signal_watcher3; struct TimerObj *timer; loop = ev_default_loop (0); switch (ev_backend(loop)) { case 1: backend="select"; break; case 2: backend="poll"; break; case 4: backend="epoll"; break; case 8: backend="kqueue"; break; } printf("Using %s as event backend\n", backend); ev_io_init(&accept_watcher,accept_cb,sockfd,EV_READ); ev_io_start(loop,&accept_watcher); ev_signal_init(&signal_watcher, sigint_cb, SIGINT); ev_signal_start(loop, &signal_watcher); ev_signal_init(&signal_watcher2, sigpipe_cb, SIGPIPE); ev_signal_start(loop, &signal_watcher2); ev_signal_init(&signal_watcher3, sigterm_cb, SIGTERM); ev_signal_start(loop, &signal_watcher3); idle_watcher = malloc(sizeof(ev_idle)); ev_idle_init(idle_watcher, idle_cb); if (list_timers_i>=0) { for (i=0; i<list_timers_i; i++) { timer=list_timers[i]; ev_timer_init(&timer->timerwatcher, timer_cb, timer->delay, timer->delay); ev_timer_start(loop, &timer->timerwatcher); } } ev_loop (loop, 0); return Py_None; }
manos_data_t * manos_init (struct ev_loop *loop) { manos_data_t *data = malloc (sizeof (manos_data_t)); memset (data, 0, sizeof (manos_data_t)); data->loop = loop; ev_idle_init (&eio_idle_watcher, eio_on_idle); eio_idle_watcher.data = data; ev_async_init (&eio_want_poll_watcher, eio_on_want_poll); ev_async_start (EV_DEFAULT_UC_ &eio_want_poll_watcher); eio_want_poll_watcher.data = data; ev_async_init (&eio_done_poll_watcher, eio_on_done_poll); ev_async_start (EV_DEFAULT_UC_ &eio_done_poll_watcher); eio_done_poll_watcher.data = data; eio_init (eio_want_poll, eio_done_poll); }
void poller_init(void) { ev_idle *idle = &eidle; ev_timer *timer = &etimer; ev_prepare *prepare = &eprepare; ev_check *check = &echeck; the_loop = ev_loop_new(EVFLAG_NOSIGMASK); //CTRACE("v %d.%d", ev_version_major(), ev_version_minor()); //CTRACE("ev_supported_backends=%08x", ev_supported_backends()); //CTRACE("ev_recommended_backends=%08x", ev_recommended_backends()); //CTRACE("ev_embeddable_backends=%08x", ev_embeddable_backends()); //CTRACE("ev_backend=%08x", ev_backend(the_loop)); #if EV_MULTIPLICITY //CTRACE("ev_now=%lf", ev_now(the_loop)); #else //CTRACE("ev_now=%lf", ev_now()); #endif ev_now_update(the_loop); timecounter_now = (uint64_t)(ev_now(the_loop) * 1000000000.); ev_idle_init(idle, _idle_cb); ev_timer_init(timer, _timer_cb, 0.0, 0.0); ev_timer_again(the_loop, timer); ev_prepare_init(prepare, _prepare_cb); ev_prepare_start(the_loop, prepare); ev_check_init(check, _check_cb); ev_check_start(the_loop, check); ev_set_syserr_cb(_syserr_cb); hash_init(&events, 65521, (hash_hashfn_t)ev_item_hash, (hash_item_comparator_t)ev_item_cmp, (hash_item_finalizer_t)ev_item_fini); }
int main (void) { // use the default event loop unless you have special needs struct ev_loop *loop = EV_DEFAULT; // initialise an io watcher, then start it // this one will watch for stdin to become readable ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); ev_io_start (loop, &stdin_watcher); // initialise a timer watcher, then start it // simple non-repeating 5.5 second timeout ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); ev_timer_start (loop, &timeout_watcher); ev_idle_init(&idle_watcher, idle_cb); ev_idle_start(loop, &idle_watcher); // now wait for events to arrive ev_run (loop, 0); // break was called, so exit return 0; }
static inline void runqueue_wait_init(void) { ev_idle_init(&rq.w, runqueue_pop); }
explicit RunOnceEvent(std::function<void()> func) : callback(func) { struct ev_loop *loop = EV_DEFAULT; ev_idle_init(&event, &RunOnceEvent::trampoline); ev_idle_start(EV_A_ &event); }
static int idle_init(lua_State *L) { ev_idle *w = get_idle(L, 1); ev_idle_init(w, watcher_cb); return 0; }
eNextState EvConnectSock(AsyncIO *IO, double conn_timeout, double first_rw_timeout, int ReadFirst) { struct sockaddr_in egress_sin; int fdflags; int rc = -1; SetEVState(IO, eIOConnectSock); become_session(IO->CitContext); if (ReadFirst) { IO->NextState = eReadMessage; } else { IO->NextState = eSendReply; } IO->SendBuf.fd = IO->RecvBuf.fd = socket( (IO->ConnectMe->IPv6)?PF_INET6:PF_INET, SOCK_STREAM, IPPROTO_TCP); if (IO->SendBuf.fd < 0) { EV_syslog(LOG_ERR, "EVENT: socket() failed: %s\n", strerror(errno)); StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno)); IO->SendBuf.fd = IO->RecvBuf.fd = 0; return eAbort; } fdflags = fcntl(IO->SendBuf.fd, F_GETFL); if (fdflags < 0) { EV_syslog(LOG_ERR, "EVENT: unable to get socket %d flags! %s \n", IO->SendBuf.fd, strerror(errno)); StrBufPrintf(IO->ErrMsg, "Failed to get socket %d flags: %s", IO->SendBuf.fd, strerror(errno)); close(IO->SendBuf.fd); IO->SendBuf.fd = IO->RecvBuf.fd = 0; return eAbort; } fdflags = fdflags | O_NONBLOCK; if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) { EV_syslog( LOG_ERR, "EVENT: unable to set socket %d nonblocking flags! %s \n", IO->SendBuf.fd, strerror(errno)); StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno)); close(IO->SendBuf.fd); IO->SendBuf.fd = IO->RecvBuf.fd = 0; return eAbort; } /* TODO: maye we could use offsetof() to calc the position of data... * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher */ ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ); IO->recv_event.data = IO; ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE); IO->send_event.data = IO; ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0); IO->conn_fail.data = IO; ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0); IO->rw_timeout.data = IO; /* for debugging you may bypass it like this: * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); * ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr = * inet_addr("127.0.0.1"); */ if (IO->ConnectMe->IPv6) { rc = connect(IO->SendBuf.fd, &IO->ConnectMe->Addr, sizeof(struct sockaddr_in6)); } else { /* If citserver is bound to a specific IP address on the host, make * sure we use that address for outbound connections. */ memset(&egress_sin, 0, sizeof(egress_sin)); egress_sin.sin_family = AF_INET; if (!IsEmptyStr(CtdlGetConfigStr("c_ip_addr"))) { egress_sin.sin_addr.s_addr = inet_addr(CtdlGetConfigStr("c_ip_addr")); if (egress_sin.sin_addr.s_addr == !INADDR_ANY) { egress_sin.sin_addr.s_addr = INADDR_ANY; } /* If this bind fails, no problem; we can still use INADDR_ANY */ bind(IO->SendBuf.fd, (struct sockaddr *)&egress_sin, sizeof(egress_sin)); } rc = connect(IO->SendBuf.fd, (struct sockaddr_in *)&IO->ConnectMe->Addr, sizeof(struct sockaddr_in)); } if (rc >= 0){ SetEVState(IO, eIOConnNow); EV_syslog(LOG_DEBUG, "connect() = %d immediate success.\n", IO->SendBuf.fd); set_start_callback(event_base, IO, 0); return IO->NextState; } else if (errno == EINPROGRESS) { SetEVState(IO, eIOConnWait); EV_syslog(LOG_DEBUG, "connect() = %d have to wait now.\n", IO->SendBuf.fd); ev_io_init(&IO->conn_event, IO_connestd_callback, IO->SendBuf.fd, EV_READ|EV_WRITE); IO->conn_event.data = IO; ev_io_start(event_base, &IO->conn_event); ev_timer_start(event_base, &IO->conn_fail); return IO->NextState; } else { SetEVState(IO, eIOConnfail); ev_idle_init(&IO->conn_fail_immediate, IO_connfailimmediate_callback); IO->conn_fail_immediate.data = IO; ev_idle_start(event_base, &IO->conn_fail_immediate); EV_syslog(LOG_ERR, "connect() = %d failed: %s\n", IO->SendBuf.fd, strerror(errno)); StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno)); return IO->NextState; } return IO->NextState; }
int main(int argc, char *argv[]) { #if EV_MULTIPLICITY lem_loop = ev_default_loop(LEM_LOOPFLAGS); if (lem_loop == NULL) { #else if (!ev_default_loop(LEM_LOOPFLAGS)) { #endif lem_log_error("lem: error initializing event loop"); return EXIT_FAILURE; } if (setsignal(SIGPIPE, SIG_IGN, 0) #if !EV_CHILD_ENABLE || setsignal(SIGCHLD, SIG_DFL, SA_NOCLDSTOP | SA_NOCLDWAIT) #endif ) goto error; /* create main Lua state */ L = luaL_newstate(); if (L == NULL) { lem_log_error("lem: error initializing Lua state"); goto error; } luaL_openlibs(L); /* push thread table */ lua_newtable(L); /* initialize runqueue */ ev_idle_init(&rq.w, runqueue_pop); ev_idle_start(LEM_ &rq.w); rq.queue = lem_xmalloc(LEM_INITIAL_QUEUESIZE * sizeof(struct lem_runqueue_slot)); rq.first = rq.last = 0; rq.mask = LEM_INITIAL_QUEUESIZE - 1; /* initialize threadpool */ if (pool_init()) { lem_log_error("lem: error initializing threadpool"); goto error; } /* load file */ if (queue_file(argc, argv, 1)) goto error; /* start the mainloop */ ev_loop(LEM_ 0); lem_debug("event loop exited"); /* if there is an error message left on L print it */ if (lua_type(L, -1) == LUA_TSTRING) lem_log_error("lem: %s", lua_tostring(L, -1)); /* shutdown Lua */ lua_close(L); /* free runqueue */ free(rq.queue); /* destroy loop */ #if EV_MULTIPLICITY ev_loop_destroy(lem_loop); #else ev_default_destroy(); #endif lem_debug("Bye %s", exit_status == EXIT_SUCCESS ? "o/" : ":("); return exit_status; error: if (L) lua_close(L); if (rq.queue) free(rq.queue); #if EV_MULTIPLICITY ev_loop_destroy(lem_loop); #else ev_default_destroy(); #endif return EXIT_FAILURE; }