summaryrefslogtreecommitdiff
path: root/PACE/tests/Cond_Var_Test.c
blob: c2f46c639f57ca59d5d8e1fb7c19e19fc507d366 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* $Id$ -*- C -*- */

/* ===================================================================== */
/*                                                                       */
/* = FILENAME                                                            */
/*    Cond_Var_Test.c                                                    */
/*                                                                       */
/* =  DESCRIPTION                                                        */
/*     Testing the platform for POSIX condition variables. 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 simply creates some threads, waits on a condition    */
/*     variable, joins the threads, and then exits.                      */
/*                                                                       */
/* = AUTHOR                                                              */
/*    Joe Hoffert <joeh@cs.wustl.edu>                                    */
/*                                                                       */
/* ===================================================================== */

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

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

#define NUM_THREADS  3
#define TCOUNT 10
#define COUNT_THRES 12

int count = 0;
int thread_ids[3] = {0,1,2};
#if defined (PACE_VXWORKS) && PACE_VXWORKS != 0
/* VxWorks does not handle the *_ININITALIZER values and we
 * can not emulate it since it involves memory allocation for
 * VxWorks.
 */
pace_pthread_mutex_t count_lock;
pace_pthread_cond_t count_hit_threshold;
#else
pace_pthread_mutex_t count_lock = PACE_PTHREAD_MUTEX_INITIALIZER;
pace_pthread_cond_t count_hit_threshold = PACE_PTHREAD_COND_INITIALIZER;
#endif

void *inc_count(void *idp)
{
  int i = 0;
  int *my_id = idp;
  pace_pthread_t pthread;

  if ((pthread = pace_pthread_self()) == NULL)
    {
      printf("inc_count, pace_pthread_self - returning NULL.\n");
    }
  else
    {
      printf("inc_count, pace_pthread_self -> %ld.\n", (long)pthread);
    }

  printf("....Before pace_sleep.\n");
  pace_sleep(1);
  printf("....After pace_sleep.\n");

  for (i = 0; i < TCOUNT; i++) {
    pace_pthread_mutex_lock(&count_lock);
    count++;
    printf("inc_counter(): thread %d, count = %d, unlocking mutex\n", 
	   *my_id, count);
    if (count == COUNT_THRES) {
      printf("inc_count(): Thread %d, count %d\n", *my_id, count);
      pace_pthread_cond_signal(&count_hit_threshold);
    }
    pace_pthread_mutex_unlock(&count_lock);
  }
  
  return(NULL);
}

void *watch_count(void *idp)
{
  int *my_id = idp;
  pace_pthread_t pthread;

  if ((pthread = pace_pthread_self()) == NULL)
    {
      printf("watch_count, pace_pthread_self - returning NULL.\n");
    }
  else
    {
      printf("watch_count, pace_pthread_self -> %ld.\n", (long)pthread);
    }

  printf("watch_count(): thread %d\n", *my_id);

  pace_pthread_mutex_lock(&count_lock);

  while (count < COUNT_THRES) {
    pace_pthread_cond_wait(&count_hit_threshold, &count_lock);
    /*printf("watch_count(): thread %d, count %d\n", *my_id, count);*/
  }

  pace_pthread_mutex_unlock(&count_lock);

  return(NULL);
}

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

#if defined (PACE_VXWORKS) && PACE_VXWORKS != 0
  /* VxWorks does not handle the *_ININITALIZER values and we
   * can not emulate it since it involves memory allocation for
   * VxWorks.
   */
  pace_pthread_mutex_init(&count_lock, 0);
  pace_pthread_cond_init(&count_hit_threshold, 0);
#endif

  printf("argc = %d.\n", argc);
  for (i = 0; i < argc; ++i)
    {
      printf("argv[%d] = %s.\n", i, argv[i]);
    }

  if ((pthread = pace_pthread_self()) == NULL)
    {
      printf("main, pace_pthread_self - returning NULL.\n");
    }
  else
    {
      printf("main, pace_pthread_self -> %ld.\n", (long)pthread);
    }

  printf("....Before pace_sleep.\n");
  pace_sleep(1);
  printf("....After pace_sleep.\n");

  pace_pthread_create(&threads[0], NULL, inc_count, (void *)&thread_ids[0]);
  pace_pthread_create(&threads[1], NULL, inc_count, (void *)&thread_ids[1]);
  pace_pthread_create(&threads[2], NULL, watch_count, (void *)&thread_ids[2]);

  for (i = 0; i < NUM_THREADS; i++) {
    pace_pthread_join(threads[i], NULL);
  }

  printf("main(): all finished\n");

#if defined (PACE_VXWORKS) && PACE_VXWORKS != 0
/* VxWorks does not handle the *_ININITALIZER values and we
 * can not emulate it since it involves memory allocation for
 * VxWorks. We need to explicitly delete the mutex and condition
 * variable allocated by the init calls.
 */
pace_pthread_mutex_destroy(&count_lock);
pace_pthread_cond_destroy(&count_hit_threshold);
#endif /* PACE_VXWORKS) && PACE_VXWORKS != 0 */

  return 0;
}