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