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 
22 volatile uint32 bad;
23 pthread_attr_t thr_attr;
24 pthread_mutex_t mutex;
25 pthread_cond_t cond;
26 uint running_threads;
27 
28 void do_tests();
29 
test_concurrently(const char * test,pthread_handler handler,int n,int m)30 void test_concurrently(const char *test, pthread_handler handler, int n, int m)
31 {
32   pthread_t t;
33   ulonglong now= my_getsystime();
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_getsystime()-now;
52   ok(!bad, "tested %s in %g secs (%d)", test, ((double)now)/1e7, bad);
53 }
54 
main(int argc,char ** argv)55 int main(int argc __attribute__((unused)), char **argv)
56 {
57   MY_INIT("thd_template");
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 #ifdef MY_ATOMIC_MODE_RWLOCKS
68 #if defined(HPUX11) || defined(__POWERPC__) /* showed to be very slow (scheduler-related) */
69 #define CYCLES 300
70 #else
71 #define CYCLES 3000
72 #endif
73 #else
74 #define CYCLES 3000
75 #endif
76 #define THREADS 30
77 
78   diag("N CPUs: %d, atomic ops: %s", my_getncpus(), MY_ATOMIC_MODE);
79 
80   do_tests();
81 
82   /*
83     workaround until we know why it crashes randomly on some machine
84     (BUG#22320).
85   */
86   sleep(2);
87   pthread_mutex_destroy(&mutex);
88   pthread_cond_destroy(&cond);
89   pthread_attr_destroy(&thr_attr);
90   my_end(0);
91   return exit_status();
92 }
93 
94