• P
    util/qemu-thread-posix: Fix qemu_thread_atexit* for OSX · a458774a
    Peter Maydell 提交于
    Our current implementation of qemu_thread_atexit* is broken on OSX.
    This is because it works by cerating a piece of thread-specific
    data with pthread_key_create() and using the destructor function
    for that data to run the notifier function passed to it by
    the caller of qemu_thread_atexit_add(). The expected use case
    is that the caller uses a __thread variable as the notifier,
    and uses the callback to clean up information that it is
    keeping per-thread in __thread variables.
    
    Unfortunately, on OSX this does not work, because on OSX
    a __thread variable may be destroyed (freed) before the
    pthread_key_create() destructor runs. (POSIX imposes no
    ordering constraint here; the OSX implementation happens
    to implement __thread variables in terms of pthread_key_create((),
    whereas Linux uses different mechanisms that mean the __thread
    variables will still be present when the pthread_key_create()
    destructor is run.)
    
    Fix this by switching to a scheme similar to the one qemu-thread-win32
    uses for qemu_thread_atexit: keep the thread's notifiers on a
    __thread variable, and run the notifiers on calls to
    qemu_thread_exit() and on return from the start routine passed
    to qemu_thread_start(). We do this with the pthread_cleanup_push()
    API.
    
    We take advantage of the qemu_thread_atexit_add() API
    permission not to run thread notifiers on process exit to
    avoid having to special case the main thread.
    Suggested-by: NPaolo Bonzini <pbonzini@redhat.com>
    Signed-off-by: NPeter Maydell <peter.maydell@linaro.org>
    Reviewed-by: NEric Blake <eblake@redhat.com>
    Message-Id: <20181105135538.28025-3-peter.maydell@linaro.org>
    Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
    a458774a
qemu-thread-posix.c 13.0 KB