1/* Copyright (c) 2006-2008 MySQL AB, 2009 Sun Microsystems, Inc.
2 Use is subject to license terms.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
17#include <my_global.h>
18#include <my_sys.h>
19#include <my_atomic.h>
20#include <tap.h>
21
22volatile uint32 bad;
23pthread_attr_t thr_attr;
24pthread_mutex_t mutex;
25pthread_cond_t cond;
26uint running_threads;
27
28void do_tests();
29
30void test_concurrently(const char *test, pthread_handler handler, int n, int m)
31{
32 pthread_t t;
33 ulonglong now= my_interval_timer();
34
35 bad= 0;
36
37 diag("Testing %s with %d threads, %d iterations... ", test, n, m);
38 for (running_threads= n ; n ; n--)
39 {
40 if (pthread_create(&t, &thr_attr, handler, &m) != 0)
41 {
42 diag("Could not create thread");
43 abort();
44 }
45 }
46 pthread_mutex_lock(&mutex);
47 while (running_threads)
48 pthread_cond_wait(&cond, &mutex);
49 pthread_mutex_unlock(&mutex);
50
51 now= my_interval_timer() - now;
52 ok(!bad, "tested %s in %g secs (%d)", test, ((double)now)/1e9, bad);
53}
54
55int main(int argc __attribute__((unused)), char **argv)
56{
57 MY_INIT(argv[0]);
58
59 if (argv[1] && *argv[1])
60 DBUG_SET_INITIAL(argv[1]);
61
62 pthread_mutex_init(&mutex, 0);
63 pthread_cond_init(&cond, 0);
64 pthread_attr_init(&thr_attr);
65 pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED);
66
67#define CYCLES 3000
68#define THREADS 30
69
70 diag("N CPUs: %d", my_getncpus());
71
72 do_tests();
73
74 /*
75 workaround until we know why it crashes randomly on some machine
76 (BUG#22320).
77 */
78#ifdef NOT_USED
79 sleep(2);
80#endif
81 pthread_mutex_destroy(&mutex);
82 pthread_cond_destroy(&cond);
83 pthread_attr_destroy(&thr_attr);
84 my_end(0);
85 return exit_status();
86}
87
88