fsdev: spelling fix
[qemu.git] / tests / test-rfifolock.c
blob9a3cb243baea48179f34985b3bdb56ce2636a57f
1 /*
2 * RFifoLock tests
4 * Copyright Red Hat, Inc. 2013
6 * Authors:
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"
14 #include <glib.h>
15 #include "qemu-common.h"
16 #include "qemu/rfifolock.h"
18 static void test_nesting(void)
20 RFifoLock lock;
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);
33 typedef struct {
34 RFifoLock lock;
35 int fd[2];
36 } CallbackTestData;
38 static void rfifolock_cb(void *opaque)
40 CallbackTestData *data = opaque;
41 int ret;
42 char c = 0;
44 ret = write(data->fd[1], &c, sizeof(c));
45 g_assert(ret == 1);
48 static void *callback_thread(void *opaque)
50 CallbackTestData *data = opaque;
52 /* The other thread holds the lock so the contention callback will be
53 * invoked...
55 rfifolock_lock(&data->lock);
56 rfifolock_unlock(&data->lock);
57 return NULL;
60 static void test_callback(void)
62 CallbackTestData data;
63 QemuThread thread;
64 int ret;
65 char c;
67 rfifolock_init(&data.lock, rfifolock_cb, &data);
68 ret = qemu_pipe(data.fd);
69 g_assert(ret == 0);
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 */
80 qemu_thread_join(&thread);
81 close(data.fd[0]);
82 close(data.fd[1]);
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);
91 return g_test_run();