This emulates Win32 manual-reset events using futexes or conditional
variables.  Typical ways to use them are with multi-producer,
single-consumer data structures, to test for a complex condition whose
elements come from different threads:
    for (;;) {
        qemu_event_reset(ev);
        ... test complex condition ...
        if (condition is true) {
            break;
        }
        qemu_event_wait(ev);
    }
Or more efficiently (but with some duplication):
    ... evaluate condition ...
    while (!condition) {
        qemu_event_reset(ev);
        ... evaluate condition ...
        if (!condition) {
            qemu_event_wait(ev);
            ... evaluate condition ...
        }
    }
QemuEvent provides a very fast userspace path in the common case when
no other thread is waiting, or the event is not changing state.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
		
	
			
		
			
				
	
	
		
			37 lines
		
	
	
		
			551 B
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			37 lines
		
	
	
		
			551 B
		
	
	
	
		
			C
		
	
	
	
	
	
#ifndef __QEMU_THREAD_POSIX_H
 | 
						|
#define __QEMU_THREAD_POSIX_H 1
 | 
						|
#include "pthread.h"
 | 
						|
#include <semaphore.h>
 | 
						|
 | 
						|
struct QemuMutex {
 | 
						|
    pthread_mutex_t lock;
 | 
						|
};
 | 
						|
 | 
						|
struct QemuCond {
 | 
						|
    pthread_cond_t cond;
 | 
						|
};
 | 
						|
 | 
						|
struct QemuSemaphore {
 | 
						|
#if defined(__APPLE__) || defined(__NetBSD__)
 | 
						|
    pthread_mutex_t lock;
 | 
						|
    pthread_cond_t cond;
 | 
						|
    unsigned int count;
 | 
						|
#else
 | 
						|
    sem_t sem;
 | 
						|
#endif
 | 
						|
};
 | 
						|
 | 
						|
struct QemuEvent {
 | 
						|
#ifndef __linux__
 | 
						|
    pthread_mutex_t lock;
 | 
						|
    pthread_cond_t cond;
 | 
						|
#endif
 | 
						|
    unsigned value;
 | 
						|
};
 | 
						|
 | 
						|
struct QemuThread {
 | 
						|
    pthread_t thread;
 | 
						|
};
 | 
						|
 | 
						|
#endif
 |