Subversion Repositories shark

Rev

Rev 1086 | Rev 1450 | 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
 ------------
1449 giacomo 21
 CVS :        $Id: ptest3.c,v 1.2 2004-05-23 09:05:51 giacomo Exp $
1085 pj 22
 
23
 File:        $File$
1449 giacomo 24
 Revision:    $Revision: 1.2 $
25
 Last update: $Date: 2004-05-23 09:05:51 $
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
#include <drivers/keyb.h>
67
 
68
int count25 = 0, count26 = 0;
69
 
70
void signal_handler(int signo, siginfo_t *info, void *extra)
71
{
72
  switch (signo) {
73
    case 25:
74
      count25++;
75
      break;
76
    case 26:
77
      count26++;
78
      break;
79
  }
80
 
81
  cprintf("Signal %d code=%s value=%d task=%d count25=%d count26=%d time=%ldusec\n",
82
              info->si_signo,
83
              (info->si_code == SI_TIMER) ? "Timer" : "Other",
84
              info->si_value.sival_int,
85
              info->si_task,
86
              count25,
87
              count26,
88
              sys_gettime(NULL));
89
}
90
 
91
void task_timer(union sigval value)
92
{
93
  cprintf("task_timer: value = %d, time = %ldusec\n",
94
              value.sival_int, sys_gettime(NULL));
95
}
96
 
97
int main(int argc, char **argv)
98
{
99
  int err;
100
  timer_t timer1, timer2, timer3;
101
  struct itimerspec timeout1, timeout2, timeout3, nulltimeout;
102
  struct sigaction sig_act;
103
  struct sigevent ev25, ev26, evtask;
104
  pthread_attr_t task_attr;
105
  struct sched_param task_param;
106
 
107
  sig_act.sa_sigaction = (void *) signal_handler;
108
  sig_act.sa_flags = SA_SIGINFO;
109
  sigemptyset(&sig_act.sa_mask);
110
 
111
  sigaction(25, &sig_act, NULL);
112
  sigaction(26, &sig_act, NULL);
113
 
114
  // set ev25, ev26, evtask
115
  ev25.sigev_notify           = SIGEV_SIGNAL;
116
  ev25.sigev_signo            = 25;
117
  ev25.sigev_value.sival_int  = 555;
118
 
119
  ev26.sigev_notify           = SIGEV_SIGNAL;
120
  ev26.sigev_signo            = 26;
121
  ev26.sigev_value.sival_int  = 666;
122
 
123
  evtask.sigev_notify            = SIGEV_THREAD;
124
  evtask.sigev_value.sival_int   = 777;
125
  evtask.sigev_notify_function   = task_timer;
126
  evtask.sigev_notify_attributes = &task_attr;
127
 
128
  pthread_attr_init(&task_attr);
129
  pthread_attr_setdetachstate(&task_attr, PTHREAD_CREATE_DETACHED);
130
  pthread_attr_setschedpolicy(&task_attr, SCHED_FIFO);
131
  task_param.sched_priority = 10;
132
  pthread_attr_setschedparam(&task_attr, &task_param);
133
 
134
  // set timeout1, timeout2, nulltimeout
135
  timeout1.it_interval.tv_sec  = 0;
136
  timeout1.it_interval.tv_nsec = 500000000;
137
  timeout1.it_value.tv_sec     = 3;
138
  timeout1.it_value.tv_nsec    = 0;
139
 
140
  timeout2.it_interval.tv_sec  = 0;
141
  timeout2.it_interval.tv_nsec = 200000000;
142
  timeout2.it_value.tv_sec     = 7;
143
  timeout2.it_value.tv_nsec    = 0;
144
 
145
  timeout3.it_interval.tv_sec  = 0;
146
  timeout3.it_interval.tv_nsec = 300000000;
147
  timeout3.it_value.tv_sec     = 5;
148
  timeout3.it_value.tv_nsec    = 0;
149
 
150
  NULL_TIMESPEC(&nulltimeout.it_value);
151
  NULL_TIMESPEC(&nulltimeout.it_interval);
152
 
153
  // create the timers
154
  err = timer_create(CLOCK_REALTIME, &ev25, &timer1);
155
  if (err == -1) { cprintf("main: unable to create timer 1\n"); }
156
 
157
  err = timer_create(CLOCK_REALTIME, &ev26, &timer2);
158
  if (err == -1) { cprintf("main: unable to create timer 2\n"); }
159
 
160
  err = timer_create(CLOCK_REALTIME, &evtask, &timer3);
161
  if (err == -1) { cprintf("main: unable to create timer 3\n"); }
162
 
163
  // arm the timers
164
  err = timer_settime(timer1, TIMER_ABSTIME, &timeout1, NULL);
165
  if (err == -1) { cprintf("main: unable to set timer 1\n"); }
166
 
167
  err = timer_settime(timer2, 0, &timeout2, NULL);
168
  if (err == -1) { cprintf("main: unable to set timer 2\n"); }
169
 
170
  err = timer_settime(timer3, TIMER_ABSTIME, &timeout3, NULL);
171
  if (err == -1) { cprintf("main: unable to set timer 3\n"); }
172
 
173
  cprintf("main: waiting signals...\n");
174
  while (sys_gettime(NULL) < 8500000) {
175
    //kern_deliver_pending_signals();
176
  }
177
 
178
  cprintf("main: disarm the timer2\n");
179
  err = timer_settime(timer2, 0, &nulltimeout, &timeout2);
180
  if (err == -1) { cprintf("main: unable to disarm timer 2\n"); }
181
 
182
  cprintf("main: timer2 disarmed, itvalue=%ld.%ld\n",
183
              timeout2.it_value.tv_sec,timeout2.it_value.tv_nsec/1000);
184
 
185
  while (sys_gettime(NULL) < 10000000) {
186
    //kern_deliver_pending_signals();
187
  }
188
 
189
  cprintf("main: disarm the timer1\n");
190
  err = timer_settime(timer1, TIMER_ABSTIME, &nulltimeout, &timeout1);
191
  if (err == -1) { cprintf("main: unable to disarm timer 1\n"); }
192
 
193
  cprintf("main: timer1 disarmed, itvalue=%ld.%ld\n",
194
              timeout1.it_value.tv_sec,timeout1.it_value.tv_nsec/1000);
195
 
196
  while (sys_gettime(NULL) < 12000000) {
197
    //kern_deliver_pending_signals();
198
  }
199
 
200
  cprintf("main: arm timer1\n");
201
  timeout1.it_interval.tv_sec  = 0;
202
  timeout1.it_interval.tv_nsec = 0;
203
  timeout1.it_value.tv_sec     = 13;
204
  timeout1.it_value.tv_nsec    = 0;
205
  err = timer_settime(timer1, TIMER_ABSTIME, &timeout1, NULL);
206
  if (err == -1) { cprintf("main: unable to arm timer 1\n"); }
207
 
208
  while (sys_gettime(NULL) < 14000000) {
209
    //kern_deliver_pending_signals();
210
  }
211
 
212
  cprintf("main: ending...\n");
213
 
214
  return 0;
215
}