/* State for the main event loop */
struct virEventLoop {
- pthread_mutex_t lock;
+ virMutex lock;
int running;
- pthread_t leader;
+ virThread leader;
int wakeupfd[2];
int handlesCount;
int handlesAlloc;
/* Unique ID for the next timer to be registered */
static int nextTimer = 1;
-static void virEventLock(void)
-{
- pthread_mutex_lock(&eventLoop.lock);
-}
-
-static void virEventUnlock(void)
-{
- pthread_mutex_unlock(&eventLoop.lock);
-}
-
/*
* Register a callback for monitoring file handle events.
* NB, it *must* be safe to call this from within a callback
virFreeCallback ff) {
int watch;
EVENT_DEBUG("Add handle fd=%d events=%d cb=%p opaque=%p", fd, events, cb, opaque);
- virEventLock();
+ virMutexLock(&eventLoop.lock);
if (eventLoop.handlesCount == eventLoop.handlesAlloc) {
EVENT_DEBUG("Used %d handle slots, adding %d more",
eventLoop.handlesAlloc, EVENT_ALLOC_EXTENT);
if (VIR_REALLOC_N(eventLoop.handles,
(eventLoop.handlesAlloc + EVENT_ALLOC_EXTENT)) < 0) {
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
return -1;
}
eventLoop.handlesAlloc += EVENT_ALLOC_EXTENT;
eventLoop.handlesCount++;
virEventInterruptLocked();
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
return watch;
}
return;
}
- virEventLock();
+ virMutexLock(&eventLoop.lock);
for (i = 0 ; i < eventLoop.handlesCount ; i++) {
if (eventLoop.handles[i].watch == watch) {
eventLoop.handles[i].events =
break;
}
}
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
}
/*
return -1;
}
- virEventLock();
+ virMutexLock(&eventLoop.lock);
for (i = 0 ; i < eventLoop.handlesCount ; i++) {
if (eventLoop.handles[i].deleted)
continue;
EVENT_DEBUG("mark delete %d %d", i, eventLoop.handles[i].fd);
eventLoop.handles[i].deleted = 1;
virEventInterruptLocked();
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
return 0;
}
}
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
return -1;
}
return -1;
}
- virEventLock();
+ virMutexLock(&eventLoop.lock);
if (eventLoop.timeoutsCount == eventLoop.timeoutsAlloc) {
EVENT_DEBUG("Used %d timeout slots, adding %d more",
eventLoop.timeoutsAlloc, EVENT_ALLOC_EXTENT);
if (VIR_REALLOC_N(eventLoop.timeouts,
(eventLoop.timeoutsAlloc + EVENT_ALLOC_EXTENT)) < 0) {
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
return -1;
}
eventLoop.timeoutsAlloc += EVENT_ALLOC_EXTENT;
eventLoop.timeoutsCount++;
ret = nextTimer-1;
virEventInterruptLocked();
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
return ret;
}
return;
}
- virEventLock();
+ virMutexLock(&eventLoop.lock);
for (i = 0 ; i < eventLoop.timeoutsCount ; i++) {
if (eventLoop.timeouts[i].timer == timer) {
eventLoop.timeouts[i].frequency = frequency;
break;
}
}
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
}
/*
return -1;
}
- virEventLock();
+ virMutexLock(&eventLoop.lock);
for (i = 0 ; i < eventLoop.timeoutsCount ; i++) {
if (eventLoop.timeouts[i].deleted)
continue;
if (eventLoop.timeouts[i].timer == timer) {
eventLoop.timeouts[i].deleted = 1;
virEventInterruptLocked();
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
return 0;
}
}
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
return -1;
}
eventLoop.timeouts[i].expiresAt =
now + eventLoop.timeouts[i].frequency;
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
(cb)(timer, opaque);
- virEventLock();
+ virMutexLock(&eventLoop.lock);
}
}
return 0;
EVENT_DEBUG("Dispatch n=%d f=%d w=%d e=%d %p", i,
fds[n].fd, eventLoop.handles[i].watch,
fds[n].revents, eventLoop.handles[i].opaque);
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
(cb)(eventLoop.handles[i].watch,
fds[n].fd, hEvents, opaque);
- virEventLock();
+ virMutexLock(&eventLoop.lock);
}
}
struct pollfd *fds = NULL;
int ret, timeout, nfds;
- virEventLock();
+ virMutexLock(&eventLoop.lock);
eventLoop.running = 1;
- eventLoop.leader = pthread_self();
+ virThreadSelf(&eventLoop.leader);
if (virEventCleanupTimeouts() < 0 ||
virEventCleanupHandles() < 0)
virEventCalculateTimeout(&timeout) < 0)
goto error;
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
retry:
EVENT_DEBUG("Poll on %d handles %p timeout %d", nfds, fds, timeout);
goto error_unlocked;
}
- virEventLock();
+ virMutexLock(&eventLoop.lock);
if (virEventDispatchTimeouts() < 0)
goto error;
goto error;
eventLoop.running = 0;
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
VIR_FREE(fds);
return 0;
error:
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
error_unlocked:
VIR_FREE(fds);
return -1;
}
+
static void virEventHandleWakeup(int watch ATTRIBUTE_UNUSED,
int fd,
int events ATTRIBUTE_UNUSED,
void *opaque ATTRIBUTE_UNUSED)
{
char c;
- virEventLock();
+ virMutexLock(&eventLoop.lock);
ignore_value(saferead(fd, &c, sizeof(c)));
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
+}
+
+#ifdef WIN32
+static inline int pipe(int fd[2]) {
+ return _pipe(fd, 4096, 0);
}
+#endif
int virEventInit(void)
{
- if (pthread_mutex_init(&eventLoop.lock, NULL) != 0)
+ if (virMutexInit(&eventLoop.lock) < 0)
return -1;
if (pipe(eventLoop.wakeupfd) < 0 ||
char c = '\0';
if (!eventLoop.running ||
- pthread_self() == eventLoop.leader) {
- VIR_DEBUG("Skip interrupt, %d %d", eventLoop.running, (int)eventLoop.leader);
+ virThreadIsSelf(&eventLoop.leader)) {
+ VIR_DEBUG("Skip interrupt, %d %d", eventLoop.running, (int)eventLoop.leader.thread);
return 0;
}
int virEventInterrupt(void)
{
int ret;
- virEventLock();
+ virMutexLock(&eventLoop.lock);
ret = virEventInterruptLocked();
- virEventUnlock();
+ virMutexUnlock(&eventLoop.lock);
return ret;
}