add a header file for atomic operations.

We'll need several of them. __sync builtins are just too ugly to type,
and do not provide seqcst load/store operations.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
rename qemu_event_{init,read}

qemu_event_init clashes with the next patch.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-thread: add QemuEvent

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);
    }

Alternatively:

    ... compute condition ...
    if (condition) {
        do {
            qemu_event_wait(ev);
            qemu_event_reset(ev);
            ... compute condition ...
        } while(condition);
        qemu_event_set(ev);
    }

QemuEvent provides a very fast userspace path in the common case when
no other thread is waiting, or the event is not changing state.  It
is used to report RCU quiescent states to the thread calling
synchronize_rcu (the latter being the single consumer), and to report
call_rcu invocations to the thread that receives them.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-thread: add global initialization for mutexes and condvars

This is useful to replace once-only initialization in most cases.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-thread: add QemuOnce

Add Windows and POSIX versions, as usual.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-thread: add TLS wrappers

Win32 emulated TLS is slow and is not available on all versions of GCC;
some versions of Unix only have pthread_getspecific as a means to access
TLS.

Actually, Win32 does have support for decent TLS, and GCC does not map
__thread to it.  But kind of unlike ELF TLS, it's perfectly possible
to declare TLS variables with simple C code!  For pthread_getspecific
we similarly allocate a memory block; we have to compute all the offsets
at load time, which is also cheaper than doing a pthread_key_create for
each variable.  Not optimal, but it works.

This patch adds wrappers to qemu-thread that will use __thread or
pthread_getspecific on POSIX systems, and the .tls segment on Windows.

It does kinda uglify the declarations, but not too much.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
add rcu library

This includes a (mangled) copy of the urcu-qsbr code from liburcu.
The main changes are: 1) removing dependencies on many other header files
in liburcu; 2) removing for simplicity the tentative busy waiting in
synchronize_rcu, which has limited performance effects; 3) replacing
futexes in synchronize_rcu with QemuEvents for Win32 portability.
The API is the same as liburcu, so it should be possible in the future
to require liburcu on POSIX systems for example and use our copy only
on Windows.

Among the various versions available I chose urcu-qsbr, which has the
fastest rcu_read_{lock,unlock} but requires the program to manually
annotate quiescent points or intervals.  QEMU threads usually have easily
identified quiescent periods, so this should not be a problem.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
rcu: add rcutorture

A stress test program (works :)).  So far not ported to Windows.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
osdep: add qemu_msleep

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
add call_rcu support

Unlike the RCU code, this is not (yet) meant to be replaced by liburcu,
because we want to run call_rcu callbacks in the main thread, so that
they run under the global lock.

The data structure is indeed based on those found in liburcu (this makes
me feel safer), but adapted to replace futexes with QemuEvents.

This has a dependency on the iothread, because it relies on the fact that
qemu_schedule_bh works outside the global lock.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
rcu: report quiescent states

Our flavor of RCU requires threads to communicate when they are going
offline for extended periods of time on a condition variable, or waiting
for I/O, or executing guest code.  Add markers to this end.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
rcuify iohandlers

Just a proof of concept, with the write-side still under the global
lock, in order to test the call_rcu code.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
split MRU ram list

Outside the execution threads the normal, non-MRU-ized order of
the RAM blocks should always be enough.  So manage two separate
lists, which will have separate locking rules.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
RCUify ram_list

Adding a separate mutex that protects the write sides.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
RCUify savevm handlers

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-thread: open code the Win32 mutex

This allows us to use an initializer in the style of pthread mutexes.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>