4 * Copyright Red Hat, Inc. 2013
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.
14 #include "qemu-common.h"
15 #include "qemu/rfifolock.h"
17 static void test_nesting(void)
21 /* Trivial test, ensure the lock is recursive */
22 rfifolock_init(&lock, NULL, NULL);
23 rfifolock_lock(&lock);
24 rfifolock_lock(&lock);
25 rfifolock_lock(&lock);
26 rfifolock_unlock(&lock);
27 rfifolock_unlock(&lock);
28 rfifolock_unlock(&lock);
29 rfifolock_destroy(&lock);
37 static void rfifolock_cb(void *opaque)
39 CallbackTestData *data = opaque;
43 ret = write(data->fd[1], &c, sizeof(c));
47 static void *callback_thread(void *opaque)
49 CallbackTestData *data = opaque;
51 /* The other thread holds the lock so the contention callback will be
54 rfifolock_lock(&data->lock);
55 rfifolock_unlock(&data->lock);
59 static void test_callback(void)
61 CallbackTestData data;
66 rfifolock_init(&data.lock, rfifolock_cb, &data);
67 ret = qemu_pipe(data.fd);
70 /* Hold lock but allow the callback to kick us by writing to the pipe */
71 rfifolock_lock(&data.lock);
72 qemu_thread_create(&thread, "callback_thread",
73 callback_thread, &data, QEMU_THREAD_JOINABLE);
74 ret = read(data.fd[0], &c, sizeof(c));
76 rfifolock_unlock(&data.lock);
77 /* If we got here then the callback was invoked, as expected */
79 qemu_thread_join(&thread);
82 rfifolock_destroy(&data.lock);
85 int main(int argc, char **argv)
87 g_test_init(&argc, &argv, NULL);
88 g_test_add_func("/nesting", test_nesting);
89 g_test_add_func("/callback", test_callback);