summaryrefslogtreecommitdiffstats
path: root/testsuites/psxtests/psxtimer01/psxtimer.c
blob: 560dc2eebdbb6dc653419faf313f613b0674ed90 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* SPDX-License-Identifier: BSD-2-Clause */

/*
 *
 *  This is a simple real-time applications which contains 3 periodic tasks.
 *
 *  Task A is an independent task.
 *
 *  Task B and C share a data.
 *
 *  Tasks are implemented as POSIX threads.
 *
 *  The share data is protected with a POSIX mutex.
 *
 *  Other POSIX facilities such as timers, condition, .. is also used
 *
 *  COPYRIGHT (c) 1989-2009.
 *  On-Line Applications Research Corporation (OAR).
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#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 <rtems/posix/timerimpl.h>
#include <rtems/score/timespec.h>
#include "pritime.h"

const char rtems_test_name[] = "PSXTIMER 1";

void StopTimer(
  timer_t  timer_id,
  struct   itimerspec *timerdata
);

/* temporal parameters of a task */

struct periodic_params {
   struct timespec period;
   int count;       /* Number of iterations to run */
   int signo;       /* signal number */
   int id;          /* task identification */
 };

pthread_attr_t attr;

/* shared datum */

struct shared_data {
   pthread_mutex_t mutex;
   pthread_cond_t  sync;
   int updated;
 };

struct shared_data data;

void StopTimer(
  timer_t  timer_id,
  struct   itimerspec *timerdata
)
{
  static int         firstTime = 1;
  struct itimerspec *pOld;
  struct itimerspec  odata;

  /*
   *  We do not care about the old value.  But we need to exercise
   *  getting and not getting the return value back.
   */
  pOld = (firstTime == 1) ? NULL : &odata;
  firstTime = 0;

  timerdata->it_value.tv_sec  = 0;
  timerdata->it_value.tv_nsec  = 0;
  if (timer_settime(timer_id,POSIX_TIMER_RELATIVE,timerdata,pOld) == -1) {
    perror ("Error in timer setting\n");
    rtems_test_exit(0);
  }
}

/* task A  */
void * task_a (void *arg)
{
   struct   timespec my_period;
   int      my_sig, received_sig;
   struct   itimerspec timerdata;
   struct   itimerspec timergetdata;
   timer_t  timer_id;
   time_t   clock;
   struct   sigevent event;
   sigset_t set;
   struct periodic_params *params;

   params = arg;
   my_period = params->period;
   my_sig    = params->signo;

   /* timer create */
   event.sigev_notify = SIGEV_SIGNAL;
   event.sigev_signo = my_sig;
   if (timer_create (CLOCK_REALTIME, &event, &timer_id) == -1) {
      perror ("Error in timer creation\n");
      rtems_test_exit(0);
    }

   /* block the timer signal */
   sigemptyset (&set);
   sigaddset (&set,my_sig);
   pthread_sigmask(SIG_BLOCK,&set,NULL);

   /* set the timer in periodic mode */
   timerdata.it_interval = my_period;
   timerdata.it_value    = my_period;
   timerdata.it_value.tv_sec *= 2;
   if (timer_settime(timer_id,POSIX_TIMER_RELATIVE,&timerdata,&timergetdata) == -1) {
     perror ("Error in timer setting\n");
     rtems_test_exit(0);
   }
   printf(
    "task A: timer_settime - value=%" PRIdtime_t ":%ld interval=%" PRIdtime_t ":%ld\n",
    timergetdata.it_value.tv_sec, timergetdata.it_value.tv_nsec,
    timergetdata.it_interval.tv_sec, timergetdata.it_interval.tv_nsec
  );


   /* periodic activity */
   while(1) {
     if (sigwait(&set,&received_sig) == -1) {
       perror ("Error in sigwait\n");
     }
     if (timer_gettime(timer_id, &timerdata) == -1) {
       perror ("Error in timer_gettime\n");
       rtems_test_exit(0);
     }
     if (! _Timespec_Equal_to( &timerdata.it_value, &my_period )){
       fprintf(
           stderr, "Error in Task A timer_gettime:\n"
           "  re-armed timer: %" PRIdtime_t ":%ld does not match interval: %" PRIdtime_t ":%ld\n",
           timerdata.it_value.tv_sec, timerdata.it_value.tv_nsec,
           my_period.tv_sec, my_period.tv_nsec
      );
     }
     clock = time(NULL);
     printf("Executing task A with count = %2i %s", params->count, ctime(&clock));
     params->count--;
     if (params->count == 0)
       StopTimer(timer_id, &timerdata);
   }
   return NULL;
}

/* task B  */

