summaryrefslogtreecommitdiff
path: root/PACE/tests/Pthread_Storage_Test.c
blob: 726b3835c1d58b1ed063489421a4e36e320ab8c8 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* $Id$ -*- C -*- */

/* ===================================================================== */
/*                                                                       */
/* = FILENAME                                                            */
/*    Pthreads_Test.c                                                    */
/*                                                                       */
/* =  DESCRIPTION                                                        */
/*     Testing the platform for POSIX threads thread specific storage.   */
/*     This is not meant to be an exhaustive test at this point but more */
/*     a sanity check that PACE works (at least somewhat) as advertised. */
/*     This program creates some threads, stores some thread specific    */
/*     information in each one (some time information) and then checks   */
/*     that the different threads contained different values.            */
/*                                                                       */
/*     This test is largely taken from the O'Reilly _Pthreads            */
/*     Programming_ book and accompanying example code.                  */
/*                                                                       */
/* = AUTHOR                                                              */
/*    Joe Hoffert <joeh@cs.wustl.edu>                                    */
/*                                                                       */
/* ===================================================================== */

#include "pace/stdio.h"
#include "pace/stdlib.h"
#include "pace/time.h"
#include "pace/pthread.h"

#if defined (PACE_VXWORKS) && PACE_VXWORKS != 0
#include "vxworks_stub.c"
#endif /* VXWORKS */

#define NUM_THREADS  3
/*pace_pthread_key_t saved_time_key;*/
pace_pthread_key_t saved_ID_key;

/*void free_time (void *arg)*/
void free_ID (void *arg)
{
  /*pace_timespec *timep = (pace_timespec *)arg; 
  pace_printf("free_time:\n");
  pace_free(timep);*/

  pace_pthread_t *threadp = (pace_pthread_t *)arg; 
  /* pace_printf("free_ID:\n"); */
  pace_free(threadp);
}

/*void save_the_time (void)*/
long save_the_ID (void)
{
  /*pace_timespec *timep;

  timep = (pace_timespec *)malloc(sizeof(pace_timespec));
  clock_gettime(1, timep);
  pace_printf("save_the_time: \t\t%ld %ld\n",timep->tv_sec, timep->tv_nsec);
  pace_pthread_setspecific(saved_time_key, (void *)timep);*/

  pace_pthread_t *pace_thread =
    (pace_pthread_t *)malloc(sizeof(pace_pthread_t));

  *pace_thread = pace_pthread_self();
  /* pace_printf("save_the_ID: \t\t%ld\n", (long)*pace_thread); */
  pace_pthread_setspecific(saved_ID_key, (void *)pace_thread);

  return ((long)*pace_thread);
}

/*void what_time_did_i_save (void)*/
long what_ID_did_i_save (void)
{
  /*pace_timespec *timep;

  timep = pace_pthread_getspecific(saved_time_key);
  printf("what_time_did_i_save: \t%ld %ld\n",timep->tv_sec, timep->tv_nsec);*/

  pace_pthread_t *pace_thread;

  pace_thread = pace_pthread_getspecific(saved_ID_key);
  /* pace_printf("what_ID_did_i_save: \t%ld\n", (long)*pace_thread); */
  return ((long)*pace_thread);
}  

void *thread_routine (void *arg)
{
  long saved_id;
  long retrieved_id;
  /*int *my_id=(int *)arg;*/
  PACE_UNUSED_ARG (arg);

  /* pace_printf("thread_routine %d\n", *my_id); */
  /*save_the_time();
    what_time_did_i_save();*/
  saved_id = save_the_ID();
  pace_sleep (1);
  retrieved_id = what_ID_did_i_save();

  if (saved_id != retrieved_id)
    {
      pace_printf ("### ERROR ###: saved id %ld does not equal retrieved
                   id %ld.\n", saved_id, retrieved_id);
      pace_exit (-1);
    }

  return (NULL); 
}

int
main (int argc, char *argv[])
{
  int i;
  int *id_arg;
  pace_pthread_t threads[NUM_THREADS];

  PACE_UNUSED_ARG (argc);
  PACE_UNUSED_ARG (argv);

  id_arg = (int *) pace_malloc (NUM_THREADS * sizeof(int));

  /* pace_printf("main : initializing the key\n"); */
  /*pace_pthread_key_create(&saved_time_key, free_time);*/
  pace_pthread_key_create(&saved_ID_key, free_ID);

  /* pace_printf("main : spawning the threads\n"); */
  for (i = 0; i < NUM_THREADS; i++)
    {
      id_arg[i] = i;

      pace_pthread_create(&(threads[i]), 
                          NULL,
                          thread_routine,
                          (void *) &(id_arg[i]));
    }

  for (i = 0; i < NUM_THREADS; i++)
    {
      pace_pthread_join(threads[i], NULL);
      /* pace_printf("main : thread %d has finished. \n", i); */
    }

  /* pace_printf("main : goodbye\n"); */

  return 0;
}