test-crbug1004254.c revision a4e54154
1/*
2 * fontconfig/test/test-pthread.c
3 *
4 * Copyright © 2000 Keith Packard
5 * Copyright © 2013 Raimund Steger
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of the author(s) not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission.  The authors make no
14 * representations about the suitability of this software for any purpose.  It
15 * is provided "as is" without express or implied warranty.
16 *
17 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
24 */
25#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <pthread.h>
29#include <fontconfig/fontconfig.h>
30
31struct thr_arg_s
32{
33    int thr_num;
34};
35
36static void
37run_query (void)
38{
39    FcPattern *pat = FcPatternCreate (), *match;
40    FcResult result;
41
42    FcPatternAddString (pat, FC_FAMILY, (const FcChar8 *) "sans-serif");
43    FcPatternAddBool (pat, FC_SCALABLE, FcTrue);
44    FcConfigSubstitute (NULL, pat, FcMatchPattern);
45    FcDefaultSubstitute (pat);
46    match = FcFontMatch (NULL, pat, &result);
47    if (result != FcResultMatch || !match)
48    {
49	fprintf (stderr, "ERROR: No matches found\n");
50    }
51    if (match)
52	FcPatternDestroy (match);
53    FcPatternDestroy (pat);
54}
55
56static void
57run_reinit (void)
58{
59    if (!FcInitReinitialize ())
60    {
61	fprintf (stderr, "ERROR: Reinitializing failed\n");
62    }
63}
64
65#define NTEST 3000
66
67static void *
68run_test_in_thread (void *arg)
69{
70    struct thr_arg_s *thr_arg = (struct thr_arg_s *) arg;
71    int thread_num = thr_arg->thr_num;
72
73    fprintf (stderr, "Worker %d: started (round %d)\n", thread_num % 2, thread_num / 2);
74    if ((thread_num % 2) == 0)
75    {
76	run_query ();
77    }
78    else
79    {
80	run_reinit ();
81    }
82    fprintf (stderr, "Worker %d: done (round %d)\n", thread_num % 2, thread_num / 2);
83
84    return NULL;
85}
86
87int
88main (int argc, char **argv)
89{
90    pthread_t threads[NTEST];
91    struct thr_arg_s thr_arg[NTEST];
92    int i, j;
93
94    for (i = 0; i < NTEST; i++)
95    {
96	int result;
97
98	fprintf (stderr, "Thread %d (worker %d round %d): creating\n", i, i % 2, i / 2);
99	thr_arg[i].thr_num = i;
100	result = pthread_create (&threads[i], NULL, run_test_in_thread,
101				 (void *) &thr_arg[i]);
102	if (result != 0)
103	{
104	    fprintf (stderr, "Cannot create thread %d\n", i);
105	    break;
106	}
107    }
108    for (j = 0; j < i; j++)
109    {
110	pthread_join(threads[j], NULL);
111	fprintf (stderr, "Joined thread %d\n", j);
112    }
113    FcFini ();
114
115    return 0;
116}
117