These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / tests / test-rfifolock.c
1 /*
2  * RFifoLock tests
3  *
4  * Copyright Red Hat, Inc. 2013
5  *
6  * Authors:
7  *  Stefan Hajnoczi    <stefanha@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  */
12
13 #include "qemu/osdep.h"
14 #include <glib.h>
15 #include "qemu-common.h"
16 #include "qemu/rfifolock.h"
17
18 static void test_nesting(void)
19 {
20     RFifoLock lock;
21
22     /* Trivial test, ensure the lock is recursive */
23     rfifolock_init(&lock, NULL, NULL);
24     rfifolock_lock(&lock);
25     rfifolock_lock(&lock);
26     rfifolock_lock(&lock);
27     rfifolock_unlock(&lock);
28     rfifolock_unlock(&lock);
29     rfifolock_unlock(&lock);
30     rfifolock_destroy(&lock);
31 }
32
33 typedef struct {
34     RFifoLock lock;
35     int fd[2];
36 } CallbackTestData;
37
38 static void rfifolock_cb(void *opaque)
39 {
40     CallbackTestData *data = opaque;
41     int ret;
42     char c = 0;
43
44     ret = write(data->fd[1], &c, sizeof(c));
45     g_assert(ret == 1);
46 }
47
48 static void *callback_thread(void *opaque)
49 {
50     CallbackTestData *data = opaque;
51
52     /* The other thread holds the lock so the contention callback will be
53      * invoked...
54      */
55     rfifolock_lock(&data->lock);
56     rfifolock_unlock(&data->lock);
57     return NULL;
58 }
59
60 static void test_callback(void)
61 {
62     CallbackTestData data;
63     QemuThread thread;
64     int ret;
65     char c;
66
67     rfifolock_init(&data.lock, rfifolock_cb, &data);
68     ret = qemu_pipe(data.fd);
69     g_assert(ret == 0);
70
71     /* Hold lock but allow the callback to kick us by writing to the pipe */
72     rfifolock_lock(&data.lock);
73     qemu_thread_create(&thread, "callback_thread",
74                        callback_thread, &data, QEMU_THREAD_JOINABLE);
75     ret = read(data.fd[0], &c, sizeof(c));
76     g_assert(ret == 1);
77     rfifolock_unlock(&data.lock);
78     /* If we got here then the callback was invoked, as expected */
79
80     qemu_thread_join(&thread);
81     close(data.fd[0]);
82     close(data.fd[1]);
83     rfifolock_destroy(&data.lock);
84 }
85
86 int main(int argc, char **argv)
87 {
88     g_test_init(&argc, &argv, NULL);
89     g_test_add_func("/nesting", test_nesting);
90     g_test_add_func("/callback", test_callback);
91     return g_test_run();
92 }