Skip to contentSkip to conversation

Threads and win32

Qemu-devel Teemu Nätkinniemi 4 participants 8 messages Jul 18, 2009 – Jul 21, 2009

lore
Share & tools
lore
#0 lore raw

Hello!

Are there any plans to make qemu-thread.c compatible with win32? At the moment compilation errors as CLOCK_REALTIME is not available on MinGW and I don't know any way around this.

Thanks for any solutions!

#1 lore raw
Quoted 6 lines · Teemu Nätkinniemi

Hello!

Are there any plans to make qemu-thread.c compatible with win32? At the moment compilation errors as CLOCK_REALTIME is not available on MinGW and I don't know any way around this.

Thanks for any solutions!

I have noticed the problem back in April when the IO thread work got commited, but noone seemed to care [1].

Marcelo, can gettimeofday be used instead of clock_gettime?

[1] http://lists.gnu.org/archive/html/qemu-devel/2009-04/msg01656.html

- Sebastian

#2 lore raw
Quoted 8 lines · Sebastian Herbszt
Quoted 6 lines · Teemu Nätkinniemi

Hello!

Are there any plans to make qemu-thread.c compatible with win32? At the moment compilation errors as CLOCK_REALTIME is not available on MinGW and I don't know any way around this.

Thanks for any solutions!

Quoted 7 lines

I have noticed the problem back in April when the IO thread work got commited, but noone seemed to care [1].

Marcelo, can gettimeofday be used instead of clock_gettime?

[1] http://lists.gnu.org/archive/html/qemu-devel/2009-04/msg01656.html

- Sebastian

Sure, gettimeofday instead of clock_gettime is fine.

#3 lore raw
Quoted 4 lines · Marcelo Tosatti
Quoted 2 lines · Sebastian Herbszt

Marcelo, can gettimeofday be used instead of clock_gettime?

Sure, gettimeofday instead of clock_gettime is fine.

If clock_gettime() is replaced by gettimeofday() qemu-thread.c does compile on MinGW if also the pthread_equal patch [1] is applied. Unfortunatelly there is a ton of compile errors in vl.c due to incomplete signal support (?).

[1] http://lists.gnu.org/archive/html/qemu-devel/2009-07/msg01514.html

- Sebastian

[PATCH] qemu-thread: replace clock_gettime() by gettimeofday()

Replace clock_gettime() which is not available on MinGW by gettimeofday().

qemu-thread.c Mon Jul 20 18:53:53 2009 renamed from qemu-thread.c.orig Mon Jul 20 18:38:24 2009 +0 -0
@@ -14,6 +14,7 @@