void * task_b (void *arg)
{
   struct   timespec my_period;
   struct   timespec now;
   int      my_sig, received_sig;
   struct   itimerspec timerdata;
   timer_t  timer_id;
   time_t   clock;
   struct   sigevent event;
   sigset_t set;
   struct periodic_params *params;

   params = arg;
   my_period = params->period;
   my_sig    = params->signo;


   /* timer create */
   event.sigev_notify = SIGEV_SIGNAL;
   event.sigev_signo = my_sig;
   if (timer_create (CLOCK_REALTIME, &event, &timer_id) == -1) {
      perror ("Error in timer creation\n");
      rtems_test_exit(0);
    }

   /* block the timer signal */
   sigemptyset (&set);
   sigaddset (&set,my_sig);
   pthread_sigmask(SIG_BLOCK,&set,NULL);

   /* set the timer in periodic mode */
   clock_gettime( CLOCK_REALTIME, &now );
   timerdata.it_interval = my_period;
   timerdata.it_value = now;
   _Timespec_Add_to( &timerdata.it_value, &my_period );
   if (timer_settime(timer_id,TIMER_ABSTIME,&timerdata,NULL) == -1) {
     perror ("Error in timer setting\n");
     rtems_test_exit(0);
   }

   /* periodic activity */
   while(1) {
     if (sigwait(&set,&received_sig) == -1) {
       perror ("Error in sigwait\n");
       rtems_test_exit(0);
     }

     if (timer_gettime(timer_id, &timerdata) == -1) {
       perror ("Error in timer_gettime\n");
       rtems_test_exit(0);
     }

#if 0
     /*
      *  It is not an error if they are not equal.  A clock tick could occur
      *  and thus they are close but not equal.  Can we test for this?
      */
     if ( !_Timespec_Equal_to( &timerdata.it_value, &my_period) ){
       printf( "NOT EQUAL %d:%d != %d:%d\n",
          timerdata.it_value.tv_sec,
          timerdata.it_value.tv_nsec,
          my_period.tv_sec,
          my_period.tv_nsec
       );
       rtems_test_exit(0);
     }
#endif

     pthread_mutex_lock (&data.mutex);
     clock = time(NULL);
     printf("Executing task B with count = %2i %s",
       params->count, ctime(&clock)
     );
     data.updated = TRUE;
     pthread_cond_signal  (&data.sync);
     pthread_mutex_unlock (&data.mutex);
     params->count--;
     if (params->count == 0)
       StopTimer(timer_id, &timerdata);
   }
   return NULL;
}

/* task C */

void * task_c (void *arg)
{
   int      count;
   struct   timespec my_period;
   int      my_sig, received_sig;
   struct   itimerspec timerdata;
   struct   itimerspec timergetdata;
   timer_t  timer_id;
   time_t   clock;
   struct   sigevent event;
   sigset_t set;
   struct   periodic_params *params;

   params = arg;
   my_period = params->period;
   my_sig    = params->signo;

   /* timer create */
   event.sigev_notify = SIGEV_SIGNAL;
   event.sigev_signo = my_sig;
   if (timer_create (CLOCK_REALTIME, &event, &timer_id) == -1) {
      perror ("Error in timer creation\n");
      rtems_test_exit(0);
    }

   /* block the timer signal */
   sigemptyset (&set);
   sigaddset (&set,my_sig);
   pthread_sigmask(SIG_BLOCK,&set,NULL);

   /* set the timer in periodic mode */
   timerdata.it_interval = my_period;
   timerdata.it_value    = my_period;
   timerdata.it_value.tv_sec *= 2;
   if (timer_settime(timer_id,POSIX_TIMER_RELATIVE,&timerdata,NULL) == -1) {
     perror ("Error in timer setting\n");
     rtems_test_exit(0);
   }

   /* periodic activity */
   for (count=0 ; ; count++) {
      if (sigwait(&set,&received_sig) == -1) {
       perror ("Error in sigwait\n");
       rtems_test_exit(0);
     }
     if (timer_gettime(timer_id, &timerdata) == -1) {
       perror ("Error in timer_gettime\n");
       rtems_test_exit(0);
     }
     if (! _Timespec_Equal_to( &timerdata.it_value, &my_period) ){
       fprintf(
           stderr, "Error in Task A timer_gettime:\n"
           "  re-armed timer: %" PRIdtime_t ":%ld does not match interval: %" PRIdtime_t ":%ld\n",
           timerdata.it_value.tv_sec, timerdata.it_value.tv_nsec,
           my_period.tv_sec, my_period.tv_nsec
       );
     }
     pthread_mutex_lock (&data.mutex);
     while (data.updated == FALSE) {
       pthread_cond_wait (&data.sync,&data.mutex);
     }
     clock = time(NULL);
     printf("Executing task C with count = %2i %s",
       params->count, ctime(&clock)
     );

     if ( count && (count % 5) == 0 ) {
       int overruns = 0;
       sleep(1);
       overruns = timer_getoverrun( timer_id );
       printf( "task C: timer_getoverrun - overruns=%d\n", overruns );

       if (timer_gettime(timer_id, &timergetdata) == -1) {
	 perror ("Error in timer setting\n");
         rtems_test_exit(0);
       }
       printf(
         "task C: timer_gettime - %" PRIdtime_t ":%ld remaining from %" PRIdtime_t ":%ld\n",
         timergetdata.it_value.tv_sec, timergetdata.it_value.tv_nsec,
         timergetdata.it_interval.tv_sec, timergetdata.it_interval.tv_nsec
       );
     }

     pthread_mutex_unlock (&data.mutex);
     params->count--;
     if (params->count == 0)
       StopTimer(timer_id, &timerdata);
   }
   return NULL;
}

