2015-08-21 07:04:50 +00:00
|
|
|
/*
|
|
|
|
* Wrappers around mutex/cond/thread functions
|
|
|
|
*
|
|
|
|
* Copyright Red Hat, Inc. 2009
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Marcelo Tosatti <mtosatti@redhat.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
2018-02-19 06:27:52 +00:00
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2017-01-20 13:13:21 +00:00
|
|
|
#include "unicorn/platform.h"
|
2015-08-21 07:04:50 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <linux/futex.h>
|
|
|
|
#endif
|
|
|
|
#include "qemu/thread.h"
|
|
|
|
#include "qemu/atomic.h"
|
|
|
|
|
|
|
|
static void error_exit(int err, const char *msg)
|
|
|
|
{
|
2016-01-25 03:46:13 +00:00
|
|
|
fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
|
|
|
|
abort();
|
2015-08-21 07:04:50 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 17:43:41 +00:00
|
|
|
int qemu_thread_create(struct uc_struct *uc, QemuThread *thread, const char *name,
|
2015-08-21 07:04:50 +00:00
|
|
|
void *(*start_routine)(void*),
|
|
|
|
void *arg, int mode)
|
|
|
|
{
|
|
|
|
sigset_t set, oldset;
|
|
|
|
int err;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
|
|
|
|
err = pthread_attr_init(&attr);
|
|
|
|
if (err) {
|
|
|
|
error_exit(err, __func__);
|
2015-11-11 17:43:41 +00:00
|
|
|
return -1;
|
2015-08-21 07:04:50 +00:00
|
|
|
}
|
|
|
|
if (mode == QEMU_THREAD_DETACHED) {
|
|
|
|
err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
if (err) {
|
|
|
|
error_exit(err, __func__);
|
2015-11-11 17:43:41 +00:00
|
|
|
return -1;
|
2015-08-21 07:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Leave signal handling to the iothread. */
|
|
|
|
sigfillset(&set);
|
2019-01-14 00:50:21 +00:00
|
|
|
/* Blocking the signals can result in undefined behaviour. */
|
|
|
|
sigdelset(&set, SIGSEGV);
|
|
|
|
sigdelset(&set, SIGFPE);
|
|
|
|
sigdelset(&set, SIGILL);
|
|
|
|
/* TODO avoid SIGBUS loss on macOS */
|
2015-08-21 07:04:50 +00:00
|
|
|
pthread_sigmask(SIG_SETMASK, &set, &oldset);
|
|
|
|
err = pthread_create(&thread->thread, &attr, start_routine, arg);
|
2015-11-11 17:43:41 +00:00
|
|
|
if (err) {
|
2015-08-21 07:04:50 +00:00
|
|
|
error_exit(err, __func__);
|
2015-11-11 17:43:41 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-08-21 07:04:50 +00:00
|
|
|
|
|
|
|
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
|
|
|
|
|
|
|
|
pthread_attr_destroy(&attr);
|
2015-11-11 17:43:41 +00:00
|
|
|
|
|
|
|
return 0;
|
2015-08-21 07:04:50 +00:00
|
|
|
}
|
|
|
|
|
2015-09-02 08:13:12 +00:00
|
|
|
void qemu_thread_exit(struct uc_struct *uc, void *retval)
|
2015-08-21 07:04:50 +00:00
|
|
|
{
|
|
|
|
pthread_exit(retval);
|
|
|
|
}
|
|
|
|
|
2016-04-23 02:17:04 +00:00
|
|
|
void *qemu_thread_join(QemuThread *thread)
|
2015-08-21 07:04:50 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
void *ret;
|
|
|
|
|
|
|
|
err = pthread_join(thread->thread, &ret);
|
|
|
|
if (err) {
|
|
|
|
error_exit(err, __func__);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|