#include <stdio.h>
#include <errno.h>
#include <time.h>
+#include <sys/time.h>
#include <signal.h>
#include <stdint.h>
#include <string.h>
@@ -62,8 +63,11 @@ int qemu_mutex_timedlock(QemuMutex *mute

{
    int err;
    struct timespec ts;

+ struct timeval now;

-    clock_gettime(CLOCK_REALTIME, &ts);
+    gettimeofday(&now, NULL);
+    ts.tv_sec = now.tv_sec;
+    ts.tv_nsec = now.tv_usec * 1000;
    timespec_add_ms(&ts, msecs);

    err = pthread_mutex_timedlock(&mutex->lock, &ts);
@@ -120,9 +124,12 @@ void qemu_cond_wait(QemuCond *cond, Qemu
int qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex, uint64_t msecs)
{
    struct timespec ts;
+    struct timeval now;
    int err;
-    clock_gettime(CLOCK_REALTIME, &ts);
+    gettimeofday(&now, NULL);
+    ts.tv_sec = now.tv_sec;
+    ts.tv_nsec = now.tv_usec * 1000;
    timespec_add_ms(&ts, msecs);

    err = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts);
#4 lore raw
Quoted 10 lines · Sebastian Herbszt
Quoted 4 lines · Marcelo Tosatti
Quoted 2 lines · Sebastian Herbszt

Marcelo, can gettimeofday be used instead of clock_gettime?

Sure, gettimeofday instead of clock_gettime is fine.

If clock_gettime() is replaced by gettimeofday() qemu-thread.c does compile on MinGW if also the pthread_equal patch [1] is applied. Unfortunatelly there is a ton of compile errors in vl.c due to incomplete signal support (?).

Quoted 7 lines

[1] http://lists.gnu.org/archive/html/qemu-devel/2009-07/msg01514.html

- Sebastian

[PATCH] qemu-thread: replace clock_gettime() by gettimeofday()

Replace clock_gettime() which is not available on MinGW by gettimeofday().

Sweet. Have you tested it?

Do the signal warnings make any sense??

Quoted 40 lines
qemu-thread.c Mon Jul 20 18:53:53 2009 renamed from qemu-thread.c.orig Mon Jul 20 18:38:24 2009 +9 -2
@@ -14,6 +14,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <time.h>
+#include <sys/time.h>
 #include <signal.h>
 #include <stdint.h>
 #include <string.h>
@@ -62,8 +63,11 @@ int qemu_mutex_timedlock(QemuMutex *mute
 {
    int err;
    struct timespec ts;
+    struct timeval now;
 
-    clock_gettime(CLOCK_REALTIME, &ts);
+    gettimeofday(&now, NULL);
+    ts.tv_sec = now.tv_sec;
+    ts.tv_nsec = now.tv_usec * 1000;
    timespec_add_ms(&ts, msecs);
 
    err = pthread_mutex_timedlock(&mutex->lock, &ts);
@@ -120,9 +124,12 @@ void qemu_cond_wait(QemuCond *cond, Qemu
 int qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex, uint64_t msecs)
 {
    struct timespec ts;
+    struct timeval now;
    int err;
 
-    clock_gettime(CLOCK_REALTIME, &ts);
+    gettimeofday(&now, NULL);
+    ts.tv_sec = now.tv_sec;
+    ts.tv_nsec = now.tv_usec * 1000;
    timespec_add_ms(&ts, msecs);
 
    err = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts);
#5 lore raw
Quoted 6 lines · Marcelo Tosatti
Quoted 3 lines · Sebastian Herbszt

[PATCH] qemu-thread: replace clock_gettime() by gettimeofday()

Replace clock_gettime() which is not available on MinGW by gettimeofday().

Sweet. Have you tested it?

Slightly on linux.

Quoted 1 line

Do the signal warnings make any sense??

CC i386-softmmu/vl.o
vl.c: In function `qemu_cpu_kick':
vl.c:3842: error: `SIGUSR1' undeclared (first use in this function) vl.c:3842: error: (Each undeclared identifier is reported only once vl.c:3842: error: for each function it appears in.)
vl.c: In function `block_io_signals':
vl.c:3859: error: storage size of 'sigact' isn't known
vl.c:3861: warning: implicit declaration of function sigemptyset' vl.c:3862: warning: implicit declaration of function sigaddset' vl.c:3862: error: SIGUSR2' undeclared (first use in this function) vl.c:3863: error: SIGIO' undeclared (first use in this function) vl.c:3864: error: SIGALRM' undeclared (first use in this function) vl.c:3865: warning: implicit declaration of function pthread_sigmask' vl.c:3868: error: SIGUSR1' undeclared (first use in this function) vl.c:3873: warning: implicit declaration of function sigaction' vl.c:3859: warning: unused variable `sigact'
vl.c: In function `unblock_io_signals':
vl.c:3861: warning: redundant redeclaration of 'sigemptyset' vl.c:3861: warning: previous implicit declaration of 'sigemptyset' was here vl.c:3862: warning: redundant redeclaration of 'sigaddset' vl.c:3862: warning: previous implicit declaration of 'sigaddset' was here vl.c:3881: error: SIGUSR2' undeclared (first use in this function) vl.c:3882: error: SIGIO' undeclared (first use in this function) vl.c:3883: error: `SIGALRM' undeclared (first use in this function) vl.c:3865: warning: redundant redeclaration of 'pthread_sigmask' vl.c:3865: warning: previous implicit declaration of 'pthread_sigmask' was here vl.c:3887: error: SIGUSR1' undeclared (first use in this function) vl.c: In function qemu_signal_lock':
vl.c:3896: error: SIGUSR1' undeclared (first use in this function) vl.c: In function pause_all_vcpus':
vl.c:3937: error: SIGUSR1' undeclared (first use in this function) vl.c: In function resume_all_vcpus':
vl.c:3959: error: `SIGUSR1' undeclared (first use in this function) make[1]: *** [vl.o] Error 1
make: *** [subdir-i386-softmmu] Error 2

- Sebastian

#6 lore raw
Quoted 5 lines · Sebastian Herbszt

vl.c:3873: warning: implicit declaration of function sigaction' vl.c:3887: error: SIGUSR1' undeclared (first use in this function) vl.c:3881: error: SIGUSR2' undeclared (first use in this function) vl.c:3882: error: SIGIO' undeclared (first use in this function) vl.c:3883: error: `SIGALRM' undeclared (first use in this function)

It's not surprising.

WIN32 doesn't have unix-style signals :-)

WIN32 doesn't have pthreads either, but there's a pthread-compatibility library people often use. That provides pthreads emulation but not signal emulation. WIN32 does provide threads and locking primitives, and it's not too painful to build pthreads on t top of that.

To emulate signals you really have to use Cygwin or one of the other unix-on-WIN32 emulation packages. Or change the code to not use signals.

WIN32 has native async I/O, by the way. I don't know if it's suitable, especially since we found how broken Linux async I/O is despite the simple API, but Windows has had it since the early 90s (all versions derived from Windows NT) and inherited the design from VMS, so it's probably in good shape.

-- Jamie

#7 lore raw
In reply to Sebastian Herbszt
Quoted 53 lines
Quoted 3 lines

Do the signal warnings make any sense??

  CC    i386-softmmu/vl.o
vl.c: In function `qemu_cpu_kick':
vl.c:3842: error: `SIGUSR1' undeclared (first use in this function)
vl.c:3842: error: (Each undeclared identifier is reported only once
vl.c:3842: error: for each function it appears in.) vl.c: In function `block_io_signals':
vl.c:3859: error: storage size of 'sigact' isn't known vl.c:3861: warning: implicit declaration of function `sigemptyset'
vl.c:3862: warning: implicit declaration of function `sigaddset'
vl.c:3862: error: `SIGUSR2' undeclared (first use in this function)
vl.c:3863: error: `SIGIO' undeclared (first use in this function)
vl.c:3864: error: `SIGALRM' undeclared (first use in this function)
vl.c:3865: warning: implicit declaration of function `pthread_sigmask'
vl.c:3868: error: `SIGUSR1' undeclared (first use in this function)
vl.c:3873: warning: implicit declaration of function `sigaction'
vl.c:3859: warning: unused variable sigact' vl.c: In function unblock_io_signals':
vl.c:3861: warning: redundant redeclaration of 'sigemptyset'
vl.c:3861: warning: previous implicit declaration of 'sigemptyset' was here
vl.c:3862: warning: redundant redeclaration of 'sigaddset' vl.c:3862: warning: previous implicit declaration of 'sigaddset' was here
vl.c:3881: error: `SIGUSR2' undeclared (first use in this function)
vl.c:3882: error: `SIGIO' undeclared (first use in this function)
vl.c:3883: error: `SIGALRM' undeclared (first use in this function)
vl.c:3865: warning: redundant redeclaration of 'pthread_sigmask'
vl.c:3865: warning: previous implicit declaration of 'pthread_sigmask' was here
vl.c:3887: error: `SIGUSR1' undeclared (first use in this function)
vl.c: In function `qemu_signal_lock':
vl.c:3896: error: `SIGUSR1' undeclared (first use in this function)

Quoted 8 lines

vl.c: In function `pause_all_vcpus':
vl.c:3937: error: `SIGUSR1' undeclared (first use in this function)
vl.c: In function `resume_all_vcpus':
vl.c:3959: error: `SIGUSR1' undeclared (first use in this function)
make[1]: *** [vl.o] Error 1
make: *** [subdir-i386-softmmu] Error 2

MinGW's signal support is very limited and it does not have these signals. One solution would be to use GnuWin32 support library (http://gnuwin32.sourceforge.net/) but I encounter following errors:

In file included from /mingw/lib/gcc/mingw32/../../../include/winx/signalx.h:5,

                 from D:/msys/src/qemu/vl.c:27:

/mingw/lib/gcc/mingw32/../../../include/winx/sys/signalx.h:57: error: expected specifier-qualifier-list before '__sigset_t' /mingw/lib/gcc/mingw32/../../../include/winx/sys/signalx.h:64: error: expected specifier-qualifier-list before '__ptr_t' /mingw/lib/gcc/mingw32/../../../include/winx/sys/signalx.h:72: error: expected specifier-qualifier-list before '__ptr_t' D:/msys/src/qemu/vl.c: In function 'block_io_signals':
D:/msys/src/qemu/vl.c:3865: warning: implicit declaration of function 'sigaddset' D:/msys/src/qemu/vl.c:3868: warning: implicit declaration of function 'pthread_sigmask' D:/msys/src/qemu/vl.c:3876: warning: implicit declaration of function 'sigaction' D:/msys/src/qemu/vl.c: In function 'qemu_uuid_parse':
D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: unknown conversion type character 'h' in format D:/msys/src/qemu/vl.c:4664: warning: too many arguments for format
make[1]: *** [vl.o] Error 1
make: *** [subdir-i386-softmmu] Error 2

The actual include file winx/sys/signalx.h looks like this:

#ifndef __WINX_SYS_SIGNALX_H__
#define __WINX_SYS_SIGNALX_H__

#include <features.h>
#include <sys/types.h>
#include <bits/sigset.h>

/* Type of a signal handler. */
typedef void (*__sighandler_t) (int);

#ifdef __USE_BSD
typedef __sighandler_t sig_t;
#endif

#  define SIGHUP      -1
#  define SIGQUIT     -3
#  define SIGTRAP     -5
#  define SIGIOT      -6
#  define SIGEMT      -7
#  define SIGKILL     -9
#  define SIGBUS      -10
#  define SIGSYS      -12
#  define SIGPIPE     -13
#  define SIGALRM     -14
#  define SIGURG      -16
#  define SIGSTOP     -17
#  define SIGTSTP     -18
#  define SIGCONT     -19
#  define SIGCHLD     -20
#  define SIGTTIN     -21
#  define SIGTTOU     -22
#  define SIGIO       -23
#  define SIGXCPU     -24
#  define SIGXFSZ     -25
#  define SIGVTALRM   -26
#  define SIGPROF     -27
#  define SIGWINCH    -28
#  define SIGLOST     -29
#  define SIGUSR1     -30
#  define SIGUSR2     -32

#define SIG_SETMASK 0 /* set mask with sigprocmask() */
#define SIG_BLOCK 1 /* set of signals to block */
#define SIG_UNBLOCK 2 /* set of signals to, well, unblock */

/* Type of a signal handler. */
typedef void (*__sighandler_t) (int);

struct sigaction

  {
    /* Signal handler.  */
    __sighandler_t sa_handler;
    /* Additional set of signals to be blocked.  */
    __sigset_t sa_mask;
    /* Special flags.  */
    int sa_flags;

};

/* Structure describing a signal stack (obsolete). */ struct sigstack
{
__ptr_t ss_sp; /* Signal stack pointer. */
int ss_onstack; /* Nonzero if executing on this stack. */
};

/* Alternate, preferred interface. */
typedef struct sigaltstack

  {
    __ptr_t ss_sp;
    size_t ss_size;
    int ss_flags;

} stack_t;

/* POSIX sigsetjmp/siglongjmp macros */
#define sigjmp_buf jmp_buf

#define _SAVEMASK _JBLEN
#define _SIGMASK (_JBLEN+1)

/*

#define sigsetjmp(env, savemask) ((env)[_SAVEMASK] = savemask,\
               sigprocmask (SIG_SETMASK, 0, (sigset_t *) ((env) + _SIGMASK)),\
               setjmp (env))

#define siglongjmp(env, val) ((((env)[_SAVEMASK])?\
sigprocmask (SIG_SETMASK, (sigset_t *) ((env) + _SIGMASK), 0):0),\
longjmp (env, val))

#define sigsetjmp(env, savemask) (1)
#define siglongjmp(env, val) (1)
*/
#ifndef sigemptyset

# define sigemptyset(s) (*(s) = 0)
#endif
#ifndef sigmask
# define sigmask(sig) (1 << ((sig) - 1))
#endif
/* # define sigaddset(s, sig) (*(s) |= sigmask (sig)) */

#endif /* __WINX_SYS_SIGNALX_H__ */

Teemu