Debian Codegraph @

testsuite/test-scheduler.c

raw
1 /* test-scheduler.c
2 *
3 * Copyright 2022 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include <libdex.h>
22
23 static DexScheduler *thread_pool;
24 static GMainLoop *main_loop;
25
26 static void
27 test_main_scheduler_simple_cb (gpointer data)
28 {
29 gboolean *count = data;
30 *count = 123;
31 g_main_loop_quit (main_loop);
32 }
33
34 static void
35 test_main_scheduler_simple (void)
36 {
37 DexScheduler *scheduler = dex_scheduler_get_default ();
38 gboolean count = 0;
39
40 g_assert_nonnull (scheduler);
41 g_assert_true (DEX_IS_MAIN_SCHEDULER (scheduler));
42
43 main_loop = g_main_loop_new (NULL, FALSE);
44 dex_scheduler_push (scheduler,
45 test_main_scheduler_simple_cb,
46 &count);
47 g_main_loop_run (main_loop);
48 g_clear_pointer (&main_loop, g_main_loop_unref);
49
50 g_assert_cmpint (count, ==, 123);
51 }
52
53 static DexFuture *
54 test_fiber2_func (gpointer user_data)
55 {
56 guint *count = user_data;
57 g_atomic_int_inc (count);
58 return dex_future_new_for_boolean (TRUE);
59 }
60
61 static DexFuture *
62 test_fiber_func (gpointer user_data)
63 {
64 GPtrArray *all = g_ptr_array_new_with_free_func (dex_unref);
65
66 for (guint i = 0; i < 10; i++)
67 g_ptr_array_add (all, dex_scheduler_spawn (dex_scheduler_get_thread_default (),
68 dex_get_min_stack_size (),
69 test_fiber2_func, user_data, NULL));
70
71 dex_await (dex_future_allv ((DexFuture **)all->pdata, all->len), NULL);
72
73 g_ptr_array_unref (all);
74
75 return NULL;
76 }
77
78 static DexFuture *
79 spawner (gpointer user_data)
80 {
81 GPtrArray *all = g_ptr_array_new_with_free_func (dex_unref);
82
83 for (guint i = 0; i < 1000; i++)
84 g_ptr_array_add (all, dex_scheduler_spawn (thread_pool,
85 dex_get_min_stack_size (),
86 test_fiber_func, user_data, NULL));
87
88 dex_await (dex_future_allv ((DexFuture **)(gpointer)all->pdata, all->len), NULL);
89
90 g_ptr_array_unref (all);
91
92 return NULL;
93 }
94
95 static DexFuture *
96 quit_cb (DexFuture *completed,
97 gpointer user_data)
98 {
99 g_test_message ("Quiting main loop");
100 g_main_loop_quit (main_loop);
101 return NULL;
102 }
103
104 static void
105 test_thread_pool_scheduler_spawn (void)
106 {
107 DexFuture *future;
108 guint count = 0;
109
110 thread_pool = dex_thread_pool_scheduler_new ();
111 main_loop = g_main_loop_new (NULL, FALSE);
112
113 g_test_message ("Spawning with stack size %u",
114 (guint)dex_get_min_stack_size ());
115
116 future = dex_scheduler_spawn (NULL, 0, spawner, &count, NULL);
117 future = dex_future_finally (future, quit_cb, NULL, NULL);
118
119 g_test_message ("Running main loop");
120 g_main_loop_run (main_loop);
121
122 g_assert_cmpint (count, ==, 10*1000);
123
124 dex_unref (future);
125 dex_unref (thread_pool);
126 }
127
128 static void
129 test_thread_pool_scheduler_push_cb (gpointer data)
130 {
131 struct {
132 GMutex mutex;
133 GCond cond;
134 } *syncobj = data;
135
136 g_mutex_lock (&syncobj->mutex);
137 g_cond_signal (&syncobj->cond);
138 g_mutex_unlock (&syncobj->mutex);
139 }
140
141 static void
142 test_thread_pool_scheduler_push (void)
143 {
144 struct {
145 GMutex mutex;
146 GCond cond;
147 } syncobj;
148
149 g_mutex_init (&syncobj.mutex);
150 g_cond_init (&syncobj.cond);
151
152 g_mutex_lock (&syncobj.mutex);
153 dex_scheduler_push (dex_thread_pool_scheduler_get_default (),
154 test_thread_pool_scheduler_push_cb,
155 &syncobj);
156 g_cond_wait (&syncobj.cond, &syncobj.mutex);
157 g_mutex_unlock (&syncobj.mutex);
158
159 g_mutex_clear (&syncobj.mutex);
160 g_cond_clear (&syncobj.cond);
161 }
162
163 int
164 main (int argc,
165 char *argv[])
166 {
167 dex_init ();
168 g_test_init (&argc, &argv, NULL);
169 g_test_add_func ("/Dex/TestSuite/MainScheduler/simple", test_main_scheduler_simple);
170 g_test_add_func ("/Dex/TestSuite/ThreadPoolScheduler/10_000_fibers", test_thread_pool_scheduler_spawn);
171 g_test_add_func ("/Dex/TestSuite/ThreadPoolScheduler/push", test_thread_pool_scheduler_push);
172 return g_test_run ();
173 }