Subversion Repositories shark

Rev

Rev 1450 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1085 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/**
20
 ------------
1550 pj 21
 CVS :        $Id: ptest3.c,v 1.4 2005-01-08 14:36:11 pj Exp $
1085 pj 22
 
23
 File:        $File$
1550 pj 24
 Revision:    $Revision: 1.4 $
25
 Last update: $Date: 2005-01-08 14:36:11 $
1085 pj 26
 ------------
27
 
28
 Posix test 3:
29
 
30
   timers...
31
   it creates two periodic timers that queues signals, a periodic timer
32
   that create tasks and an one-shot timer.
33
 
34
 non standard function used:
35
   cprintf
36
   sys_gettime
37
   keyboard stuffs
38
 
39
**/
40
 
41
/*
42
 * Copyright (C) 2000 Paolo Gai
43
 *
44
 * This program is free software; you can redistribute it and/or modify
45
 * it under the terms of the GNU General Public License as published by
46
 * the Free Software Foundation; either version 2 of the License, or
47
 * (at your option) any later version.
48
 *
49
 * This program is distributed in the hope that it will be useful,
50
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
52
 * GNU General Public License for more details.
53
 *
54
 * You should have received a copy of the GNU General Public License
55
 * along with this program; if not, write to the Free Software
56
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
57
 *
58
 */
59
 
60
#include <sys/types.h>
61
#include <pthread.h>
62
#include <time.h>
63
 
64
#include <kernel/kern.h>
65
 
66
int count25 = 0, count26 = 0;
67
 
68
void signal_handler(int signo, siginfo_t *info, void *extra)
69
{
70
  switch (signo) {
71
    case 25:
72
      count25++;
73
      break;
74
    case 26:
75
      count26++;
76
      break;
77
  }
78
 
79
  cprintf("Signal %d code=%s value=%d task=%d count25=%d count26=%d time=%ldusec\n",
80
              info->si_signo,
81
              (info->si_code == SI_TIMER) ? "Timer" : "Other",
82
              info->si_value.sival_int,
83
              info->si_task,
84
              count25,
85
              count26,
86
              sys_gettime(NULL));
87
}
88
 
89
void task_timer(union sigval value)
90
{
91
  cprintf("task_timer: value = %d, time = %ldusec\n",
92
              value.sival_int, sys_gettime(NULL));
93
}
94
 
