Debian Codegraph @

tests/API/SEAP/test_api_seap_concurency.c

raw symbols SCIP
1 /*
2 * Copyright 2008 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This 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 * This 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 this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * "Daniel Kopecek" <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <sexp.h>
29 #include <sys/time.h>
30 #include <pthread.h>
31 #include <stdlib.h>
32
33 #ifndef TEST_THREAD_COUNT_MAX
34 #define TEST_THREAD_COUNT_MAX 8
35 #endif
36
37 #ifndef TEST_REF_COUNT
38 #define TEST_REF_COUNT 32767
39 #endif
40
41 void *worker_thread (void *arg);
42
43 #define __PRINT_TIME(ident, t0, t1, tz, code) \
44 do { \
45 if (gettimeofday (&(t0), &(tz)) != 0) abort(); \
46 code \
47 if (gettimeofday (&(t1), &(tz)) != 0) abort(); \
48 \
49 flockfile (stdout); \
50 fprintf (stdout, "[%zu][%s] t= %.3g\n", (uintptr_t)(pthread_self ()), ident, \
51 ((double)t1.tv_sec + ((double)t1.tv_usec / 1000000.0)) - \
52 ((double)t0.tv_sec + ((double)t0.tv_usec / 1000000.0))); \
53 funlockfile(stdout); \
54 } while (0)
55
56 void *worker_thread (void *arg)
57 {
58 register int i;
59 struct timezone tz;
60 struct timeval t0, t1;
61
62 tz.tz_minuteswest = 0;
63 tz.tz_dsttime = 0;
64
65 SEXP_t *ref0 = (SEXP_t *)arg;
66 SEXP_t *ref1 = SEXP_string_newf ("I'm a non-shared S-expression!\n");
67
68 SEXP_t *set0[TEST_REF_COUNT];
69 SEXP_t *set1[TEST_REF_COUNT];
70
71 /******************************************/
72 __PRINT_TIME("T1", t0, t1, tz,
73 for (i = 0; i < TEST_REF_COUNT; ++i) {
74 set0[i] = SEXP_ref (ref0);
75 set1[i] = SEXP_ref (ref1);
76 }
77 for (i = 0; i < TEST_REF_COUNT; ++i) {
78 SEXP_free (set0[i]);
79 SEXP_free (set1[i]);
80 }
81 );
82 /******************************************/
83 __PRINT_TIME("T2", t0, t1, tz,
84 for (i = 0; i < TEST_REF_COUNT; ++i) {
85 set1[i] = SEXP_ref (ref1);
86 set0[i] = SEXP_ref (ref0);
87 }
88 for (i = 0; i < TEST_REF_COUNT; ++i) {
89 SEXP_free (set0[i]);
90 SEXP_free (set1[i]);
91 }
92 );
93 /******************************************/
94 __PRINT_TIME("T3", t0, t1, tz,
95 for (i = 0; i < TEST_REF_COUNT; ++i) {
96 set0[i] = SEXP_ref (ref0);
97 }
98 for (i = 0; i < TEST_REF_COUNT; ++i) {
99 SEXP_free (set0[i]);
100 }
101 );
102 /******************************************/
103 __PRINT_TIME("T4", t0, t1, tz,
104 for (i = 0; i < TEST_REF_COUNT; ++i) {
105 set1[i] = SEXP_ref (ref1);
106 }
107 for (i = 0; i < TEST_REF_COUNT; ++i) {
108 SEXP_free (set1[i]);
109 }
110 );
111 /******************************************/
112 SEXP_free (ref1);
113
114 return (ref0);
115 }
116
117 int main (void)
118 {
119 pthread_t th[TEST_THREAD_COUNT_MAX];
120 SEXP_t *s0;
121 register int i, th_cnt;
122
123 s0 = SEXP_string_newf ("I'm a shared S-expression!\n");
124
125 for (th_cnt = 2; th_cnt <= TEST_THREAD_COUNT_MAX; ++th_cnt) {
126 SEXP_t *s0_ref;
127
128 fprintf (stdout, "----- th_cnt= %u -----\n", th_cnt);
129
130 for (i = 0; i < th_cnt; ++i) {
131 s0_ref = SEXP_ref (s0);
132
133 if (pthread_create (&th[i], NULL, worker_thread, (void *)s0_ref) != 0) {
134 abort ();
135 }
136 }
137
138 for (i = 0; i < th_cnt; ++i) {
139 if (pthread_join (th[i], (void **)&s0_ref) != 0) {
140 abort ();
141 }
142
143 SEXP_free (s0_ref);
144 }
145 }
146
147 if (SEXP_refs (s0) != 1) abort ();
148 SEXP_free (s0);
149
150 return (0);
151 }