/* main */

void *POSIX_Init (
  void *argument
)

{
   pthread_mutexattr_t mutexattr;    /* mutex attributes */
   pthread_condattr_t  condattr;     /* condition attributes */
   pthread_attr_t attr;              /* task attributes */
   pthread_t ta,tb,tc, tc1;          /* threads */
   sigset_t  set;                    /* signals */

   struct sched_param sch_param;     /* schedule parameters */
   struct periodic_params params_a, params_b, params_c, params_c1;

   TEST_BEGIN();

   data.updated = FALSE;

   /* mask signal */
   sigemptyset (&set);
   sigaddset (&set,SIGALRM);
   pthread_sigmask (SIG_BLOCK,&set,NULL);

   /* set mutex attributes */
   if (pthread_mutexattr_init (&mutexattr) != 0) {
     perror ("Error in mutex attribute init\n");
   }

   /* init mutex */
   if (pthread_mutex_init (&data.mutex,&mutexattr) != 0) {
     perror ("Error in mutex init");
   }

    /* init condition attributes */
   if (pthread_condattr_init (&condattr) != 0) {
     perror ("Error in condition attribute init\n");
   }

   /* init condition */
   if (pthread_cond_init (&data.sync,&condattr) != 0) {
     perror ("Error in condition init");
   }

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

   /* set explicit schedule for every task */
   if (pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED) != 0) {
      perror("Error in attribute inheritsched\n");
   }

   /* set task independent (join will not use) */
   if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED) != 0) {
      perror ("Error in attribute detachstate\n");
   }

   /* schedule policy POSIX_FIFO (priority preemtive and FIFO within the same
      priority) */
   if (pthread_attr_setschedpolicy (&attr, SCHED_FIFO) != 0) {
      perror ("Error in attribute setschedpolicy\n");
    }

   /* set and create thread A with priority 1 */

   sch_param.sched_priority = 1;
   if (pthread_attr_setschedparam(&attr, &sch_param) != 0) {
      perror ("Error in attribute schedparam\n");
    }

   /* Temporal parameters (1 sec. periodicity) */

   params_a.period.tv_sec  = 1;         /* seconds */
   params_a.period.tv_nsec = 000000000; /* nanoseconds */
   params_a.count          = 20;
   params_a.signo = SIGALRM;
   if (pthread_create (&ta, &attr, task_a, &params_a) != 0 ) {
     perror ("Error in thread create for task a\n");
   }

   /* set and create thread B with priority 15 */

   sch_param.sched_priority = 15;
   if (pthread_attr_setschedparam(&attr, &sch_param) != 0) {
      perror ("Error in attribute schedparam");
    }

   /* Temporal parameters (2 sec. periodicity) */
   params_b.period.tv_sec  = 2;         /* seconds */
   params_b.period.tv_nsec = 000000000; /* nanoseconds */
   params_b.count          = 10;
   params_b.signo = SIGALRM;
   if (pthread_create (&tb, &attr, task_b, &params_b) != 0) {
     perror ("Error in thread create for task b\n");
   }

   /* set and create thread B with priority 14 */

   sch_param.sched_priority = 14;
   if (pthread_attr_setschedparam(&attr, &sch_param) != 0 ) {
      perror ("Error in attribute schedparam\n");
    }

   /* Temporal parameters (3 sec. periodicity) */
   params_c.period.tv_sec  = 3;         /* seconds */
   params_c.period.tv_nsec = 000000000; /* nanoseconds */
   params_c.count          = 6;
   params_c.signo = SIGALRM;
   if (pthread_create (&tc, &attr, task_c, &params_c) != 0) {
     perror ("Error in thread create for task c\n");
   }

   /* execute 25 seconds and finish */
   sleep (25);

   puts( "starting C again with 0.5 second periodicity" );
   /* Temporal parameters (0.5 sec. periodicity) */
   params_c1.period.tv_sec  = 0;         /* seconds */
   params_c1.period.tv_nsec = 500000000; /* nanoseconds */
   params_c1.count          = 6;
   params_c1.signo = SIGALRM;
   if (pthread_create (&tc1, &attr, task_c, &params_c1) != 0) {
     perror ("Error in thread create for task c1\n");
   }

   sleep(5);

   TEST_END();
   rtems_test_exit (0);
 }