summaryrefslogtreecommitdiffstats
path: root/testsuites/psxtests/psxcleanup/psxcleanup.c
blob: 2ab4e384f406384f104acc73c1f41598fd69d6d1 (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 *
 *  This is a simple real-time applications XXX.
 *
 *  Other POSIX facilities such as XXX, condition, .. is also used
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#define CONFIGURE_INIT
#include "system.h"
#include <pthread.h>  /* thread facilities */
#include <signal.h>   /* signal facilities */
#include <unistd.h>   /* sleep facilities */
#include <sched.h>    /* schedule facilities */
#include <time.h>     /* time facilities */
#include <stdio.h>    /* console facilities */
#include "tmacros.h"

#define NUMBER_THREADS 2
pthread_t ThreadIds[NUMBER_THREADS];

typedef struct {
  pthread_mutex_t lock;
  pthread_cond_t rcond;
  pthread_cond_t wcond;
  int lock_count; /* < 0 .. Held by writer. */
                  /* > 0 .. Held by lock_count readers. */
                  /* = 0 .. Held by nobody. */
  int waiting_writers; /* Count of waiting writers. */
} lock_t;

volatile bool reader_cleanup_ran;
volatile bool release_read_lock_ran;
volatile bool writer_cleanup_ran;

void waiting_reader_cleanup(void *arg);
void lock_for_read(void *arg);
void release_read_lock(void *arg);
void waiting_writer_cleanup(void *arg);
void lock_for_write(lock_t *l);
void release_write_lock(void *arg);
void initialize_lock_t(lock_t *l);
void *ReaderThread(void *arg);
void *WriterThread(void *arg);

void waiting_reader_cleanup(void *arg)
{
  lock_t *l;

  reader_cleanup_ran = TRUE;

  l = (lock_t *) arg;
  pthread_mutex_unlock(&l->lock);
}

void lock_for_read(void *arg)
{
  lock_t *l = arg;

  pthread_mutex_lock(&l->lock);
  pthread_cleanup_push(waiting_reader_cleanup, l);
  while ((l->lock_count < 0) && (l->waiting_writers != 0))
    pthread_cond_wait(&l->rcond, &l->lock);
  l->lock_count++;
  reader_cleanup_ran = FALSE;

  /*
   * Note the pthread_cleanup_pop executes
   * waiting_reader_cleanup.
   */
  pthread_cleanup_pop(1);

  if ( reader_cleanup_ran == FALSE ) {
    puts( "reader cleanup did not run" );
    rtems_test_exit(0);
  }
}

void release_read_lock(void *arg)
{
  lock_t *l = arg;

  release_read_lock_ran = TRUE;
  pthread_mutex_lock(&l->lock);
  if (--l->lock_count == 0)
    pthread_cond_signal(&l->wcond);
  pthread_mutex_unlock(&l->lock);
}

void waiting_writer_cleanup(void *arg)
{
  lock_t *l = arg;

  writer_cleanup_ran = TRUE;

  if ((--l->waiting_writers == 0) && (l->lock_count >= 0)) {
    /*
     * This only happens if we have been canceled.
     */
    pthread_cond_broadcast(&l->wcond);
  }
    pthread_mutex_unlock(&l->lock);
}

void lock_for_write(lock_t *l)
{
  pthread_mutex_lock(&l->lock);
  l->waiting_writers++;
  l->lock_count = -1;

  pthread_cleanup_push(waiting_writer_cleanup, l);

  while (l->lock_count != 0)
      pthread_cond_wait(&l->wcond, &l->lock);
  l->lock_count = -1;

  /*
   * Note the pthread_cleanup_pop executes
   * waiting_writer_cleanup.
   */
  writer_cleanup_ran = FALSE;
  pthread_cleanup_pop(1);

  if ( writer_cleanup_ran == FALSE ) {
    puts( "writer cleanup did not run" );
    rtems_test_exit(0);
  }
}

void release_write_lock(void *arg)
{
  lock_t *l = arg;

  writer_cleanup_ran = TRUE;

  /* pthread_mutex_lock(&l->lock); */
  l->lock_count = 0;
  if (l->waiting_writers == 0)
      pthread_cond_broadcast(&l->rcond);
  else
      pthread_cond_signal(&l->wcond);
  /* pthread_mutex_unlock(&l->lock); */
}


/*
 * This function is called to initialize the read/write lock.
 */
void initialize_lock_t(lock_t *l)
{
  pthread_mutexattr_t mutexattr;    /* mutex attributes */
  pthread_condattr_t  condattr;     /* condition attributes */

  if (pthread_mutexattr_init (&mutexattr) != 0) {
    perror ("Error in mutex attribute init\n");
  }
  if (pthread_mutex_init (&l->lock,&mutexattr) != 0) {
    perror ("Error in mutex init");
  }

  if (pthread_condattr_init (&condattr) != 0) {
     perror ("Error in condition attribute init\n");
  }
  if (pthread_cond_init (&l->wcond,&condattr) != 0) {
    perror ("Error in write condition init");
  }
  if (pthread_cond_init (&l->rcond,&condattr) != 0) {
    perror ("Error in read condition init");
  }

  l->lock_count = 0;
  l->waiting_writers = 0;
}

void *ReaderThread(void *arg)
{
  lock_t *l = arg;

  puts("Lock for read");
  lock_for_read(l);
  puts("cleanup push for read");
  pthread_cleanup_push(release_read_lock, &l->lock);

  /* Thread has read lock. */
  release_read_lock_ran = FALSE;
  puts("cleanup pop for read");
  pthread_cleanup_pop(1);

  if ( release_read_lock_ran == FALSE ) {
    puts( "release read lock did not run" );
    rtems_test_exit(0);
  }
  return NULL;
}

void *WriterThread(void *arg)
{
  lock_t *l = arg;

  puts("Lock for write");
  lock_for_write(l);
  puts("cleanup push for write");
  pthread_cleanup_push(release_write_lock, &l->lock);

  /* Thread has write lock. */
  release_write_lock(&l->lock);

  /* do nothing */
  puts("do nothing cleanup pop for write");
  pthread_cleanup_pop(0);
  return NULL;
}

/*
 *  main entry point to the test
 */

void *POSIX_Init(
  void *argument
)
{
  pthread_attr_t attr;              /* task attributes */
  int            status;
  lock_t         l;

  puts( "\n\n*** POSIX CLEANUP TEST ***" );

  /*************** INITIALIZE  ***************/
  initialize_lock_t(&l);
  if (pthread_attr_init(&attr) != 0) {
    perror ("Error in attribute init\n");
  }

  /*************** CREATE THREADS  ***************/

  status = pthread_create(&ThreadIds[0], NULL, ReaderThread, &l);
  posix_service_failed( status, "pthread_create Reader" );

  sleep(1);

  status = pthread_create(&ThreadIds[1], NULL, WriterThread, &l);
  posix_service_failed( status, "pthread_create Writer" );

  sleep(1);

  /*************** END OF TEST *****************/
  puts( "*** END OF POSIX CLEANUP TEST ***\n" );
  rtems_test_exit(0);
}