[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [patch 1/2] qemu: sem/thread helpers
From: |
Marcelo Tosatti |
Subject: |
[Qemu-devel] [patch 1/2] qemu: sem/thread helpers |
Date: |
Wed, 11 Mar 2009 13:16:46 -0300 |
User-agent: |
quilt/0.46-1 |
Index: qemu/Makefile.target
===================================================================
--- qemu.orig/Makefile.target
+++ qemu/Makefile.target
@@ -500,7 +500,7 @@ endif #CONFIG_BSD_USER
# System emulator target
ifndef CONFIG_USER_ONLY
-OBJS=vl.o osdep.o monitor.o pci.o loader.o isa_mmio.o machine.o
+OBJS=vl.o qemu-thread.o osdep.o monitor.o pci.o loader.o isa_mmio.o machine.o
# virtio has to be here due to weird dependency between PCI and virtio-net.
# need to fix this properly
OBJS+=virtio.o virtio-blk.o virtio-balloon.o virtio-net.o virtio-console.o
Index: qemu/vl.c
===================================================================
--- qemu.orig/vl.c
+++ qemu/vl.c
@@ -36,6 +36,7 @@
#include "gdbstub.h"
#include "qemu-timer.h"
#include "qemu-char.h"
+#include "qemu-thread.h"
#include "cache-utils.h"
#include "block.h"
#include "audio/audio.h"
@@ -263,6 +264,8 @@ static QEMUTimer *nographic_timer;
uint8_t qemu_uuid[16];
+QemuSem qemu_sem;
+
/***********************************************************/
/* x86 ISA bus support */
@@ -3650,7 +3653,10 @@ void main_loop_wait(int timeout)
slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
}
#endif
+
+ qemu_sem_unlock(&qemu_sem);
ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
+ qemu_sem_lock(&qemu_sem);
if (ret > 0) {
IOHandlerRecord **pioh;
@@ -3708,6 +3714,9 @@ static int main_loop(void)
#endif
CPUState *env;
+ qemu_sem_init(&qemu_sem);
+ qemu_sem_lock(&qemu_sem);
+
cur_cpu = first_cpu;
next_cpu = cur_cpu->next_cpu ?: first_cpu;
for(;;) {
Index: qemu/qemu-thread.c
===================================================================
--- /dev/null
+++ qemu/qemu-thread.c
@@ -0,0 +1,78 @@
+#include <errno.h>
+#include <time.h>
+#include <signal.h>
+#include "qemu-thread.h"
+
+int qemu_sem_init(QemuSem *sem)
+{
+ return sem_init(&sem->sema, 0, 1);
+}
+
+int qemu_sem_lock(QemuSem *sem)
+{
+ int r;
+
+ while ((r = sem_wait(&sem->sema)) == -1 && errno == EINTR)
+ continue;
+
+ return r;
+}
+
+int qemu_sem_trylock(QemuSem *sem)
+{
+ return sem_trywait(&sem->sema);
+}
+
+static void add_to_timespec(struct timespec *ts, unsigned int msecs)
+{
+ ts->tv_sec = ts->tv_sec + (long)(msecs / 1000);
+ ts->tv_nsec = (ts->tv_nsec + ((long)msecs % 1000) * 1000000);
+ if (ts->tv_nsec >= 1000000000) {
+ ts->tv_nsec -= 1000000000;
+ ts->tv_sec++;
+ }
+}
+
+int qemu_sem_timedlock(QemuSem *sem, unsigned int msecs)
+{
+ int r;
+ struct timespec ts;
+
+ clock_gettime(CLOCK_REALTIME, &ts);
+ add_to_timespec(&ts, msecs);
+
+ while ((r = sem_timedwait(&sem->sema, &ts)) == -1 && errno == EINTR)
+ continue;
+
+ return r;
+}
+
+int qemu_sem_unlock(QemuSem *sem)
+{
+ return sem_post(&sem->sema);
+}
+
+int qemu_thread_create(QemuThread *thread,
+ void *(*start_routine)(void*),
+ void *arg)
+{
+ return pthread_create(&thread->thread, NULL, start_routine, arg);
+}
+
+int qemu_thread_signal(QemuThread *thread, int sig)
+{
+ if (thread->thread != 0)
+ return pthread_kill(thread->thread, sig);
+ return -1; /* XXX: ESCHR */
+}
+
+void qemu_thread_self(QemuThread *thread)
+{
+ thread->thread = pthread_self();
+}
+
+int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2)
+{
+ return (thread1->thread == thread2->thread);
+}
+
Index: qemu/qemu-thread.h
===================================================================
--- /dev/null
+++ qemu/qemu-thread.h
@@ -0,0 +1,27 @@
+#include "semaphore.h"
+#include "pthread.h"
+
+struct QemuSem {
+ sem_t sema;
+};
+
+struct QemuThread {
+ pthread_t thread;
+};
+
+typedef struct QemuSem QemuSem;
+typedef struct QemuThread QemuThread;
+
+int qemu_sem_init(QemuSem *sem);
+int qemu_sem_lock(QemuSem *sem);
+int qemu_sem_trylock(QemuSem *sem);
+int qemu_sem_timedlock(QemuSem *sem, unsigned int msecs);
+int qemu_sem_unlock(QemuSem *sem);
+
+int qemu_thread_create(QemuThread *thread,
+ void *(*start_routine)(void*),
+ void *arg);
+int qemu_thread_signal(QemuThread *thread, int sig);
+void qemu_thread_self(QemuThread *thread);
+int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2);
+
--
[Qemu-devel] [patch 2/2] qemu: separate thread for io, Marcelo Tosatti, 2009/03/11