summaryrefslogtreecommitdiff
path: root/tests/test-rfifolock.c
blob: 0572ebb42a8d9b8ec0680cf5ef80bfaf38281c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * RFifoLock tests
 *
 * Copyright Red Hat, Inc. 2013
 *
 * Authors:
 *  Stefan Hajnoczi    <stefanha@redhat.com>
 *
 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
 * See the COPYING.LIB file in the top-level directory.
 */

#include <glib.h>
#include "qemu-common.h"
#include "qemu/rfifolock.h"

static void test_nesting(void)
{
    RFifoLock lock;

    /* Trivial test, ensure the lock is recursive */
    rfifolock_init(&lock, NULL, NULL);
    rfifolock_lock(&lock);
    rfifolock_lock(&lock);
    rfifolock_lock(&lock);
    rfifolock_unlock(&lock);
    rfifolock_unlock(&lock);
    rfifolock_unlock(&lock);
    rfifolock_destroy(&lock);
}

typedef struct {
    RFifoLock lock;
    int fd[2];
} CallbackTestData;

static void rfifolock_cb(void *opaque)
{
    CallbackTestData *data = opaque;
    int ret;
    char c = 0;

    ret = write(data->fd[1], &c, sizeof(c));
    g_assert(ret == 1);
}

static void *callback_thread(void *opaque)
{
    CallbackTestData *data = opaque;

    /* The other thread holds the lock so the contention callback will be
     * invoked...
     */
    rfifolock_lock(&data->lock);
    rfifolock_unlock(&data->lock);
    return NULL;
}

static void test_callback(void)
{
    CallbackTestData data;
    QemuThread thread;
    int ret;
    char c;

    rfifolock_init(&data.lock, rfifolock_cb, &data);
    ret = qemu_pipe(data.fd);
    g_assert(ret == 0);

    /* Hold lock but allow the callback to kick us by writing to the pipe */
    rfifolock_lock(&data.lock);
    qemu_thread_create(&thread, "callback_thread",
                       callback_thread, &data, QEMU_THREAD_JOINABLE);
    ret = read(data.fd[0], &c, sizeof(c));
    g_assert(ret == 1);
    rfifolock_unlock(&data.lock);
    /* If we got here then the callback was invoked, as expected */

    qemu_thread_join(&thread);
    close(data.fd[0]);
    close(data.fd[1]);
    rfifolock_destroy(&data.lock);
}

int main(int argc, char **argv)
{
    g_test_init(&argc, &argv, NULL);
    g_test_add_func("/nesting", test_nesting);
    g_test_add_func("/callback", test_callback);
    return g_test_run();
}