| 1 |
/* test-semaphore.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 |
#include "dex-aio-backend-private.h" |
| 24 |
#include "dex-main-scheduler-private.h" |
| 25 |
#include "dex-semaphore-private.h" |
| 26 |
#include "dex-thread-storage-private.h" |
| 27 |
|
| 28 |
#define N_THREADS 32 |
| 29 |
|
| 30 |
static guint total_count; |
| 31 |
static gboolean in_shutdown; |
| 32 |
static guint done; |
| 33 |
|
| 34 |
typedef struct |
| 35 |
{ |
| 36 |
DexSemaphore *semaphore; |
| 37 |
GThread *thread; |
| 38 |
GSource *source; |
| 39 |
int threadid; |
| 40 |
int handled; |
| 41 |
} WorkerState; |
| 42 |
|
| 43 |
static DexFuture * |
| 44 |
worker_thread_callback (DexFuture *future, |
| 45 |
gpointer user_data) |
| 46 |
{ |
| 47 |
WorkerState *state = user_data; |
| 48 |
|
| 49 |
g_atomic_int_inc (&total_count); |
| 50 |
state->handled++; |
| 51 |
|
| 52 |
if (!g_atomic_int_get (&in_shutdown)) |
| 53 |
return dex_semaphore_wait (state->semaphore); |
| 54 |
|
| 55 |
return NULL; |
| 56 |
} |
| 57 |
|
| 58 |
static gpointer |
| 59 |
worker_thread_func (gpointer data) |
| 60 |
{ |
| 61 |
WorkerState *state = data; |
| 62 |
GMainContext *main_context = g_main_context_new (); |
| 63 |
DexMainScheduler *main_scheduler = dex_main_scheduler_new (main_context); |
| 64 |
DexFuture *future; |
| 65 |
|
| 66 |
future = dex_semaphore_wait (state->semaphore); |
| 67 |
future = dex_future_then_loop (future, worker_thread_callback, state, NULL); |
| 68 |
|
| 69 |
while (!g_atomic_int_get (&in_shutdown)) |
| 70 |
g_main_context_iteration (main_context, TRUE); |
| 71 |
|
| 72 |
dex_unref (future); |
| 73 |
|
| 74 |
while (g_main_context_pending (main_context)) |
| 75 |
g_main_context_iteration (main_context, FALSE); |
| 76 |
|
| 77 |
dex_unref (main_scheduler); |
| 78 |
g_main_context_unref (main_context); |
| 79 |
|
| 80 |
g_atomic_int_inc (&done); |
| 81 |
|
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
static void |
| 86 |
test_semaphore_threaded (void) |
| 87 |
{ |
| 88 |
DexSemaphore *semaphore = dex_semaphore_new (); |
| 89 |
WorkerState state[N_THREADS] = {{0}}; |
| 90 |
|
| 91 |
for (guint i = 0; i < G_N_ELEMENTS (state); i++) |
| 92 |
{ |
| 93 |
char *name = g_strdup_printf ("test-semaphore-%u", i); |
| 94 |
|
| 95 |
state[i].semaphore = semaphore; |
| 96 |
state[i].threadid = i; |
| 97 |
state[i].thread = g_thread_new (name, worker_thread_func, &state[i]); |
| 98 |
|
| 99 |
g_free (name); |
| 100 |
} |
| 101 |
|
| 102 |
g_usleep (G_USEC_PER_SEC/2); |
| 103 |
|
| 104 |
for (guint i = 0; i < 3; i++) |
| 105 |
{ |
| 106 |
int count = 10000; |
| 107 |
g_atomic_int_set (&total_count, 0); |
| 108 |
dex_semaphore_post_many (semaphore, count); |
| 109 |
g_usleep (G_USEC_PER_SEC); |
| 110 |
g_test_message ("Expected %u, got %u", count, g_atomic_int_get (&total_count)); |
| 111 |
g_assert_cmpint (g_atomic_int_get (&total_count), ==, count); |
| 112 |
} |
| 113 |
|
| 114 |
g_atomic_int_set (&in_shutdown, TRUE); |
| 115 |
|
| 116 |
while (g_atomic_int_get (&done) < N_THREADS) |
| 117 |
dex_semaphore_post (semaphore); |
| 118 |
|
| 119 |
dex_semaphore_close (semaphore); |
| 120 |
|
| 121 |
for (guint i = 0; i < G_N_ELEMENTS (state); i++) |
| 122 |
{ |
| 123 |
g_thread_join (state[i].thread); |
| 124 |
|
| 125 |
g_test_message ("Thread %d handled %d items", i, state[i].handled); |
| 126 |
} |
| 127 |
|
| 128 |
dex_unref (semaphore); |
| 129 |
} |
| 130 |
|
| 131 |
int |
| 132 |
main (int argc, |
| 133 |
char *argv[]) |
| 134 |
{ |
| 135 |
dex_init (); |
| 136 |
g_test_init (&argc, &argv, NULL); |
| 137 |
g_test_add_func ("/Dex/TestSuite/Semaphore/threaded", test_semaphore_threaded); |
| 138 |
return g_test_run (); |
| 139 |
} |