mirror of https://gitee.com/openkylin/libvirt.git
Move event code out of the daemon/ into src/util/
The event loop implementation is used by more than just the daemon, so move it into the shared area. * daemon/event.c, src/util/event_poll.c: Renamed * daemon/event.h, src/util/event_poll.h: Renamed * tools/Makefile.am, tools/console.c, tools/virsh.c: Update to use new virEventPoll APIs * daemon/mdns.c, daemon/mdns.c, daemon/Makefile.am: Update to use new virEventPoll APIs
This commit is contained in:
parent
5d2c045cd8
commit
343eaa150b
|
@ -3,7 +3,6 @@
|
|||
CLEANFILES =
|
||||
|
||||
DAEMON_SOURCES = \
|
||||
event.c event.h \
|
||||
libvirtd.c libvirtd.h \
|
||||
remote.c remote.h \
|
||||
dispatch.c dispatch.h \
|
||||
|
|
|
@ -63,7 +63,7 @@
|
|||
#include "remote_driver.h"
|
||||
#include "conf.h"
|
||||
#include "event.h"
|
||||
#include "src/util/event.h"
|
||||
#include "event_poll.h"
|
||||
#include "memory.h"
|
||||
#include "stream.h"
|
||||
#include "hooks.h"
|
||||
|
@ -893,7 +893,7 @@ static struct qemud_server *qemudInitialize(void) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if (virEventInit() < 0) {
|
||||
if (virEventPollInit() < 0) {
|
||||
VIR_ERROR0(_("Failed to initialize event system"));
|
||||
virMutexDestroy(&server->lock);
|
||||
if (virCondDestroy(&server->job) < 0)
|
||||
|
@ -957,12 +957,12 @@ static struct qemud_server *qemudInitialize(void) {
|
|||
# endif
|
||||
#endif
|
||||
|
||||
virEventRegisterImpl(virEventAddHandleImpl,
|
||||
virEventUpdateHandleImpl,
|
||||
virEventRemoveHandleImpl,
|
||||
virEventAddTimeoutImpl,
|
||||
virEventUpdateTimeoutImpl,
|
||||
virEventRemoveTimeoutImpl);
|
||||
virEventRegisterImpl(virEventPollAddHandle,
|
||||
virEventPollUpdateHandle,
|
||||
virEventPollRemoveHandle,
|
||||
virEventPollAddTimeout,
|
||||
virEventPollUpdateTimeout,
|
||||
virEventPollRemoveTimeout);
|
||||
|
||||
return server;
|
||||
}
|
||||
|
@ -2283,7 +2283,7 @@ qemudDispatchServerEvent(int watch, int fd, int events, void *opaque) {
|
|||
static int qemudOneLoop(void) {
|
||||
sig_atomic_t errors;
|
||||
|
||||
if (virEventRunOnce() < 0)
|
||||
if (virEventPollRunOnce() < 0)
|
||||
return -1;
|
||||
|
||||
/* Check for any signal handling errors and log them. */
|
||||
|
|
|
@ -40,7 +40,7 @@
|
|||
#include "libvirtd.h"
|
||||
#include "mdns.h"
|
||||
#include "event.h"
|
||||
#include "src/util/event.h"
|
||||
#include "event_poll.h"
|
||||
#include "memory.h"
|
||||
|
||||
#define AVAHI_DEBUG(fmt, ...) VIR_DEBUG(fmt, __VA_ARGS__)
|
||||
|
@ -231,7 +231,7 @@ static void libvirtd_mdns_client_callback(AvahiClient *c, AvahiClientState state
|
|||
static void libvirtd_mdns_watch_dispatch(int watch, int fd, int events, void *opaque)
|
||||
{
|
||||
AvahiWatch *w = (AvahiWatch*)opaque;
|
||||
int fd_events = virEventHandleTypeToPollEvent(events);
|
||||
int fd_events = virEventPollToNativeEvents(events);
|
||||
AVAHI_DEBUG("Dispatch watch %d FD %d Event %d", watch, fd, fd_events);
|
||||
w->revents = fd_events;
|
||||
w->callback(w, fd, fd_events, w->userdata);
|
||||
|
@ -257,7 +257,7 @@ static AvahiWatch *libvirtd_mdns_watch_new(const AvahiPoll *api ATTRIBUTE_UNUSED
|
|||
w->userdata = userdata;
|
||||
|
||||
AVAHI_DEBUG("New handle %p FD %d Event %d", w, w->fd, event);
|
||||
hEvents = virPollEventToEventHandleType(event);
|
||||
hEvents = virEventPollFromNativeEvents(event);
|
||||
if ((w->watch = virEventAddHandle(fd, hEvents,
|
||||
libvirtd_mdns_watch_dispatch,
|
||||
w,
|
||||
|
|
|
@ -52,6 +52,7 @@ UTIL_SOURCES = \
|
|||
util/conf.c util/conf.h \
|
||||
util/cgroup.c util/cgroup.h \
|
||||
util/event.c util/event.h \
|
||||
util/event_poll.c util/event_poll.h \
|
||||
util/files.c util/files.h \
|
||||
util/hash.c util/hash.h \
|
||||
util/hooks.c util/hooks.h \
|
||||
|
|
|
@ -393,6 +393,19 @@ virEventUpdateHandle;
|
|||
virEventUpdateTimeout;
|
||||
|
||||
|
||||
# event_poll.h
|
||||
virEventPollToNativeEvents;
|
||||
virEventPollFromNativeEvents;
|
||||
virEventPollRunOnce;
|
||||
virEventPollInit;
|
||||
virEventPollRemoveTimeout;
|
||||
virEventPollUpdateTimeout;
|
||||
virEventPollAddTimeout;
|
||||
virEventPollRemoveHandle;
|
||||
virEventPollUpdateHandle;
|
||||
virEventPollAddHandle;
|
||||
|
||||
|
||||
# fdstream.h
|
||||
virFDStreamOpen;
|
||||
virFDStreamConnectUNIX;
|
||||
|
|
|
@ -32,17 +32,17 @@
|
|||
|
||||
#include "threads.h"
|
||||
#include "logging.h"
|
||||
#include "event.h"
|
||||
#include "event_poll.h"
|
||||
#include "memory.h"
|
||||
#include "util.h"
|
||||
#include "ignore-value.h"
|
||||
|
||||
#define EVENT_DEBUG(fmt, ...) VIR_DEBUG(fmt, __VA_ARGS__)
|
||||
|
||||
static int virEventInterruptLocked(void);
|
||||
static int virEventPollInterruptLocked(void);
|
||||
|
||||
/* State for a single file handle being monitored */
|
||||
struct virEventHandle {
|
||||
struct virEventPollHandle {
|
||||
int watch;
|
||||
int fd;
|
||||
int events;
|
||||
|
@ -53,7 +53,7 @@ struct virEventHandle {
|
|||
};
|
||||
|
||||
/* State for a single timer being generated */
|
||||
struct virEventTimeout {
|
||||
struct virEventPollTimeout {
|
||||
int timer;
|
||||
int frequency;
|
||||
unsigned long long expiresAt;
|
||||
|
@ -63,26 +63,26 @@ struct virEventTimeout {
|
|||
int deleted;
|
||||
};
|
||||
|
||||
/* Allocate extra slots for virEventHandle/virEventTimeout
|
||||
/* Allocate extra slots for virEventPollHandle/virEventPollTimeout
|
||||
records in this multiple */
|
||||
#define EVENT_ALLOC_EXTENT 10
|
||||
|
||||
/* State for the main event loop */
|
||||
struct virEventLoop {
|
||||
struct virEventPollLoop {
|
||||
virMutex lock;
|
||||
int running;
|
||||
virThread leader;
|
||||
int wakeupfd[2];
|
||||
size_t handlesCount;
|
||||
size_t handlesAlloc;
|
||||
struct virEventHandle *handles;
|
||||
struct virEventPollHandle *handles;
|
||||
size_t timeoutsCount;
|
||||
size_t timeoutsAlloc;
|
||||
struct virEventTimeout *timeouts;
|
||||
struct virEventPollTimeout *timeouts;
|
||||
};
|
||||
|
||||
/* Only have one event loop */
|
||||
static struct virEventLoop eventLoop;
|
||||
static struct virEventPollLoop eventLoop;
|
||||
|
||||
/* Unique ID for the next FD watch to be registered */
|
||||
static int nextWatch = 1;
|
||||
|
@ -95,7 +95,7 @@ static int nextTimer = 1;
|
|||
* NB, it *must* be safe to call this from within a callback
|
||||
* For this reason we only ever append to existing list.
|
||||
*/
|
||||
int virEventAddHandleImpl(int fd, int events,
|
||||
int virEventPollAddHandle(int fd, int events,
|
||||
virEventHandleCallback cb,
|
||||
void *opaque,
|
||||
virFreeCallback ff) {
|
||||
|
@ -117,7 +117,7 @@ int virEventAddHandleImpl(int fd, int events,
|
|||
eventLoop.handles[eventLoop.handlesCount].watch = watch;
|
||||
eventLoop.handles[eventLoop.handlesCount].fd = fd;
|
||||
eventLoop.handles[eventLoop.handlesCount].events =
|
||||
virEventHandleTypeToPollEvent(events);
|
||||
virEventPollToNativeEvents(events);
|
||||
eventLoop.handles[eventLoop.handlesCount].cb = cb;
|
||||
eventLoop.handles[eventLoop.handlesCount].ff = ff;
|
||||
eventLoop.handles[eventLoop.handlesCount].opaque = opaque;
|
||||
|
@ -125,13 +125,13 @@ int virEventAddHandleImpl(int fd, int events,
|
|||
|
||||
eventLoop.handlesCount++;
|
||||
|
||||
virEventInterruptLocked();
|
||||
virEventPollInterruptLocked();
|
||||
virMutexUnlock(&eventLoop.lock);
|
||||
|
||||
return watch;
|
||||
}
|
||||
|
||||
void virEventUpdateHandleImpl(int watch, int events) {
|
||||
void virEventPollUpdateHandle(int watch, int events) {
|
||||
int i;
|
||||
EVENT_DEBUG("Update handle w=%d e=%d", watch, events);
|
||||
|
||||
|
@ -144,8 +144,8 @@ void virEventUpdateHandleImpl(int watch, int events) {
|
|||
for (i = 0 ; i < eventLoop.handlesCount ; i++) {
|
||||
if (eventLoop.handles[i].watch == watch) {
|
||||
eventLoop.handles[i].events =
|
||||
virEventHandleTypeToPollEvent(events);
|
||||
virEventInterruptLocked();
|
||||
virEventPollToNativeEvents(events);
|
||||
virEventPollInterruptLocked();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -158,7 +158,7 @@ void virEventUpdateHandleImpl(int watch, int events) {
|
|||
* For this reason we only ever set a flag in the existing list.
|
||||
* Actual deletion will be done out-of-band
|
||||
*/
|
||||
int virEventRemoveHandleImpl(int watch) {
|
||||
int virEventPollRemoveHandle(int watch) {
|
||||
int i;
|
||||
EVENT_DEBUG("Remove handle w=%d", watch);
|
||||
|
||||
|
@ -175,7 +175,7 @@ int virEventRemoveHandleImpl(int watch) {
|
|||
if (eventLoop.handles[i].watch == watch) {
|
||||
EVENT_DEBUG("mark delete %d %d", i, eventLoop.handles[i].fd);
|
||||
eventLoop.handles[i].deleted = 1;
|
||||
virEventInterruptLocked();
|
||||
virEventPollInterruptLocked();
|
||||
virMutexUnlock(&eventLoop.lock);
|
||||
return 0;
|
||||
}
|
||||
|
@ -190,7 +190,7 @@ int virEventRemoveHandleImpl(int watch) {
|
|||
* NB, it *must* be safe to call this from within a callback
|
||||
* For this reason we only ever append to existing list.
|
||||
*/
|
||||
int virEventAddTimeoutImpl(int frequency,
|
||||
int virEventPollAddTimeout(int frequency,
|
||||
virEventTimeoutCallback cb,
|
||||
void *opaque,
|
||||
virFreeCallback ff) {
|
||||
|
@ -225,12 +225,12 @@ int virEventAddTimeoutImpl(int frequency,
|
|||
|
||||
eventLoop.timeoutsCount++;
|
||||
ret = nextTimer-1;
|
||||
virEventInterruptLocked();
|
||||
virEventPollInterruptLocked();
|
||||
virMutexUnlock(&eventLoop.lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void virEventUpdateTimeoutImpl(int timer, int frequency) {
|
||||
void virEventPollUpdateTimeout(int timer, int frequency) {
|
||||
struct timeval tv;
|
||||
int i;
|
||||
EVENT_DEBUG("Updating timer %d timeout with %d ms freq", timer, frequency);
|
||||
|
@ -252,7 +252,7 @@ void virEventUpdateTimeoutImpl(int timer, int frequency) {
|
|||
frequency >= 0 ? frequency +
|
||||
(((unsigned long long)tv.tv_sec)*1000) +
|
||||
(((unsigned long long)tv.tv_usec)/1000) : 0;
|
||||
virEventInterruptLocked();
|
||||
virEventPollInterruptLocked();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -265,7 +265,7 @@ void virEventUpdateTimeoutImpl(int timer, int frequency) {
|
|||
* For this reason we only ever set a flag in the existing list.
|
||||
* Actual deletion will be done out-of-band
|
||||
*/
|
||||
int virEventRemoveTimeoutImpl(int timer) {
|
||||
int virEventPollRemoveTimeout(int timer) {
|
||||
int i;
|
||||
EVENT_DEBUG("Remove timer %d", timer);
|
||||
|
||||
|
@ -281,7 +281,7 @@ int virEventRemoveTimeoutImpl(int timer) {
|
|||
|
||||
if (eventLoop.timeouts[i].timer == timer) {
|
||||
eventLoop.timeouts[i].deleted = 1;
|
||||
virEventInterruptLocked();
|
||||
virEventPollInterruptLocked();
|
||||
virMutexUnlock(&eventLoop.lock);
|
||||
return 0;
|
||||
}
|
||||
|
@ -296,7 +296,7 @@ int virEventRemoveTimeoutImpl(int timer) {
|
|||
* no timeout is pending
|
||||
* returns: 0 on success, -1 on error
|
||||
*/
|
||||
static int virEventCalculateTimeout(int *timeout) {
|
||||
static int virEventPollCalculateTimeout(int *timeout) {
|
||||
unsigned long long then = 0;
|
||||
int i;
|
||||
EVENT_DEBUG("Calculate expiry of %zu timers", eventLoop.timeoutsCount);
|
||||
|
@ -339,7 +339,7 @@ static int virEventCalculateTimeout(int *timeout) {
|
|||
* file handles. The caller must free the returned data struct
|
||||
* returns: the pollfd array, or NULL on error
|
||||
*/
|
||||
static struct pollfd *virEventMakePollFDs(int *nfds) {
|
||||
static struct pollfd *virEventPollMakePollFDs(int *nfds) {
|
||||
struct pollfd *fds;
|
||||
int i;
|
||||
|
||||
|
@ -384,7 +384,7 @@ static struct pollfd *virEventMakePollFDs(int *nfds) {
|
|||
*
|
||||
* Returns 0 upon success, -1 if an error occurred
|
||||
*/
|
||||
static int virEventDispatchTimeouts(void) {
|
||||
static int virEventPollDispatchTimeouts(void) {
|
||||
struct timeval tv;
|
||||
unsigned long long now;
|
||||
int i;
|
||||
|
@ -433,7 +433,7 @@ static int virEventDispatchTimeouts(void) {
|
|||
*
|
||||
* Returns 0 upon success, -1 if an error occurred
|
||||
*/
|
||||
static int virEventDispatchHandles(int nfds, struct pollfd *fds) {
|
||||
static int virEventPollDispatchHandles(int nfds, struct pollfd *fds) {
|
||||
int i, n;
|
||||
VIR_DEBUG("Dispatch %d", nfds);
|
||||
|
||||
|
@ -460,7 +460,7 @@ static int virEventDispatchHandles(int nfds, struct pollfd *fds) {
|
|||
virEventHandleCallback cb = eventLoop.handles[i].cb;
|
||||
int watch = eventLoop.handles[i].watch;
|
||||
void *opaque = eventLoop.handles[i].opaque;
|
||||
int hEvents = virPollEventToEventHandleType(fds[n].revents);
|
||||
int hEvents = virEventPollFromNativeEvents(fds[n].revents);
|
||||
EVENT_DEBUG("Dispatch n=%d f=%d w=%d e=%d %p", i,
|
||||
fds[n].fd, watch, fds[n].revents, opaque);
|
||||
virMutexUnlock(&eventLoop.lock);
|
||||
|
@ -477,7 +477,7 @@ static int virEventDispatchHandles(int nfds, struct pollfd *fds) {
|
|||
* were previously marked as deleted. This asynchronous
|
||||
* cleanup is needed to make dispatch re-entrant safe.
|
||||
*/
|
||||
static int virEventCleanupTimeouts(void) {
|
||||
static void virEventPollCleanupTimeouts(void) {
|
||||
int i;
|
||||
size_t gap;
|
||||
VIR_DEBUG("Cleanup %zu", eventLoop.timeoutsCount);
|
||||
|
@ -499,7 +499,7 @@ static int virEventCleanupTimeouts(void) {
|
|||
if ((i+1) < eventLoop.timeoutsCount) {
|
||||
memmove(eventLoop.timeouts+i,
|
||||
eventLoop.timeouts+i+1,
|
||||
sizeof(struct virEventTimeout)*(eventLoop.timeoutsCount
|
||||
sizeof(struct virEventPollTimeout)*(eventLoop.timeoutsCount
|
||||
-(i+1)));
|
||||
}
|
||||
eventLoop.timeoutsCount--;
|
||||
|
@ -513,14 +513,13 @@ static int virEventCleanupTimeouts(void) {
|
|||
eventLoop.timeoutsCount, eventLoop.timeoutsAlloc, gap);
|
||||
VIR_SHRINK_N(eventLoop.timeouts, eventLoop.timeoutsAlloc, gap);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Used post dispatch to actually remove any handles that
|
||||
* were previously marked as deleted. This asynchronous
|
||||
* cleanup is needed to make dispatch re-entrant safe.
|
||||
*/
|
||||
static int virEventCleanupHandles(void) {
|
||||
static void virEventPollCleanupHandles(void) {
|
||||
int i;
|
||||
size_t gap;
|
||||
VIR_DEBUG("Cleanup %zu", eventLoop.handlesCount);
|
||||
|
@ -540,7 +539,7 @@ static int virEventCleanupHandles(void) {
|
|||
if ((i+1) < eventLoop.handlesCount) {
|
||||
memmove(eventLoop.handles+i,
|
||||
eventLoop.handles+i+1,
|
||||
sizeof(struct virEventHandle)*(eventLoop.handlesCount
|
||||
sizeof(struct virEventPollHandle)*(eventLoop.handlesCount
|
||||
-(i+1)));
|
||||
}
|
||||
eventLoop.handlesCount--;
|
||||
|
@ -554,14 +553,13 @@ static int virEventCleanupHandles(void) {
|
|||
eventLoop.handlesCount, eventLoop.handlesAlloc, gap);
|
||||
VIR_SHRINK_N(eventLoop.handles, eventLoop.handlesAlloc, gap);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Run a single iteration of the event loop, blocking until
|
||||
* at least one file handle has an event, or a timer expires
|
||||
*/
|
||||
int virEventRunOnce(void) {
|
||||
int virEventPollRunOnce(void) {
|
||||
struct pollfd *fds = NULL;
|
||||
int ret, timeout, nfds;
|
||||
|
||||
|
@ -569,12 +567,11 @@ int virEventRunOnce(void) {
|
|||
eventLoop.running = 1;
|
||||
virThreadSelf(&eventLoop.leader);
|
||||
|
||||
if (virEventCleanupTimeouts() < 0 ||
|
||||
virEventCleanupHandles() < 0)
|
||||
goto error;
|
||||
virEventPollCleanupTimeouts();
|
||||
virEventPollCleanupHandles();
|
||||
|
||||
if (!(fds = virEventMakePollFDs(&nfds)) ||
|
||||
virEventCalculateTimeout(&timeout) < 0)
|
||||
if (!(fds = virEventPollMakePollFDs(&nfds)) ||
|
||||
virEventPollCalculateTimeout(&timeout) < 0)
|
||||
goto error;
|
||||
|
||||
virMutexUnlock(&eventLoop.lock);
|
||||
|
@ -592,16 +589,15 @@ int virEventRunOnce(void) {
|
|||
EVENT_DEBUG("Poll got %d event(s)", ret);
|
||||
|
||||
virMutexLock(&eventLoop.lock);
|
||||
if (virEventDispatchTimeouts() < 0)
|
||||
if (virEventPollDispatchTimeouts() < 0)
|
||||
goto error;
|
||||
|
||||
if (ret > 0 &&
|
||||
virEventDispatchHandles(nfds, fds) < 0)
|
||||
virEventPollDispatchHandles(nfds, fds) < 0)
|
||||
goto error;
|
||||
|
||||
if (virEventCleanupTimeouts() < 0 ||
|
||||
virEventCleanupHandles() < 0)
|
||||
goto error;
|
||||
virEventPollCleanupTimeouts();
|
||||
virEventPollCleanupHandles();
|
||||
|
||||
eventLoop.running = 0;
|
||||
virMutexUnlock(&eventLoop.lock);
|
||||
|
@ -616,10 +612,10 @@ error_unlocked:
|
|||
}
|
||||
|
||||
|
||||
static void virEventHandleWakeup(int watch ATTRIBUTE_UNUSED,
|
||||
int fd,
|
||||
int events ATTRIBUTE_UNUSED,
|
||||
void *opaque ATTRIBUTE_UNUSED)
|
||||
static void virEventPollHandleWakeup(int watch ATTRIBUTE_UNUSED,
|
||||
int fd,
|
||||
int events ATTRIBUTE_UNUSED,
|
||||
void *opaque ATTRIBUTE_UNUSED)
|
||||
{
|
||||
char c;
|
||||
virMutexLock(&eventLoop.lock);
|
||||
|
@ -627,27 +623,30 @@ static void virEventHandleWakeup(int watch ATTRIBUTE_UNUSED,
|
|||
virMutexUnlock(&eventLoop.lock);
|
||||
}
|
||||
|
||||
int virEventInit(void)
|
||||
int virEventPollInit(void)
|
||||
{
|
||||
if (virMutexInit(&eventLoop.lock) < 0)
|
||||
if (virMutexInit(&eventLoop.lock) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (pipe(eventLoop.wakeupfd) < 0 ||
|
||||
virSetNonBlock(eventLoop.wakeupfd[0]) < 0 ||
|
||||
virSetNonBlock(eventLoop.wakeupfd[1]) < 0 ||
|
||||
virSetCloseExec(eventLoop.wakeupfd[0]) < 0 ||
|
||||
virSetCloseExec(eventLoop.wakeupfd[1]) < 0)
|
||||
virSetCloseExec(eventLoop.wakeupfd[1]) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (virEventAddHandleImpl(eventLoop.wakeupfd[0],
|
||||
if (virEventPollAddHandle(eventLoop.wakeupfd[0],
|
||||
VIR_EVENT_HANDLE_READABLE,
|
||||
virEventHandleWakeup, NULL, NULL) < 0)
|
||||
virEventPollHandleWakeup, NULL, NULL) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int virEventInterruptLocked(void)
|
||||
static int virEventPollInterruptLocked(void)
|
||||
{
|
||||
char c = '\0';
|
||||
|
||||
|
@ -664,17 +663,17 @@ static int virEventInterruptLocked(void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int virEventInterrupt(void)
|
||||
int virEventPollInterrupt(void)
|
||||
{
|
||||
int ret;
|
||||
virMutexLock(&eventLoop.lock);
|
||||
ret = virEventInterruptLocked();
|
||||
ret = virEventPollInterruptLocked();
|
||||
virMutexUnlock(&eventLoop.lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
virEventHandleTypeToPollEvent(int events)
|
||||
virEventPollToNativeEvents(int events)
|
||||
{
|
||||
int ret = 0;
|
||||
if(events & VIR_EVENT_HANDLE_READABLE)
|
||||
|
@ -689,7 +688,7 @@ virEventHandleTypeToPollEvent(int events)
|
|||
}
|
||||
|
||||
int
|
||||
virPollEventToEventHandleType(int events)
|
||||
virEventPollFromNativeEvents(int events)
|
||||
{
|
||||
int ret = 0;
|
||||
if(events & POLLIN)
|
|
@ -21,13 +21,13 @@
|
|||
* Author: Daniel P. Berrange <berrange@redhat.com>
|
||||
*/
|
||||
|
||||
#ifndef __VIRTD_EVENT_H__
|
||||
# define __VIRTD_EVENT_H__
|
||||
#ifndef __VIR_EVENT_POLL_H__
|
||||
# define __VIR_EVENT_POLL_H__
|
||||
|
||||
# include "internal.h"
|
||||
|
||||
/**
|
||||
* virEventAddHandleImpl: register a callback for monitoring file handle events
|
||||
* virEventPollAddHandle: register a callback for monitoring file handle events
|
||||
*
|
||||
* @fd: file handle to monitor for events
|
||||
* @events: bitset of events to watch from POLLnnn constants
|
||||
|
@ -36,32 +36,32 @@
|
|||
*
|
||||
* returns -1 if the file handle cannot be registered, 0 upon success
|
||||
*/
|
||||
int virEventAddHandleImpl(int fd, int events,
|
||||
virEventHandleCallback cb,
|
||||
void *opaque,
|
||||
virFreeCallback ff);
|
||||
int virEventPollAddHandle(int fd, int events,
|
||||
virEventHandleCallback cb,
|
||||
void *opaque,
|
||||
virFreeCallback ff);
|
||||
|
||||
/**
|
||||
* virEventUpdateHandleImpl: change event set for a monitored file handle
|
||||
* virEventPollUpdateHandle: change event set for a monitored file handle
|
||||
*
|
||||
* @watch: watch whose handle to update
|
||||
* @events: bitset of events to watch from POLLnnn constants
|
||||
*
|
||||
* Will not fail if fd exists
|
||||
*/
|
||||
void virEventUpdateHandleImpl(int watch, int events);
|
||||
void virEventPollUpdateHandle(int watch, int events);
|
||||
|
||||
/**
|
||||
* virEventRemoveHandleImpl: unregister a callback from a file handle
|
||||
* virEventPollRemoveHandle: unregister a callback from a file handle
|
||||
*
|
||||
* @watch: watch whose handle to remove
|
||||
*
|
||||
* returns -1 if the file handle was not registered, 0 upon success
|
||||
*/
|
||||
int virEventRemoveHandleImpl(int watch);
|
||||
int virEventPollRemoveHandle(int watch);
|
||||
|
||||
/**
|
||||
* virEventAddTimeoutImpl: register a callback for a timer event
|
||||
* virEventPollAddTimeout: register a callback for a timer event
|
||||
*
|
||||
* @frequency: time between events in milliseconds
|
||||
* @cb: callback to invoke when an event occurs
|
||||
|
@ -73,13 +73,13 @@ int virEventRemoveHandleImpl(int watch);
|
|||
* returns -1 if the file handle cannot be registered, a positive
|
||||
* integer timer id upon success
|
||||
*/
|
||||
int virEventAddTimeoutImpl(int frequency,
|
||||
virEventTimeoutCallback cb,
|
||||
void *opaque,
|
||||
virFreeCallback ff);
|
||||
int virEventPollAddTimeout(int frequency,
|
||||
virEventTimeoutCallback cb,
|
||||
void *opaque,
|
||||
virFreeCallback ff);
|
||||
|
||||
/**
|
||||
* virEventUpdateTimeoutImpl: change frequency for a timer
|
||||
* virEventPollUpdateTimeout: change frequency for a timer
|
||||
*
|
||||
* @timer: timer id to change
|
||||
* @frequency: time between events in milliseconds
|
||||
|
@ -89,46 +89,44 @@ int virEventAddTimeoutImpl(int frequency,
|
|||
*
|
||||
* Will not fail if timer exists
|
||||
*/
|
||||
void virEventUpdateTimeoutImpl(int timer, int frequency);
|
||||
void virEventPollUpdateTimeout(int timer, int frequency);
|
||||
|
||||
/**
|
||||
* virEventRemoveTimeoutImpl: unregister a callback for a timer
|
||||
* virEventPollRemoveTimeout: unregister a callback for a timer
|
||||
*
|
||||
* @timer: the timer id to remove
|
||||
*
|
||||
* returns -1 if the timer was not registered, 0 upon success
|
||||
*/
|
||||
int virEventRemoveTimeoutImpl(int timer);
|
||||
int virEventPollRemoveTimeout(int timer);
|
||||
|
||||
/**
|
||||
* virEventInit: Initialize the event loop
|
||||
* virEventPollInit: Initialize the event loop
|
||||
*
|
||||
* returns -1 if initialization failed
|
||||
*/
|
||||
int virEventInit(void);
|
||||
int virEventPollInit(void);
|
||||
|
||||
/**
|
||||
* virEventRunOnce: run a single iteration of the event loop.
|
||||
* virEventPollRunOnce: run a single iteration of the event loop.
|
||||
*
|
||||
* Blocks the caller until at least one file handle has an
|
||||
* event or the first timer expires.
|
||||
*
|
||||
* returns -1 if the event monitoring failed
|
||||
*/
|
||||
int virEventRunOnce(void);
|
||||
int virEventPollRunOnce(void);
|
||||
|
||||
int
|
||||
virEventHandleTypeToPollEvent(int events);
|
||||
int
|
||||
virPollEventToEventHandleType(int events);
|
||||
int virEventPollFromNativeEvents(int events);
|
||||
int virEventPollToNativeEvents(int events);
|
||||
|
||||
|
||||
/**
|
||||
* virEventInterrupt: wakeup any thread waiting in poll()
|
||||
* virEventPollInterrupt: wakeup any thread waiting in poll()
|
||||
*
|
||||
* return -1 if wakup failed
|
||||
*/
|
||||
int virEventInterrupt(void);
|
||||
int virEventPollInterrupt(void);
|
||||
|
||||
|
||||
#endif /* __VIRTD_EVENT_H__ */
|
|
@ -375,7 +375,7 @@ virbuftest_LDADD = $(LDADDS)
|
|||
|
||||
if WITH_LIBVIRTD
|
||||
eventtest_SOURCES = \
|
||||
eventtest.c testutils.h testutils.c ../daemon/event.c
|
||||
eventtest.c testutils.h testutils.c
|
||||
eventtest_LDADD = -lrt $(LDADDS)
|
||||
endif
|
||||
|
||||
|
|
|
@ -31,7 +31,8 @@
|
|||
#include "threads.h"
|
||||
#include "logging.h"
|
||||
#include "util.h"
|
||||
#include "../daemon/event.h"
|
||||
#include "event.h"
|
||||
#include "event_poll.h"
|
||||
|
||||
#define NUM_FDS 31
|
||||
#define NUM_TIME 31
|
||||
|
@ -89,7 +90,7 @@ testPipeReader(int watch, int fd, int events, void *data)
|
|||
info->error = EV_ERROR_NONE;
|
||||
|
||||
if (info->delete != -1)
|
||||
virEventRemoveHandleImpl(info->delete);
|
||||
virEventPollRemoveHandle(info->delete);
|
||||
}
|
||||
|
||||
|
||||
|
@ -108,7 +109,7 @@ testTimer(int timer, void *data)
|
|||
info->error = EV_ERROR_NONE;
|
||||
|
||||
if (info->delete != -1)
|
||||
virEventRemoveTimeoutImpl(info->delete);
|
||||
virEventPollRemoveTimeout(info->delete);
|
||||
}
|
||||
|
||||
static pthread_mutex_t eventThreadMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
@ -127,7 +128,7 @@ static void *eventThreadLoop(void *data ATTRIBUTE_UNUSED) {
|
|||
eventThreadRunOnce = 0;
|
||||
pthread_mutex_unlock(&eventThreadMutex);
|
||||
|
||||
virEventRunOnce();
|
||||
virEventPollRunOnce();
|
||||
|
||||
pthread_mutex_lock(&eventThreadMutex);
|
||||
eventThreadJobDone = 1;
|
||||
|
@ -288,12 +289,12 @@ mymain(int argc, char **argv)
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
virEventInit();
|
||||
virEventPollInit();
|
||||
|
||||
for (i = 0 ; i < NUM_FDS ; i++) {
|
||||
handles[i].delete = -1;
|
||||
handles[i].watch =
|
||||
virEventAddHandleImpl(handles[i].pipeFD[0],
|
||||
virEventPollAddHandle(handles[i].pipeFD[0],
|
||||
VIR_EVENT_HANDLE_READABLE,
|
||||
testPipeReader,
|
||||
&handles[i], NULL);
|
||||
|
@ -303,7 +304,7 @@ mymain(int argc, char **argv)
|
|||
timers[i].delete = -1;
|
||||
timers[i].timeout = -1;
|
||||
timers[i].timer =
|
||||
virEventAddTimeoutImpl(timers[i].timeout,
|
||||
virEventPollAddTimeout(timers[i].timeout,
|
||||
testTimer,
|
||||
&timers[i], NULL);
|
||||
}
|
||||
|
@ -324,7 +325,7 @@ mymain(int argc, char **argv)
|
|||
|
||||
/* Now lets delete one before starting poll(), and
|
||||
* try triggering another handle */
|
||||
virEventRemoveHandleImpl(handles[0].watch);
|
||||
virEventPollRemoveHandle(handles[0].watch);
|
||||
startJob();
|
||||
if (safewrite(handles[1].pipeFD[1], &one, 1) != 1)
|
||||
return EXIT_FAILURE;
|
||||
|
@ -338,13 +339,13 @@ mymain(int argc, char **argv)
|
|||
|
||||
/* NB: this case is subject to a bit of a race condition.
|
||||
* We yield & sleep, and pray that the other thread gets
|
||||
* scheduled before we run EventRemoveHandleImpl */
|
||||
* scheduled before we run EventRemoveHandle */
|
||||
startJob();
|
||||
pthread_mutex_unlock(&eventThreadMutex);
|
||||
sched_yield();
|
||||
usleep(100 * 1000);
|
||||
pthread_mutex_lock(&eventThreadMutex);
|
||||
virEventRemoveHandleImpl(handles[1].watch);
|
||||
virEventPollRemoveHandle(handles[1].watch);
|
||||
if (finishJob("Interrupted during poll", -1, -1) != EXIT_SUCCESS)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
|
@ -380,22 +381,22 @@ mymain(int argc, char **argv)
|
|||
|
||||
|
||||
/* Run a timer on its own */
|
||||
virEventUpdateTimeoutImpl(timers[1].timer, 100);
|
||||
virEventPollUpdateTimeout(timers[1].timer, 100);
|
||||
startJob();
|
||||
if (finishJob("Firing a timer", -1, 1) != EXIT_SUCCESS)
|
||||
return EXIT_FAILURE;
|
||||
virEventUpdateTimeoutImpl(timers[1].timer, -1);
|
||||
virEventPollUpdateTimeout(timers[1].timer, -1);
|
||||
|
||||
resetAll();
|
||||
|
||||
/* Now lets delete one before starting poll(), and
|
||||
* try triggering another timer */
|
||||
virEventUpdateTimeoutImpl(timers[1].timer, 100);
|
||||
virEventRemoveTimeoutImpl(timers[0].timer);
|
||||
virEventPollUpdateTimeout(timers[1].timer, 100);
|
||||
virEventPollRemoveTimeout(timers[0].timer);
|
||||
startJob();
|
||||
if (finishJob("Deleted before poll", -1, 1) != EXIT_SUCCESS)
|
||||
return EXIT_FAILURE;
|
||||
virEventUpdateTimeoutImpl(timers[1].timer, -1);
|
||||
virEventPollUpdateTimeout(timers[1].timer, -1);
|
||||
|
||||
resetAll();
|
||||
|
||||
|
@ -404,13 +405,13 @@ mymain(int argc, char **argv)
|
|||
|
||||
/* NB: this case is subject to a bit of a race condition.
|
||||
* We yield & sleep, and pray that the other thread gets
|
||||
* scheduled before we run EventRemoveTimeoutImpl */
|
||||
* scheduled before we run EventRemoveTimeout */
|
||||
startJob();
|
||||
pthread_mutex_unlock(&eventThreadMutex);
|
||||
sched_yield();
|
||||
usleep(100 * 1000);
|
||||
pthread_mutex_lock(&eventThreadMutex);
|
||||
virEventRemoveTimeoutImpl(timers[1].timer);
|
||||
virEventPollRemoveTimeout(timers[1].timer);
|
||||
if (finishJob("Interrupted during poll", -1, -1) != EXIT_SUCCESS)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
|
@ -423,27 +424,27 @@ mymain(int argc, char **argv)
|
|||
* before poll() exits for the first safewrite(). We don't
|
||||
* see a hard failure in other cases, so nothing to worry
|
||||
* about */
|
||||
virEventUpdateTimeoutImpl(timers[2].timer, 100);
|
||||
virEventUpdateTimeoutImpl(timers[3].timer, 100);
|
||||
virEventPollUpdateTimeout(timers[2].timer, 100);
|
||||
virEventPollUpdateTimeout(timers[3].timer, 100);
|
||||
startJob();
|
||||
timers[2].delete = timers[3].timer;
|
||||
if (finishJob("Deleted during dispatch", -1, 2) != EXIT_SUCCESS)
|
||||
return EXIT_FAILURE;
|
||||
virEventUpdateTimeoutImpl(timers[2].timer, -1);
|
||||
virEventPollUpdateTimeout(timers[2].timer, -1);
|
||||
|
||||
resetAll();
|
||||
|
||||
/* Extreme fun, lets delete ourselves during dispatch */
|
||||
virEventUpdateTimeoutImpl(timers[2].timer, 100);
|
||||
virEventPollUpdateTimeout(timers[2].timer, 100);
|
||||
startJob();
|
||||
timers[2].delete = timers[2].timer;
|
||||
if (finishJob("Deleted during dispatch", -1, 2) != EXIT_SUCCESS)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
for (i = 0 ; i < NUM_FDS - 1 ; i++)
|
||||
virEventRemoveHandleImpl(handles[i].watch);
|
||||
virEventPollRemoveHandle(handles[i].watch);
|
||||
for (i = 0 ; i < NUM_TIME - 1 ; i++)
|
||||
virEventRemoveTimeoutImpl(timers[i].timer);
|
||||
virEventPollRemoveTimeout(timers[i].timer);
|
||||
|
||||
resetAll();
|
||||
|
||||
|
@ -464,11 +465,11 @@ mymain(int argc, char **argv)
|
|||
handles[0].pipeFD[0] = handles[1].pipeFD[0];
|
||||
handles[0].pipeFD[1] = handles[1].pipeFD[1];
|
||||
|
||||
handles[0].watch = virEventAddHandleImpl(handles[0].pipeFD[0],
|
||||
handles[0].watch = virEventPollAddHandle(handles[0].pipeFD[0],
|
||||
0,
|
||||
testPipeReader,
|
||||
&handles[0], NULL);
|
||||
handles[1].watch = virEventAddHandleImpl(handles[1].pipeFD[0],
|
||||
handles[1].watch = virEventPollAddHandle(handles[1].pipeFD[0],
|
||||
VIR_EVENT_HANDLE_READABLE,
|
||||
testPipeReader,
|
||||
&handles[1], NULL);
|
||||
|
|
|
@ -38,7 +38,6 @@ virt-pki-validate.1: virt-pki-validate.in
|
|||
|
||||
virsh_SOURCES = \
|
||||
console.c console.h \
|
||||
../daemon/event.c ../daemon/event.h \
|
||||
virsh.c
|
||||
|
||||
virsh_LDFLAGS = $(WARN_LDFLAGS) $(COVERAGE_LDFLAGS)
|
||||
|
|
|
@ -43,8 +43,8 @@
|
|||
# include "memory.h"
|
||||
# include "virterror_internal.h"
|
||||
|
||||
# include "daemon/event.h"
|
||||
# include "src/util/event.h"
|
||||
# include "event.h"
|
||||
# include "event_poll.h"
|
||||
|
||||
/* ie Ctrl-] as per telnet */
|
||||
# define CTRL_CLOSE_BRACKET '\35'
|
||||
|
@ -350,7 +350,7 @@ int vshRunConsole(virDomainPtr dom, const char *devname)
|
|||
NULL);
|
||||
|
||||
while (!con->quit) {
|
||||
if (virEventRunOnce() < 0)
|
||||
if (virEventPollRunOnce() < 0)
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -53,7 +53,7 @@
|
|||
#include "xml.h"
|
||||
#include "libvirt/libvirt-qemu.h"
|
||||
#include "files.h"
|
||||
#include "../daemon/event.h"
|
||||
#include "event_poll.h"
|
||||
#include "configmake.h"
|
||||
#include "threads.h"
|
||||
#include "command.h"
|
||||
|
@ -11677,13 +11677,13 @@ vshInit(vshControl *ctl)
|
|||
/* set up the signals handlers to catch disconnections */
|
||||
vshSetupSignals();
|
||||
|
||||
virEventRegisterImpl(virEventAddHandleImpl,
|
||||
virEventUpdateHandleImpl,
|
||||
virEventRemoveHandleImpl,
|
||||
virEventAddTimeoutImpl,
|
||||
virEventUpdateTimeoutImpl,
|
||||
virEventRemoveTimeoutImpl);
|
||||
virEventInit();
|
||||
virEventRegisterImpl(virEventPollAddHandle,
|
||||
virEventPollUpdateHandle,
|
||||
virEventPollRemoveHandle,
|
||||
virEventPollAddTimeout,
|
||||
virEventPollUpdateTimeout,
|
||||
virEventPollRemoveTimeout);
|
||||
virEventPollInit();
|
||||
|
||||
ctl->conn = virConnectOpenAuth(ctl->name,
|
||||
virConnectAuthPtrDefault,
|
||||
|
|
Loading…
Reference in New Issue