95
int main(int argc, char **argv)
96
{
97
  int err;
98
  timer_t timer1, timer2, timer3;
99
  struct itimerspec timeout1, timeout2, timeout3, nulltimeout;
100
  struct sigaction sig_act;
101
  struct sigevent ev25, ev26, evtask;
102
  pthread_attr_t task_attr;
103
  struct sched_param task_param;
104
 
105
  sig_act.sa_sigaction = (void *) signal_handler;
106
  sig_act.sa_flags = SA_SIGINFO;
107
  sigemptyset(&sig_act.sa_mask);
108
 
109
  sigaction(25, &sig_act, NULL);
110
  sigaction(26, &sig_act, NULL);
111
 
112
  // set ev25, ev26, evtask
113
  ev25.sigev_notify           = SIGEV_SIGNAL;
114
  ev25.sigev_signo            = 25;
115
  ev25.sigev_value.sival_int  = 555;
116
 
117
  ev26.sigev_notify           = SIGEV_SIGNAL;
118
  ev26.sigev_signo            = 26;
119
  ev26.sigev_value.sival_int  = 666;
120
 
121
  evtask.sigev_notify            = SIGEV_THREAD;
122
  evtask.sigev_value.sival_int   = 777;
123
  evtask.sigev_notify_function   = task_timer;
124
  evtask.sigev_notify_attributes = &task_attr;
125
 
126
  pthread_attr_init(&task_attr);
127
  pthread_attr_setdetachstate(&task_attr, PTHREAD_CREATE_DETACHED);
128
  pthread_attr_setschedpolicy(&task_attr, SCHED_FIFO);
129
  task_param.sched_priority = 10;
130
  pthread_attr_setschedparam(&task_attr, &task_param);
131
 
132
  // set timeout1, timeout2, nulltimeout
133
  timeout1.it_interval.tv_sec  = 0;
134
  timeout1.it_interval.tv_nsec = 500000000;
135
  timeout1.it_value.tv_sec     = 3;
136
  timeout1.it_value.tv_nsec    = 0;
137
 
138
  timeout2.it_interval.tv_sec  = 0;
139
  timeout2.it_interval.tv_nsec = 200000000;
140
  timeout2.it_value.tv_sec     = 7;
141
  timeout2.it_value.tv_nsec    = 0;
142
 
143
  timeout3.it_interval.tv_sec  = 0;
144
  timeout3.it_interval.tv_nsec = 300000000;
145
  timeout3.it_value.tv_sec     = 5;
146
  timeout3.it_value.tv_nsec    = 0;
147
 
148
  NULL_TIMESPEC(&nulltimeout.it_value);
149
  NULL_TIMESPEC(&nulltimeout.it_interval);
150
 
151
  // create the timers
152
  err = timer_create(CLOCK_REALTIME, &ev25, &timer1);
153
  if (err == -1) { cprintf("main: unable to create timer 1\n"); }
154
 
155
  err = timer_create(CLOCK_REALTIME, &ev26, &timer2);
156
  if (err == -1) { cprintf("main: unable to create timer 2\n"); }
157
 
158
  err = timer_create(CLOCK_REALTIME, &evtask, &timer3);
159
  if (err == -1) { cprintf("main: unable to create timer 3\n"); }
160
 
161
  // arm the timers
162
  err = timer_settime(timer1, TIMER_ABSTIME, &timeout1, NULL);
163
  if (err == -1) { cprintf("main: unable to set timer 1\n"); }
164
 
165
  err = timer_settime(timer2, 0, &timeout2, NULL);
166
  if (err == -1) { cprintf("main: unable to set timer 2\n"); }
167
 
168
  err = timer_settime(timer3, TIMER_ABSTIME, &timeout3, NULL);
169
  if (err == -1) { cprintf("main: unable to set timer 3\n"); }
170
 
171
  cprintf("main: waiting signals...\n");
172
  while (sys_gettime(NULL) < 8500000) {
173
    //kern_deliver_pending_signals();
174
  }
175
 
176
  cprintf("main: disarm the timer2\n");
177
  err = timer_settime(timer2, 0, &nulltimeout, &timeout2);
178
  if (err == -1) { cprintf("main: unable to disarm timer 2\n"); }
179
 
180
  cprintf("main: timer2 disarmed, itvalue=%ld.%ld\n",
181
              timeout2.it_value.tv_sec,timeout2.it_value.tv_nsec/1000);
182
 
183
  while (sys_gettime(NULL) < 10000000) {
184
    //kern_deliver_pending_signals();
185
  }
186
 
187
  cprintf("main: disarm the timer1\n");
188
  err = timer_settime(timer1, TIMER_ABSTIME, &nulltimeout, &timeout1);
189
  if (err == -1) { cprintf("main: unable to disarm timer 1\n"); }
190
 
191
  cprintf("main: timer1 disarmed, itvalue=%ld.%ld\n",
192
              timeout1.it_value.tv_sec,timeout1.it_value.tv_nsec/1000);
193
 
194
  while (sys_gettime(NULL) < 12000000) {
195
    //kern_deliver_pending_signals();
196
  }
197
 
198
  cprintf("main: arm timer1\n");
199
  timeout1.it_interval.tv_sec  = 0;
200
  timeout1.it_interval.tv_nsec = 0;
201
  timeout1.it_value.tv_sec     = 13;
202
  timeout1.it_value.tv_nsec    = 0;
203
  err = timer_settime(timer1, TIMER_ABSTIME, &timeout1, NULL);
204
  if (err == -1) { cprintf("main: unable to arm timer 1\n"); }
205
 
206
  while (sys_gettime(NULL) < 14000000) {
207
    //kern_deliver_pending_signals();
208
  }
209
 
210
  cprintf("main: ending...\n");
211
 
212
  return 0;
213
}