1/* Test that iconv works in a multi-threaded program.
2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but 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 Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19/* This test runs several worker threads that perform the following three
20 steps in staggered synchronization with each other:
21 1. initialization (iconv_open)
22 2. conversion (iconv)
23 3. cleanup (iconv_close)
24 Having several threads synchronously (using pthread_barrier_*) perform
25 these routines exercises iconv code that handles concurrent attempts to
26 initialize and/or read global iconv state (namely configuration). */
27
28#include <iconv.h>
29#include <stdio.h>
30#include <string.h>
31
32#include <support/support.h>
33#include <support/xthread.h>
34#include <support/check.h>
35
36#define TCOUNT 16
37_Static_assert (TCOUNT %2 == 0,
38 "thread count must be even, since we need two groups.");
39
40
41#define CONV_INPUT "Hello, iconv!"
42
43
44pthread_barrier_t sync;
45pthread_barrier_t sync_half_pool;
46
47
48/* Execute iconv_open, iconv and iconv_close in a synchronized way in
49 conjunction with other sibling worker threads. If any step fails, print
50 an error to stdout and return NULL to the main thread to indicate the
51 error. */
52static void *
53worker (void * arg)
54{
55 long int tidx = (long int) arg;
56
57 iconv_t cd;
58
59 char ascii[] = CONV_INPUT;
60 size_t bytes = sizeof (CONV_INPUT) - 1;
61 char *inbufpos = ascii;
62 size_t inbytesleft = bytes;
63
64 char *utf8 = xcalloc (n: bytes, s: 1);
65 char *outbufpos = utf8;
66 size_t outbytesleft = bytes;
67
68 if (tidx < TCOUNT/2)
69 /* The first half of the worker thread pool synchronize together here,
70 then call iconv_open immediately after. */
71 xpthread_barrier_wait (barrier: &sync_half_pool);
72 else
73 /* The second half wait for the first half to finish iconv_open and
74 continue to the next barrier (before the call to iconv below). */
75 xpthread_barrier_wait (barrier: &sync);
76
77 /* The above block of code staggers all subsequent pthread_barrier_wait
78 calls in a way that ensures a high chance of encountering these
79 combinations of concurrent iconv usage:
80 1) concurrent calls to iconv_open,
81 2) concurrent calls to iconv_open *and* iconv,
82 3) concurrent calls to iconv,
83 4) concurrent calls to iconv *and* iconv_close,
84 5) concurrent calls to iconv_close. */
85
86 cd = iconv_open (tocode: "UTF8", fromcode: "ASCII");
87 TEST_VERIFY_EXIT (cd != (iconv_t) -1);
88
89 xpthread_barrier_wait (barrier: &sync);
90
91 TEST_VERIFY_EXIT (iconv (cd, &inbufpos, &inbytesleft, &outbufpos,
92 &outbytesleft)
93 != (size_t) -1);
94
95 xpthread_barrier_wait (barrier: &sync);
96
97 TEST_VERIFY_EXIT (iconv_close (cd) == 0);
98
99 /* The next conditional barrier wait is needed because we staggered the
100 threads into two groups in the beginning and at this point, the second
101 half of worker threads are waiting for the first half to finish
102 iconv_close before they can executing the same: */
103 if (tidx < TCOUNT/2)
104 xpthread_barrier_wait (barrier: &sync);
105
106 TEST_COMPARE_BLOB (utf8, bytes, CONV_INPUT, bytes);
107
108 pthread_exit (NULL);
109}
110
111
112static int
113do_test (void)
114{
115 pthread_t thread[TCOUNT];
116 void * worker_output;
117 int i;
118
119 xpthread_barrier_init (barrier: &sync, NULL, TCOUNT);
120 xpthread_barrier_init (barrier: &sync_half_pool, NULL, TCOUNT/2);
121
122 for (i = 0; i < TCOUNT; i++)
123 thread[i] = xpthread_create (NULL, thread_func: worker, closure: (void *) (long int) i);
124
125 for (i = 0; i < TCOUNT; i++)
126 {
127 worker_output = xpthread_join (thr: thread[i]);
128 TEST_VERIFY_EXIT (worker_output == NULL);
129 }
130
131 xpthread_barrier_destroy (barrier: &sync);
132 xpthread_barrier_destroy (barrier: &sync_half_pool);
133
134 return 0;
135}
136
137#include <support/test-driver.c>
138

source code of glibc/iconv/tst-iconv-mt.c