-/**
- * Gets a sd_event item for the current thread.
- * @return a sd_event or NULL in case of error
- */
-static struct sd_event *get_sd_event_locked()
-{
- struct evloop *el;
- uint64_t x;
- int rc;
-
- /* creates the evloop on need */
- el = &evloop[0];
- if (!el->sdev) {
- /* start the creation */
- el->state = 0;
- /* creates the eventfd for waking up polls */
- el->efd = eventfd(0, EFD_CLOEXEC);
- if (el->efd < 0) {
- ERROR("can't make eventfd for events");
- goto error1;
- }
- /* create the systemd event loop */
- rc = sd_event_new(&el->sdev);
- if (rc < 0) {
- ERROR("can't make new event loop");
- goto error2;
- }
- /* put the eventfd in the event loop */
- rc = sd_event_add_io(el->sdev, NULL, el->efd, EPOLLIN, on_evloop_efd, el);
- if (rc < 0) {
- ERROR("can't register eventfd");
- sd_event_unref(el->sdev);
- el->sdev = NULL;
-error2:
- close(el->efd);
-error1:
- return NULL;
- }
- }
-
- /* attach the event loop to the current thread */
- if (current_evloop != el) {
- if (current_evloop)
- __atomic_sub_fetch(¤t_evloop->state, EVLOOP_STATE_LOCK, __ATOMIC_RELAXED);
- current_evloop = el;
- __atomic_add_fetch(&el->state, EVLOOP_STATE_LOCK, __ATOMIC_RELAXED);