static void *eventer_epoll_spec_alloc() { struct epoll_spec *spec; spec = calloc(1, sizeof(*spec)); spec->epoll_fd = epoll_create(1024); if(spec->epoll_fd < 0) { mtevFatal(mtev_error, "error in eveter_epoll_spec_alloc... spec->epoll_fd < 0 (%d)\n", spec->epoll_fd); } spec->event_fd = -1; #if defined(EFD_NONBLOCK) && defined(EFD_CLOEXEC) spec->event_fd = eventfd(0, EFD_NONBLOCK|EFD_CLOEXEC); #elif defined(HAVE_SYS_EVENTFD_H) spec->event_fd = eventfd(0, 0); if(spec->event_fd >= 0) { int flags; if(((flags = fcntl(spec->event_fd, F_GETFL, 0)) == -1) || (fcntl(spec->event_fd, F_SETFL, flags|O_NONBLOCK) == -1)) { close(spec->event_fd); spec->event_fd = -1; } } if(spec->event_fd >= 0) { int flags; if(((flags = fcntl(spec->event_fd, F_GETFD, 0)) == -1) || (fcntl(spec->event_fd, F_SETFD, flags|FD_CLOEXEC) == -1)) { close(spec->event_fd); spec->event_fd = -1; } } #endif return spec; }
/* * Use this to create processes that need to setup a full context * and sync it with their parents using cheap mechanisms. * * This will create two blocking eventfd(s). A pair for the parent and * the other for the child so they can be used as a notify mechanism. * Each process will gets its copy of the parent and child eventfds. * * This is useful in case: * 1) If the parent fails or dies, the child must die. * 2) Child will install PR_SET_PDEATHSIG as soon as possible. * 3) Parent and child need to sync using less resources. * 4) If parent is not able to install a SIGCHLD handler: * parent will wait using a blocking eventfd_read() or * eventfd_child_succeeded() call on the child eventfd. * * * If the child setup succeeded, child should notify with an * EVENTFD_CHILD_SUCCEEDED, parent will continue. * * If the child setup failed, child should notify with an * EVENTFD_CHILD_FAILED before any _exit(). This avoids blocking * the parent. * * 5) If parent is able to install a SIGCHLD handler: * An empty signal handler without SA_RESTART will do it, since the * blocking eventfd_read() or eventfd_parent_succeeded() of the * parent will be interrupted by SIGCHLD and the call will fail with * EINTR. This is useful in case the child dies abnormaly and did * not have a chance to notify its parent using EVENTFD_CHILD_FAILED. * * 6) Call wait*() in the main instead of the signal handler in order * to: 1) reduce side effects and 2) have a better handling for * child termination in order to reduce various race conditions. * * * The return value of clone_with_eventfd() is the same of clone(). * On success the eventfds[] will contain the two eventfd(s). These * file descriptors can be closed later with safe_close(). On failure, * a negative value is returned in the caller's context, and errno will * be set appropriately. * * Extra preliminary work: * 1) Child can wait before starting its setup by using the * eventfd_recv_start() call on the parent eventfd, in that case the * parent must notify with EVENTFD_START, after doing any preliminary * work. * * Note: this function depends on systemd internal functions * safe_close() and it should be used only by direct binaries, no * libraries. */ pid_t clone_with_eventfd(int flags, int eventfds[2]) { pid_t pid; assert(eventfds); eventfds[0] = eventfd(EVENTFD_INIT, EFD_CLOEXEC); if (eventfds[0] < 0) return -1; eventfds[1] = eventfd(EVENTFD_INIT, EFD_CLOEXEC); if (eventfds[1] < 0) goto err_eventfd0; pid = syscall(__NR_clone, flags, NULL); if (pid < 0) goto err_eventfd1; return pid; err_eventfd1: eventfds[1] = safe_close(eventfds[1]); err_eventfd0: eventfds[0] = safe_close(eventfds[0]); return -1; }
// Create a new kernel event container BSP_DECLARE(BSP_EVENT_CONTAINER *) bsp_new_event_container() { BSP_EVENT_CONTAINER *ec = bsp_calloc(1, sizeof(BSP_EVENT_CONTAINER)); if (!ec) { bsp_trace_message(BSP_TRACE_EMERGENCY, _tag_, "Create event container failed"); return NULL; } int epoll_fd = epoll_create(_BSP_MAX_EVENTS); if (0 > epoll_fd) { switch (errno) { case ENFILE : bsp_trace_message(BSP_TRACE_CRITICAL, _tag_, "Cannot open another file descriptor"); break; case ENOMEM : bsp_trace_message(BSP_TRACE_EMERGENCY, _tag_, "Kernel memory full"); break; case EINVAL : default : bsp_trace_message(BSP_TRACE_ERROR, _tag_, "Cannot create event container"); break; } bsp_free(ec); return NULL; } struct epoll_event *list = bsp_calloc(_BSP_MAX_EVENTS, sizeof(struct epoll_event)); if (!list) { bsp_free(ec); return NULL; } ec->epoll_fd = epoll_fd; ec->event_list = list; bsp_trace_message(BSP_TRACE_INFORMATIONAL, _tag_, "Create new event container %d with Epoll", epoll_fd); // Create event fd #ifdef EFD_NONBLOCK ec->notify_fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); #else ec->notify_fd = eventfd(0, 0); bsp_set_blocking(ec->notify_fd, BSP_FD_NONBLOCK); #endif BSP_EVENT ev; ev.data.fd = ec->notify_fd; ev.data.fd_type = BSP_FD_EVENT; ev.data.associate.buff = 0; ev.events = BSP_EVENT_EVENT; bsp_add_event(ec, &ev); bsp_trace_message(BSP_TRACE_DEBUG, _tag_, "Create notification event of container %d", epoll_fd); return ec; }
static int afalg_setup_async_event_notification(afalg_aio *aio) { ASYNC_JOB *job; ASYNC_WAIT_CTX *waitctx; void *custom = NULL; int ret; if ((job = ASYNC_get_current_job()) != NULL) { /* Async mode */ waitctx = ASYNC_get_wait_ctx(job); if (waitctx == NULL) { ALG_WARN("%s(%d): ASYNC_get_wait_ctx error", __FILE__, __LINE__); return 0; } /* Get waitfd from ASYNC_WAIT_CTX if it is already set */ ret = ASYNC_WAIT_CTX_get_fd(waitctx, engine_afalg_id, &aio->efd, &custom); if (ret == 0) { /* * waitfd is not set in ASYNC_WAIT_CTX, create a new one * and set it. efd will be signaled when AIO operation completes */ aio->efd = eventfd(0); if (aio->efd == -1) { ALG_PERR("%s(%d): Failed to get eventfd : ", __FILE__, __LINE__); AFALGerr(AFALG_F_AFALG_SETUP_ASYNC_EVENT_NOTIFICATION, AFALG_R_EVENTFD_FAILED); return 0; } ret = ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_afalg_id, aio->efd, custom, afalg_waitfd_cleanup); if (ret == 0) { ALG_WARN("%s(%d): Failed to set wait fd", __FILE__, __LINE__); close(aio->efd); return 0; } /* make fd non-blocking in async mode */ if (fcntl(aio->efd, F_SETFL, O_NONBLOCK) != 0) { ALG_WARN("%s(%d): Failed to set event fd as NONBLOCKING", __FILE__, __LINE__); } } aio->mode = MODE_ASYNC; } else { /* Sync mode */ aio->efd = eventfd(0); if (aio->efd == -1) { ALG_PERR("%s(%d): Failed to get eventfd : ", __FILE__, __LINE__); AFALGerr(AFALG_F_AFALG_SETUP_ASYNC_EVENT_NOTIFICATION, AFALG_R_EVENTFD_FAILED); return 0; } aio->mode = MODE_SYNC; } return 1; }
static int sync_kloop_eventfds(struct TestContext *ctx) { struct nmreq_opt_sync_kloop_eventfds *evopt = NULL; struct nmreq_opt_sync_kloop_mode modeopt; struct nmreq_option evsave; int num_entries; size_t opt_size; int ret, i; memset(&modeopt, 0, sizeof(modeopt)); modeopt.nro_opt.nro_reqtype = NETMAP_REQ_OPT_SYNC_KLOOP_MODE; modeopt.mode = ctx->sync_kloop_mode; push_option(&modeopt.nro_opt, ctx); num_entries = num_registered_rings(ctx); opt_size = sizeof(*evopt) + num_entries * sizeof(evopt->eventfds[0]); evopt = calloc(1, opt_size); evopt->nro_opt.nro_next = 0; evopt->nro_opt.nro_reqtype = NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS; evopt->nro_opt.nro_status = 0; evopt->nro_opt.nro_size = opt_size; for (i = 0; i < num_entries; i++) { int efd = eventfd(0, 0); evopt->eventfds[i].ioeventfd = efd; efd = eventfd(0, 0); evopt->eventfds[i].irqfd = efd; } push_option(&evopt->nro_opt, ctx); evsave = evopt->nro_opt; ret = sync_kloop_start_stop(ctx); if (ret != 0) { free(evopt); clear_options(ctx); return ret; } #ifdef __linux__ evsave.nro_status = 0; #else /* !__linux__ */ evsave.nro_status = EOPNOTSUPP; #endif /* !__linux__ */ ret = checkoption(&evopt->nro_opt, &evsave); free(evopt); clear_options(ctx); return ret; }
static void init_async(h2o_multithread_queue_t *queue, h2o_loop_t *loop) { #if defined(__linux__) /** * The kernel overhead of an eventfd file descriptor is * much lower than that of a pipe, and only one file descriptor is required */ int fd; fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (fd == -1) { perror("eventfd"); abort(); } queue->async.write = fd; queue->async.read = h2o_evloop_socket_create(loop, fd, 0); #else int fds[2]; if (cloexec_pipe(fds) != 0) { perror("pipe"); abort(); } fcntl(fds[1], F_SETFL, O_NONBLOCK); queue->async.write = fds[1]; queue->async.read = h2o_evloop_socket_create(loop, fds[0], 0); #endif queue->async.read->data = queue; h2o_socket_read_start(queue->async.read, on_read); }
queue_t * queue_create() { queue_t * queue; // Alloc queue if (!(queue = malloc(sizeof(queue_t)))) { goto ERR_QUEUE; } // Create an eventfd if ((queue->eventfd = eventfd(0, EFD_SEMAPHORE)) == -1) { goto ERR_EVENTFD; } // Create the list that will contain the elements if (!(queue->elements = list_create())) { goto ERR_ELEMENTS; } return queue; ERR_ELEMENTS: close(queue->eventfd); ERR_EVENTFD: free(queue); ERR_QUEUE: return NULL; }
/*L:216 * This actually creates the thread which services the virtqueue for a device. */ static void create_thread(struct virtqueue *vq) { /* * Create stack for thread. Since the stack grows upwards, we point * the stack pointer to the end of this region. */ char *stack = malloc(32768); unsigned long args[] = { LHREQ_EVENTFD, vq->config.pfn*getpagesize(), 0 }; /* Create a zero-initialized eventfd. */ vq->eventfd = eventfd(0, 0); if (vq->eventfd < 0) err(1, "Creating eventfd"); args[2] = vq->eventfd; /* * Attach an eventfd to this virtqueue: it will go off when the Guest * does an LHCALL_NOTIFY for this vq. */ if (write(lguest_fd, &args, sizeof(args)) != 0) err(1, "Attaching eventfd"); /* * CLONE_VM: because it has to access the Guest memory, and SIGCHLD so * we get a signal if it dies. */ vq->thread = clone(do_thread, stack + 32768, CLONE_VM | SIGCHLD, vq); if (vq->thread == (pid_t)-1) err(1, "Creating clone"); /* We close our local copy now the child has it. */ close(vq->eventfd); }
static int eventfd_open(struct file_desc *d) { struct eventfd_file_info *info; int tmp; info = container_of(d, struct eventfd_file_info, d); tmp = eventfd(info->efe->counter, 0); if (tmp < 0) { pr_perror("Can't create eventfd %#08x", info->efe->id); return -1; } if (rst_file_params(tmp, info->efe->fown, info->efe->flags)) { pr_perror("Can't restore params on eventfd %#08x", info->efe->id); goto err_close; } return tmp; err_close: close(tmp); return -1; }
int main(void) { int i, epoll_fd; eventfd_t tmp; struct epoll_event epoll_ev; struct sigaction sa; efd = eventfd(0, 0); if (efd < 0) abort(); memset(&sa, 0, sizeof(struct sigaction)); sa.sa_handler = sig_handler; if (sigaction(SIGUSR1, &sa, NULL) < 0) abort(); epoll_fd = epoll_create1(0); if (epoll_fd < 0) abort(); memset(&epoll_ev, 0, sizeof(struct epoll_event)); epoll_ev.events = EPOLLIN; if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, efd, &epoll_ev) < 0) abort(); write(STDOUT_FILENO, "START\n", 6); for (i=0; i < ROUNDS; ++i) { if (raise(SIGUSR1)) abort(); if (epoll_wait(epoll_fd, &epoll_ev, 1, -1) < 0) abort(); if (read(efd, &tmp, sizeof(eventfd_t)) != sizeof(eventfd_t)) abort(); } write(STDOUT_FILENO, "OK\n", 3); return 0; }
//-------------------------------------------------------------------------------------------------- event_PerThreadRec_t* fa_event_CreatePerThreadInfo ( void ) { event_LinuxPerThreadRec_t* recPtr = le_mem_ForceAlloc(PerThreadPool); // Create the epoll file descriptor for this thread. This will be used to monitor for // events on various file descriptors. recPtr->epollFd = epoll_create1(0); LE_FATAL_IF(recPtr->epollFd < 0, "epoll_create1(0) failed with errno %d.", errno); // Open an eventfd for this thread. This will be uses to signal to the epoll fd that there // are Event Reports on the Event Queue. recPtr->eventQueueFd = eventfd(0, 0); LE_FATAL_IF(recPtr->eventQueueFd < 0, "eventfd() failed with errno %d.", errno); // Add the eventfd to the list of file descriptors to wait for using epoll_wait(). struct epoll_event ev; memset(&ev, 0, sizeof(ev)); ev.events = EPOLLIN | EPOLLWAKEUP; ev.data.ptr = NULL; // This being set to NULL is what tells the main event loop that this // is the Event Queue FD, rather than another FD that is being // monitored. if (epoll_ctl(recPtr->epollFd, EPOLL_CTL_ADD, recPtr->eventQueueFd, &ev) == -1) { LE_FATAL( "epoll_ctl(ADD) failed for fd %d. errno = %d", recPtr->eventQueueFd, errno); } return &recPtr->portablePerThreadRec; }
/** * @short Returns a poller object that helps polling on sockets and files * @memberof onion_poller_t * * This poller is implemented through epoll, but other implementations are possible * */ onion_poller *onion_poller_new(int n){ onion_poller *p=onion_low_malloc(sizeof(onion_poller)); p->fd=epoll_create1(EPOLL_CLOEXEC); if (p->fd < 0){ ONION_ERROR("Error creating the poller. %s", strerror(errno)); onion_low_free(p); return NULL; } p->eventfd=eventfd(0,EFD_CLOEXEC | EFD_NONBLOCK); #if EFD_CLOEXEC == 0 fcntl(p->eventfd,F_SETFD,FD_CLOEXEC); #endif p->head=NULL; p->n=0; p->stop=0; #ifdef HAVE_PTHREADS ONION_DEBUG("Init thread stuff for poll. Eventfd at %d", p->eventfd); p->npollers=0; pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&p->mutex, &attr); pthread_mutexattr_destroy(&attr); #endif onion_poller_slot *ev=onion_poller_slot_new(p->eventfd,onion_poller_stop_helper,p); onion_poller_add(p,ev); return p; }
int main (int argc, char ** argv) { int event = eventfd(0, 0); if (argc < 2) { printf("not enough arguments\n"); exit(1); } int count = atoi(argv[1]) * 1000 * 1000; char * memsize = (char *) malloc(count); int i; long int total_time = 0; for (i = 0; i < ITERATIONS; i++) { start(); if (fork() == 0) { // child long int status = 1; write(event, &status, 8); exit(0); } else { // parent long int status; read(event, &status, 8); // child is done long int t = stop(); total_time += t; } } printf("Total time for %d iterations allocating %d bytes of memory: %d\n", ITERATIONS, count, total_time); }
/* * Called from CUSE IOCTL: VHOST_SET_VRING_KICK * The virtio device sends an eventfd that it can use to notify us. * This fd gets copied into our process space. */ static int set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file) { struct virtio_net *dev; struct eventfd_copy eventfd_call; struct vhost_virtqueue *vq; dev = get_device(ctx); if (dev == NULL) return -1; /* file->index refers to the queue index. The txq is 1, rxq is 0. */ vq = dev->virtqueue[file->index]; if (vq->callfd) close((int)vq->callfd); /* Populate the eventfd_copy structure and call eventfd_copy. */ vq->callfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); eventfd_call.source_fd = vq->callfd; eventfd_call.target_fd = file->fd; eventfd_call.target_pid = ctx.pid; if (eventfd_copy(dev, &eventfd_call)) return -1; return 0; }
PUBLIC int mprCreateNotifierService(MprWaitService *ws) { struct epoll_event ev; if ((ws->handlerMap = mprCreateList(MPR_FD_MIN, 0)) == 0) { return MPR_ERR_CANT_INITIALIZE; } if ((ws->epoll = epoll_create(ME_MAX_EVENTS)) < 0) { mprLog("critical mpr event", 0, "Call to epoll failed"); return MPR_ERR_CANT_INITIALIZE; } #if defined(EFD_NONBLOCK) if ((ws->breakFd[MPR_READ_PIPE] = eventfd(0, 0)) < 0) { mprLog("critical mpr event", 0, "Cannot open breakout event"); return MPR_ERR_CANT_INITIALIZE; } #else /* Initialize the "wakeup" pipe. This is used to wakeup the service thread if other threads need to wait for I/O. */ if (pipe(ws->breakFd) < 0) { mprLog("critical mpr event", 0, "Cannot open breakout pipe"); return MPR_ERR_CANT_INITIALIZE; } fcntl(ws->breakFd[0], F_SETFL, fcntl(ws->breakFd[0], F_GETFL) | O_NONBLOCK); fcntl(ws->breakFd[1], F_SETFL, fcntl(ws->breakFd[1], F_GETFL) | O_NONBLOCK); #endif memset(&ev, 0, sizeof(ev)); ev.events = EPOLLIN | EPOLLERR | EPOLLHUP; ev.data.fd = ws->breakFd[MPR_READ_PIPE]; epoll_ctl(ws->epoll, EPOLL_CTL_ADD, ws->breakFd[MPR_READ_PIPE], &ev); return 0; }
/* * Function to get the tap device name from the provided file descriptor and * save it in the device structure. */ static int get_ifname(struct virtio_net *dev, int tap_fd, int pid) { struct eventfd_copy fd_tap; struct ifreq ifr; uint32_t size, ifr_size; int ret; fd_tap.source_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); fd_tap.target_fd = tap_fd; fd_tap.target_pid = pid; if (eventfd_copy(dev, &fd_tap)) return -1; ret = ioctl(fd_tap.source_fd, TUNGETIFF, &ifr); if (close(fd_tap.source_fd) < 0) RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") fd close failed\n", dev->device_fh); if (ret >= 0) { ifr_size = strnlen(ifr.ifr_name, sizeof(ifr.ifr_name)); size = ifr_size > sizeof(dev->ifname) ? sizeof(dev->ifname) : ifr_size; strncpy(dev->ifname, ifr.ifr_name, size); } else RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") TUNGETIFF ioctl failed\n", dev->device_fh); return 0; }
Server::Server(std::shared_ptr<Logger> logger) : _logger(logger), _listenSock(-1), _epollFd(-1), _eventFd(-1), _maxKeepAliveDrops(0), _lameConnectionTimeoutSeconds(DefaultLameConnectionTimeoutSeconds), _nextDeadConnectionCheck(0), _threadId(0), _terminate(false), _expectedTerminate(false) { _epollFd = epoll_create(10); if (_epollFd == -1) { LS_ERROR(_logger, "Unable to create epoll: " << getLastError()); return; } _eventFd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (_eventFd == -1) { LS_ERROR(_logger, "Unable to create event FD: " << getLastError()); return; } epoll_event eventWake = { EPOLLIN, { &_eventFd } }; if (epoll_ctl(_epollFd, EPOLL_CTL_ADD, _eventFd, &eventWake) == -1) { LS_ERROR(_logger, "Unable to add wake socket to epoll: " << getLastError()); return; } }
static void eventfd_create(grpc_wakeup_fd* fd_info) { int efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); /* TODO(klempner): Handle failure more gracefully */ GPR_ASSERT(efd >= 0); fd_info->read_fd = efd; fd_info->write_fd = -1; }
int qat_setup_async_event_notification(int notificationNo) { /* We will ignore notificationNo for the moment */ ASYNC_JOB *job; ASYNC_WAIT_CTX *waitctx; OSSL_ASYNC_FD efd; void *custom = NULL; int ret = 0; if ((job = ASYNC_get_current_job()) == NULL) return ret; if ((waitctx = ASYNC_get_wait_ctx(job)) == NULL) return ret; if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_qat_id, &efd, &custom)) { ret = 1; } else { efd = eventfd(0, 0); if (efd == -1) { WARN("Failed to get eventfd = %d\n", errno); return ret; } if ((ret = ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_qat_id, efd, custom, qat_fd_cleanup)) == 0) { qat_fd_cleanup(waitctx, engine_qat_id, efd, NULL); } } return ret; }
int aio_create(void) { int n; aio_fd = eventfd(0, 0); if (-1 == aio_fd) { logerror("create eventaio_fd fail. %s", strerror(errno)); return aio_fd; } n = 1; if (-1 == ioctl(aio_fd, FIONBIO, &n)) { logerror("ioctl fail. %s", strerror(errno)); close(aio_fd); return aio_fd; } if(-1 == io_setup(64, &aio_ctx)) { logerror("io_setup fail. %s", strerror(errno)); close(aio_fd); return aio_fd; } #if AIO_QUEUE aio_queue = listCreate(); #endif return aio_fd; }
/** * Returns the readable end of a pipe that becomes readable once termination * of the object is requested (vlc_object_kill()). * This can be used to wake-up out of a select() or poll() event loop, such * typically when doing network I/O. * * Note that the pipe will remain the same for the lifetime of the object. * DO NOT read the pipe nor close it yourself. Ever. * * @param obj object that would be "killed" * @return a readable pipe descriptor, or -1 on error. */ int vlc_object_waitpipe( vlc_object_t *obj ) { vlc_object_internals_t *internals = vlc_internals( obj ); vlc_mutex_lock (&pipe_lock); if (internals->pipes[0] == -1) { /* This can only ever happen if someone killed us without locking: */ assert (internals->pipes[1] == -1); /* pipe() is not a cancellation point, but write() is and eventfd() is * unspecified (not in POSIX). */ int canc = vlc_savecancel (); #if defined (HAVE_SYS_EVENTFD_H) internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC); if (internals->pipes[0] == -1) #endif { if (pipe (internals->pipes)) internals->pipes[0] = internals->pipes[1] = -1; } if (internals->pipes[0] != -1 && obj->b_die) { /* Race condition: vlc_object_kill() already invoked! */ msg_Dbg (obj, "waitpipe: object already dying"); write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t)); } vlc_restorecancel (canc); } vlc_mutex_unlock (&pipe_lock); return internals->pipes[0]; }
static inline int le_channelEventInit(le_EventLoop* loop) { int eventFd; le_TcpBasicEvent* channelEvent = &loop->channelEvent; if( channelEvent->fd != INVALID_SOCKET ) { return LE_OK; } // int eventfd(unsigned int initval, int flags); // eventfd better than pipe; // creates an "eventfd object" that can be used as an event // wait/notify mechanism by user-space applications, and by the kernel // to notify user-space applications of events. The object contains an // unsigned 64-bit integer (uint64_t) counter that is maintained by the // kernel. This counter is initialized with the value specified in the // argument initval. eventFd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if( eventFd == INVALID_SOCKET ) { return LE_ERROR; } channelEvent->fd = eventFd; le_addEvent(loop, channelEvent, EPOLLIN); return LE_OK; }
/** * g_wakeup_new: * * Creates a new #GWakeup. * * You should use g_wakeup_free() to free it when you are done. * * Returns: a new #GWakeup * * Since: 2.30 **/ GWakeup * g_wakeup_new (void) { GError *error = NULL; GWakeup *wakeup; wakeup = g_slice_new (GWakeup); /* try eventfd first, if we think we can */ #if defined (HAVE_EVENTFD) #ifndef TEST_EVENTFD_FALLBACK wakeup->fds[0] = eventfd (0, EFD_CLOEXEC | EFD_NONBLOCK); #else wakeup->fds[0] = -1; #endif if (wakeup->fds[0] != -1) { wakeup->fds[1] = -1; return wakeup; } /* for any failure, try a pipe instead */ #endif if (!g_unix_open_pipe (wakeup->fds, FD_CLOEXEC, &error)) g_error ("Creating pipes for GWakeup: %s\n", error->message); if (!g_unix_set_fd_nonblocking (wakeup->fds[0], TRUE, &error) || !g_unix_set_fd_nonblocking (wakeup->fds[1], TRUE, &error)) g_error ("Set pipes non-blocking for GWakeup: %s\n", error->message); return wakeup; }
ANTStatus ant_init(void) { ANTStatus status = ANT_STATUS_FAILED; ANT_FUNC_START(); stRxThreadInfo.stRxThread = 0; stRxThreadInfo.ucRunThread = 0; stRxThreadInfo.ucChipResetting = 0; stRxThreadInfo.pstEnabledStatusLock = &stEnabledStatusLock; g_fnStateCallback = 0; #ifdef ANT_DEVICE_NAME // Single transport path ant_channel_init(&stRxThreadInfo.astChannels[SINGLE_CHANNEL], ANT_DEVICE_NAME); #else // Separate data/command paths ant_channel_init(&stRxThreadInfo.astChannels[COMMAND_CHANNEL], ANT_COMMANDS_DEVICE_NAME); ant_channel_init(&stRxThreadInfo.astChannels[DATA_CHANNEL], ANT_DATA_DEVICE_NAME); #endif // Separate data/command paths // Make the eventfd. Want it non blocking so that we can easily reset it by reading. stRxThreadInfo.iRxShutdownEventFd = eventfd(0, EFD_NONBLOCK); // Check for error case if(stRxThreadInfo.iRxShutdownEventFd == -1) { ANT_ERROR("ANT init failed. Could not create event fd. Reason: %s", strerror(errno)); } else { status = ANT_STATUS_SUCCESS; } ANT_FUNC_END(); return status; }
static int accord_init(const char *option, uint8_t *myaddr) { if (!option) { eprintf("specify one of the accord servers.\n"); eprintf("e.g. sheep /store -c accord:127.0.0.1\n"); return -1; } pthread_mutex_lock(&start_lock); ahandle = acrd_init(option, 9090, acrd_join_fn, acrd_leave_fn, NULL); if (!ahandle) { eprintf("failed to connect to accrd server %s\n", option); return -1; } if (get_addr(myaddr) < 0) return -1; efd = eventfd(0, EFD_NONBLOCK); if (efd < 0) { eprintf("failed to create an event fd: %m\n"); return -1; } acrd_wq = init_work_queue(1); if (!acrd_wq) eprintf("failed to create accord workqueue: %m\n"); return -1; }
static int ezApiCreate(ezEventLoop * eventLoop) { ezApiState *state = (ezApiState *) ez_malloc(sizeof(ezApiState)); if (!state) return AE_ERR; state->events = (struct epoll_event *)ez_malloc(sizeof(struct epoll_event) * eventLoop->setsize); if (!state->events) { ez_free(state); return AE_ERR; } state->epfd = epoll_create(1024); /* 1024 is just a hint for the kernel */ if (state->epfd == -1) { ez_free(state->events); ez_free(state); return AE_ERR; } state->evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (state->evfd == -1) { ez_free(state->events); ez_free(state); return AE_ERR; } eventLoop->apidata = state; return AE_OK; }
int open_event_fd(char *memcg_path) { int efd, ecfd, oomfd; char ecfd_str[32]; char *filename; ssize_t s; filename = malloc(sizeof(char) * (strlen(memcg_path) + 22)); sprintf(filename, "%s/memory.oom_control", memcg_path); oomfd = open(filename, O_RDONLY); if(oomfd == -1) handle_error("open oomfd"); efd = eventfd(0, 0); if(efd == -1) handle_error("open efd"); sprintf(filename, "%s/cgroup.event_control", memcg_path); ecfd = open(filename, O_WRONLY); if(ecfd == -1) handle_error("open ecfd"); free(filename); /* configure event_control with file descriptors */ sprintf(ecfd_str, "%d %d", efd, oomfd); s = write(ecfd, &ecfd_str, strlen(ecfd_str)); if((size_t)s != (size_t)strlen(ecfd_str)) handle_error("writing to cgroup.event_control"); return efd; }
zevbase_t *zevbase_create(void) { zevbase_t *eb; int eventfd_fd; eb = (zevbase_t *) zcalloc(1, sizeof(zevbase_t)); eb->aio_rwbuf_mpool = zmcot_create(sizeof(zaio_rwbuf_t)); zrbtree_init(&(eb->general_timer_tree), zevtimer_cmp); zrbtree_init(&(eb->aio_timer_tree), zaio_timer_cmp); eb->epoll_fd = epoll_create(1024); zclose_on_exec(eb->epoll_fd, 1); eb->epoll_event_list = (struct epoll_event *)zmalloc(sizeof(struct epoll_event) * epoll_event_count); eventfd_fd = eventfd(0, 0); zclose_on_exec(eventfd_fd, 1); znonblocking(eventfd_fd, 1); zev_init(&(eb->eventfd_event), eb, eventfd_fd); zev_set(&(eb->eventfd_event), ZEV_READ, zevbase_notify_reader); return eb; }
static int check_fdinfo_eventfd(void) { int fd, ret; int cnt = 13, proc_cnt = 0; fd = eventfd(cnt, 0); if (fd < 0) { pr_perror("Can't make eventfd"); return -1; } ret = parse_fdinfo(fd, FD_TYPES__EVENTFD, check_one_fdinfo, &proc_cnt); close(fd); if (ret) { pr_err("Error parsing proc fdinfo\n"); return -1; } if (proc_cnt != cnt) { pr_err("Counter mismatch (or not met) %d want %d\n", proc_cnt, cnt); return -1; } pr_info("Eventfd fdinfo works OK (%d vs %d)\n", cnt, proc_cnt); return 0; }
ViewBackend::ViewBackend(struct wpe_view_backend* backend) : backend(backend) { ipcHost.initialize(*this); bcm_host_init(); displayHandle = vc_dispmanx_display_open(0); graphics_get_display_size(DISPMANX_ID_HDMI, &width, &height); updateFd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); if (updateFd == -1) { fprintf(stderr, "ViewBackend: failed to create the update eventfd\n"); return; } updateSource = g_source_new(&UpdateSource::sourceFuncs, sizeof(UpdateSource)); auto& source = *reinterpret_cast<UpdateSource*>(updateSource); source.backend = this; source.pfd.fd = updateFd; source.pfd.events = G_IO_IN | G_IO_ERR | G_IO_HUP; source.pfd.revents = 0; g_source_add_poll(updateSource, &source.pfd); g_source_set_name(updateSource, "[WPE] BCMRPi update"); g_source_set_priority(updateSource, G_PRIORITY_HIGH + 30); g_source_set_can_recurse(updateSource, TRUE); g_source_attach(updateSource, g_main_context_get_thread_default()); }