3 #include <glib/gwakeup.h>
6 void alarm (int sec) { }
10 check_signaled (GWakeup *wakeup)
14 g_wakeup_get_pollfd (wakeup, &fd);
15 return g_poll (&fd, 1, 0);
19 wait_for_signaled (GWakeup *wakeup)
23 g_wakeup_get_pollfd (wakeup, &fd);
33 /* prevent the test from deadlocking */
36 wakeup = g_wakeup_new ();
37 g_assert (!check_signaled (wakeup));
39 g_wakeup_signal (wakeup);
40 g_assert (check_signaled (wakeup));
42 g_wakeup_acknowledge (wakeup);
43 g_assert (!check_signaled (wakeup));
45 g_wakeup_free (wakeup);
48 wakeup = g_wakeup_new ();
49 g_wakeup_free (wakeup);
51 /* free while signaled */
52 wakeup = g_wakeup_new ();
53 g_wakeup_signal (wakeup);
54 g_wakeup_free (wakeup);
56 /* ensure excessive signalling doesn't deadlock */
57 wakeup = g_wakeup_new ();
58 for (i = 0; i < 1000000; i++)
59 g_wakeup_signal (wakeup);
60 g_assert (check_signaled (wakeup));
62 /* ensure a single acknowledgement is sufficient */
63 g_wakeup_acknowledge (wakeup);
64 g_assert (!check_signaled (wakeup));
66 g_wakeup_free (wakeup);
68 /* cancel the alarm */
80 GSList *pending_tokens;
86 #define NUM_THREADS 50
88 #define TOKEN_TTL 100000
90 static struct context contexts[NUM_THREADS];
91 static GThread *threads[NUM_THREADS];
92 static GWakeup *last_token_wakeup;
93 static volatile gint tokens_alive;
96 context_init (struct context *ctx)
98 ctx->pending_tokens = NULL;
99 g_mutex_init (&ctx->lock);
100 ctx->wakeup = g_wakeup_new ();
105 context_clear (struct context *ctx)
107 g_assert (ctx->pending_tokens == NULL);
108 g_assert (ctx->quit);
110 g_wakeup_free (ctx->wakeup);
114 context_quit (struct context *ctx)
117 g_wakeup_signal (ctx->wakeup);
120 static struct token *
121 context_pop_token (struct context *ctx)
125 g_mutex_lock (&ctx->lock);
126 token = ctx->pending_tokens->data;
127 ctx->pending_tokens = g_slist_remove_link (ctx->pending_tokens,
128 ctx->pending_tokens);
129 g_mutex_unlock (&ctx->lock);
135 context_push_token (struct context *ctx,
138 g_assert (token->owner == ctx);
140 g_mutex_lock (&ctx->lock);
141 ctx->pending_tokens = g_slist_prepend (ctx->pending_tokens, token);
142 g_mutex_unlock (&ctx->lock);
144 g_wakeup_signal (ctx->wakeup);
148 dispatch_token (struct token *token)
155 next_ctx = g_test_rand_int_range (0, NUM_THREADS);
156 ctx = &contexts[next_ctx];
160 context_push_token (ctx, token);
164 g_slice_free (struct token, token);
166 if (g_atomic_int_dec_and_test (&tokens_alive))
167 g_wakeup_signal (last_token_wakeup);
171 static struct token *
176 token = g_slice_new (struct token);
179 g_atomic_int_inc (&tokens_alive);
185 thread_func (gpointer data)
187 struct context *ctx = data;
191 wait_for_signaled (ctx->wakeup);
192 g_wakeup_acknowledge (ctx->wakeup);
194 while (ctx->pending_tokens)
198 token = context_pop_token (ctx);
199 g_assert (token->owner == ctx);
200 dispatch_token (token);
212 /* make sure we don't block forever */
215 /* simple mainloop test based on GWakeup.
217 * create a bunch of contexts and a thread to 'run' each one. create
218 * some tokens and randomly pass them between the threads, until the
219 * TTL on each token is zero.
221 * when no tokens are left, signal that we are done. the mainthread
222 * will then signal each worker thread to exit and join them to make
226 last_token_wakeup = g_wakeup_new ();
228 /* create contexts, assign to threads */
229 for (i = 0; i < NUM_THREADS; i++)
231 context_init (&contexts[i]);
232 threads[i] = g_thread_new ("test", thread_func, &contexts[i]);
235 /* dispatch tokens */
236 for (i = 0; i < NUM_TOKENS; i++)
237 dispatch_token (token_new (TOKEN_TTL));
239 /* wait until all tokens are gone */
240 wait_for_signaled (last_token_wakeup);
242 /* ask threads to quit, join them, cleanup */
243 for (i = 0; i < NUM_THREADS; i++)
245 context_quit (&contexts[i]);
246 g_thread_join (threads[i]);
247 context_clear (&contexts[i]);
250 g_wakeup_free (last_token_wakeup);
257 main (int argc, char **argv)
259 g_test_init (&argc, &argv, NULL);
261 #ifdef TEST_EVENTFD_FALLBACK
262 #define TESTNAME_SUFFIX "-fallback"
264 #define TESTNAME_SUFFIX
268 g_test_add_func ("/gwakeup/semantics" TESTNAME_SUFFIX, test_semantics);
269 g_test_add_func ("/gwakeup/threaded" TESTNAME_SUFFIX, test_threaded);
271 return g_test_run ();