4 * Copyright Red Hat, Inc. 2013
7 * Stefan Hajnoczi <stefanha@redhat.com>
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.
13 #include "qemu/osdep.h"
15 #include "qemu-common.h"
16 #include "qemu/rfifolock.h"
18 static void test_nesting(void)
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
);
38 static void rfifolock_cb(void *opaque
)
40 CallbackTestData
*data
= opaque
;
44 ret
= write(data
->fd
[1], &c
, sizeof(c
));
48 static void *callback_thread(void *opaque
)
50 CallbackTestData
*data
= opaque
;
52 /* The other thread holds the lock so the contention callback will be
55 rfifolock_lock(&data
->lock
);
56 rfifolock_unlock(&data
->lock
);
60 static void test_callback(void)
62 CallbackTestData data
;
67 rfifolock_init(&data
.lock
, rfifolock_cb
, &data
);
68 ret
= qemu_pipe(data
.fd
);
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
));
77 rfifolock_unlock(&data
.lock
);
78 /* If we got here then the callback was invoked, as expected */
80 qemu_thread_join(&thread
);
83 rfifolock_destroy(&data
.lock
);
86 int main(int argc
, char **argv
)
88 g_test_init(&argc
, &argv
, NULL
);
89 g_test_add_func("/nesting", test_nesting
);
90 g_test_add_func("/callback", test_callback
);