Add some more cases to the app-id unit tests
[glib.git] / glib / tests / gwakeuptest.c
blob2ec29bddad9e80b825607265535da4f577f5fba9
1 #include <glib.h>
2 #include <glib/gwakeup.h>
3 #ifdef G_OS_UNIX
4 #include <unistd.h>
5 #endif
7 #ifdef _WIN32
8 static void alarm (int sec) { }
9 #endif
11 static gboolean
12 check_signaled (GWakeup *wakeup)
14 GPollFD fd;
16 g_wakeup_get_pollfd (wakeup, &fd);
17 return g_poll (&fd, 1, 0);
20 static void
21 wait_for_signaled (GWakeup *wakeup)
23 GPollFD fd;
25 g_wakeup_get_pollfd (wakeup, &fd);
26 g_poll (&fd, 1, -1);
29 static void
30 test_semantics (void)
32 GWakeup *wakeup;
33 gint i;
35 /* prevent the test from deadlocking */
36 alarm (60);
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);
49 /* free unused */
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 */
71 alarm (0);
74 struct token
76 gpointer owner;
77 gint ttl;
80 struct context
82 GSList *pending_tokens;
83 GMutex lock;
84 GWakeup *wakeup;
85 gboolean quit;
88 #define NUM_THREADS 50
89 #define NUM_TOKENS 5
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;
97 static void
98 context_init (struct context *ctx)
100 ctx->pending_tokens = NULL;
101 g_mutex_init (&ctx->lock);
102 ctx->wakeup = g_wakeup_new ();
103 ctx->quit = FALSE;
106 static void
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);
116 static void
117 context_quit (struct context *ctx)
119 ctx->quit = TRUE;
120 g_wakeup_signal (ctx->wakeup);
123 static struct token *
124 context_pop_token (struct context *ctx)
126 struct token *token;
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);
134 return token;
137 static void
138 context_push_token (struct context *ctx,
139 struct token *token)
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);
150 static void
151 dispatch_token (struct token *token)
153 if (token->ttl > 0)
155 struct context *ctx;
156 gint next_ctx;
158 next_ctx = g_test_rand_int_range (0, NUM_THREADS);
159 ctx = &contexts[next_ctx];
160 token->owner = ctx;
161 token->ttl--;
163 context_push_token (ctx, token);
165 else
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 *
175 token_new (int ttl)
177 struct token *token;
179 token = g_slice_new (struct token);
180 token->ttl = ttl;
182 g_atomic_int_inc (&tokens_alive);
184 return token;
187 static gpointer
188 thread_func (gpointer data)
190 struct context *ctx = data;
192 while (!ctx->quit)
194 wait_for_signaled (ctx->wakeup);
195 g_wakeup_acknowledge (ctx->wakeup);
197 while (ctx->pending_tokens)
199 struct token *token;
201 token = context_pop_token (ctx);
202 g_assert (token->owner == ctx);
203 dispatch_token (token);
207 return NULL;
210 static void
211 test_threaded (void)
213 gint i;
215 /* make sure we don't block forever */
216 alarm (60);
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
226 * sure that works.
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);
255 /* cancel alarm */
256 alarm (0);
260 main (int argc, char **argv)
262 g_test_init (&argc, &argv, NULL);
264 #ifdef TEST_EVENTFD_FALLBACK
265 #define TESTNAME_SUFFIX "-fallback"
266 #else
267 #define TESTNAME_SUFFIX
268 #endif
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 ();