1 /* This testcase is part of GDB, the GNU debugger.
2 
3    Copyright 2009-2013 Free Software Foundation, Inc.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program 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
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 
18    Contributed by Markus Deuling <deuling@de.ibm.com>  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <libspe2.h>
23 #include <pthread.h>
24 #include <sys/wait.h>
25 
26 extern spe_program_handle_t bt_spu;
27 extern spe_program_handle_t bt2_spu;
28 #define nr_t 5
29 
30 void *
spe_thread(void * arg)31 spe_thread (void * arg)
32 {
33   int flags = 0;
34   unsigned int entry = SPE_DEFAULT_ENTRY;
35   spe_context_ptr_t *ctx = (spe_context_ptr_t *) arg;
36 
37   spe_program_load (*ctx, &bt_spu);
38   spe_context_run (*ctx, &entry, flags, NULL, NULL, NULL);
39 
40   pthread_exit (NULL);
41 }
42 
43 int
indirect_handler(unsigned char * base,unsigned long offset)44 indirect_handler (unsigned char *base, unsigned long offset)
45 {
46   int flags = 0;
47   unsigned int entry = SPE_DEFAULT_ENTRY;
48   spe_context_ptr_t ctx = spe_context_create (0, NULL);
49 
50   spe_program_load (ctx, &bt2_spu);
51   spe_context_run (ctx, &entry, flags, NULL, NULL, NULL);
52 
53   return 0;
54 }
55 
56 static pthread_mutex_t crash_mutex = PTHREAD_MUTEX_INITIALIZER;
57 static int crash_count = 0;
58 
59 int
crash_handler(unsigned char * base,unsigned long offset)60 crash_handler (unsigned char *base, unsigned long offset)
61 {
62   int count;
63 
64   pthread_mutex_lock (&crash_mutex);
65   count = ++crash_count;
66   pthread_mutex_unlock (&crash_mutex);
67 
68   while (count < nr_t)
69     ;
70 
71   abort ();
72 }
73 
74 int
main(void)75 main (void)
76 {
77   int thread_id[nr_t];
78   pthread_t pts[nr_t];
79   spe_context_ptr_t ctx[nr_t];
80   int value = 1;
81   int cnt;
82 
83   spe_callback_handler_register (indirect_handler, 0x11, SPE_CALLBACK_NEW);
84   spe_callback_handler_register (crash_handler, 0x12, SPE_CALLBACK_NEW);
85 
86   for (cnt = 0; cnt < nr_t; cnt++)
87     {
88       ctx[cnt] = spe_context_create (0, NULL);
89       thread_id[cnt]
90 	= pthread_create (&pts[cnt], NULL, &spe_thread, &ctx[cnt]);
91     }
92 
93   for (cnt = 0; cnt < nr_t; cnt++)
94     pthread_join (pts[cnt], NULL);
95 
96   for (cnt = 0; cnt < nr_t; cnt++)
97     spe_context_destroy (ctx[cnt]);
98 
99   return 0;
100 }
101 
102