aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.hp/thread-local-in-lib.lib.c
blob: 0fe03b35e52c4f22c84c28038cee156fee62f505 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>

/* Library code for thread local in lib test.
*/
#include "thread-local-in-lib.h"

extern pthread_mutex_t mutex;
extern int get_number();
extern int total;
extern int the_code_saw[NTHREADS][ELEMS_PER_THREAD];

/* The debugger should see this without a declaration.
 *
 * extern int debugger_saw[NTHREADS][ELEMS_PER_THREAD]; 
 */

/* The actual thread locals.
 */
__thread int sum;
__thread int x[ ELEMS_PER_THREAD ]; /* [3] */

void sumup()
{
   int j;

   sum = 0;
   for (j = 0; j < ELEMS_PER_THREAD; j++) {
      sum += x[j];
   }

   if( sum == x[0] )
       /* It won't be "==", but this lets us set a breakpoint
        * and look at the thread-local storage.
        */
       sum++;

   x[0] = x[2];  /* Another no-op for debugger use */
}

void *adder( vid )
   void * vid;
{
   int id;
   int i, j;
   int ret;

   id = (int) vid;
   
   /* printf( "== Thread: Welcome to adder %d\n", id ); */

   for (j = 0; j < ELEMS_PER_THREAD; j++) {
      x[j] = 0;
   }

   for (i = id, j = 0; i < NUM_ELEMS; i += NTHREADS, j++ ) {

   /*   printf( "== Thread: id %d, i %d, j %d\n", id, i, j );
      fflush( stdout ); */
    
      x[j] = get_number(i); /* {0,1,2,3} +0, +4, +8 */

      /* Record for posterity; the debugger will gather
       * the same data here, using "x[j]".
       */
      the_code_saw[ id ][ j ] = x[j];   

   /*   printf( "== Thread %d, sample %d, val %d, i %d\n", id, j, x[j],i );
      fflush( stdout ); */
   }

   sumup();
   /* printf("== Thread: adder %d contributes total %d\n", id, sum); */

   /* protect global data */
   ret = pthread_mutex_lock(&mutex);
   if (ret != 0) {
      printf("== Thread: pthread_mutex_lock() error: %d\n", ret);
      exit(1);
   }

   total += sum;

   ret = pthread_mutex_unlock(&mutex);
   if (ret != 0) {
      printf("== Thread: pthread_mutex_unlock() error: %d\n", ret);
      exit(1);
   }

   if( NTHREADS != 4 || ELEMS_PER_THREAD != 3 || NUM_ELEMS != 12 ) {
      printf( "** ERROR in test code **\n" );
   }
}