2 #include <glib/gwakeup.h>
8 static void alarm (int sec) { }
12 check_signaled (GWakeup *wakeup)
16 g_wakeup_get_pollfd (wakeup, &fd);
17 return g_poll (&fd, 1, 0);
21 wait_for_signaled (GWakeup *wakeup)
25 g_wakeup_get_pollfd (wakeup, &fd);
35 /* prevent the test from deadlocking */
38 wakeup = g_wakeup_new ();
39 g_assert (!check_signaled (wakeup));
41 g_wakeup_signal (wakeup);
42 g_assert (check_signaled (wakeup));
44 g_wakeup_acknowledge (wakeup);
45 g_assert (!check_signaled (wakeup));
47 g_wakeup_free (wakeup);
50 wakeup = g_wakeup_new ();
51 g_wakeup_free (wakeup);
53 /* free while signaled */
54 wakeup = g_wakeup_new ();
55 g_wakeup_signal (wakeup);
56 g_wakeup_free (wakeup);
58 /* ensure excessive signalling doesn't deadlock */
59 wakeup = g_wakeup_new ();
60 for (i = 0; i < 1000000; i++)
61 g_wakeup_signal (wakeup);
62 g_assert (check_signaled (wakeup));
64 /* ensure a single acknowledgement is sufficient */
65 g_wakeup_acknowledge (wakeup);
66 g_assert (!check_signaled (wakeup));
68 g_wakeup_free (wakeup);
70 /* cancel the alarm */
82 GSList *pending_tokens;
88 #define NUM_THREADS 50
90 #define TOKEN_TTL 100000
92 static struct context contexts[NUM_THREADS];
93 static GThread *threads[NUM_THREADS];
94 static GWakeup *last_token_wakeup;
95 static volatile gint tokens_alive;
98 context_init (struct context *ctx)
100 ctx->pending_tokens = NULL;
101 g_mutex_init (&ctx->lock);
102 ctx->wakeup = g_wakeup_new ();
107 context_clear (struct context *ctx)
109 g_assert (ctx->pending_tokens == NULL);
110 g_assert (ctx->quit);
112 g_mutex_clear (&ctx->lock);
113 g_wakeup_free (ctx->wakeup);
117 context_quit (struct context *ctx)
120 g_wakeup_signal (ctx->wakeup);
123 static struct token *
124 context_pop_token (struct context *ctx)
128 g_mutex_lock (&ctx->lock);
129 token = ctx->pending_tokens->data;
130 ctx->pending_tokens = g_slist_delete_link (ctx->pending_tokens,
131 ctx->pending_tokens);
132 g_mutex_unlock (&ctx->lock);
138 context_push_token (struct context *ctx,
141 g_assert (token->owner == ctx);
143 g_mutex_lock (&ctx->lock);
144 ctx->pending_tokens = g_slist_prepend (ctx->pending_tokens, token);
145 g_mutex_unlock (&ctx->lock);
147 g_wakeup_signal (ctx->wakeup);
151 dispatch_token (struct token *token)
158 next_ctx = g_test_rand_int_range (0, NUM_THREADS);
159 ctx = &contexts[next_ctx];
163 context_push_token (ctx, token);
167 g_slice_free (struct token, token);
169 if (g_atomic_int_dec_and_test (&tokens_alive))
170 g_wakeup_signal (last_token_wakeup);
174 static struct token *
179 token = g_slice_new (struct token);
182 g_atomic_int_inc (&tokens_alive);
188 thread_func (gpointer data)
190 struct context *ctx = data;
194 wait_for_signaled (ctx->wakeup);
195 g_wakeup_acknowledge (ctx->wakeup);
197 while (ctx->pending_tokens)
201 token = context_pop_token (ctx);
202 g_assert (token->owner == ctx);
203 dispatch_token (token);
215 /* make sure we don't block forever */
218 /* simple mainloop test based on GWakeup.
220 * create a bunch of contexts and a thread to 'run' each one. create
221 * some tokens and randomly pass them between the threads, until the
222 * TTL on each token is zero.
224 * when no tokens are left, signal that we are done. the mainthread
225 * will then signal each worker thread to exit and join them to make
229 last_token_wakeup = g_wakeup_new ();
231 /* create contexts, assign to threads */
232 for (i = 0; i < NUM_THREADS; i++)
234 context_init (&contexts[i]);
235 threads[i] = g_thread_new ("test", thread_func, &contexts[i]);
238 /* dispatch tokens */
239 for (i = 0; i < NUM_TOKENS; i++)
240 dispatch_token (token_new (TOKEN_TTL));
242 /* wait until all tokens are gone */
243 wait_for_signaled (last_token_wakeup);
245 /* ask threads to quit, join them, cleanup */
246 for (i = 0; i < NUM_THREADS; i++)
248 context_quit (&contexts[i]);
249 g_thread_join (threads[i]);
250 context_clear (&contexts[i]);
253 g_wakeup_free (last_token_wakeup);
260 main (int argc, char **argv)
262 g_test_init (&argc, &argv, NULL);
264 #ifdef TEST_EVENTFD_FALLBACK
265 #define TESTNAME_SUFFIX "-fallback"
267 #define TESTNAME_SUFFIX
271 g_test_add_func ("/gwakeup/semantics" TESTNAME_SUFFIX, test_semantics);
272 g_test_add_func ("/gwakeup/threaded" TESTNAME_SUFFIX, test_threaded);
274 return g_test_run ();