Subversion Repositories shark

Rev

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