Subversion Repositories shark

Rev

Rev 1085 | 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
 ------------
21
 CVS :        $Id: ptest3.c,v 1.1.1.1 2002-09-02 09:37:47 pj Exp $
22
 
23
 File:        $File$
24
 Revision:    $Revision: 1.1.1.1 $
25
 Last update: $Date: 2002-09-02 09:37:47 $
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 fine(KEY_EVT *e)
92
{
93
  sys_end();
94
}
95
 
96
void task_timer(union sigval value)
97
{
98
  cprintf("task_timer: value = %d, time = %ldusec\n",
99
              value.sival_int, sys_gettime(NULL));
100
}
101
 
102
int main(int argc, char **argv)
103
{
104
  int err;
105
  timer_t timer1, timer2, timer3;
106
  struct itimerspec timeout1, timeout2, timeout3, nulltimeout;
107
  struct sigaction sig_act;
108
  struct sigevent ev25, ev26, evtask;
109
  pthread_attr_t task_attr;
110
  struct sched_param task_param;
111
 
112
    KEY_EVT emerg;
113
    //keyb_set_map(itaMap);
114
    emerg.ascii = 'x';
115
    emerg.scan = KEY_X;
116
    emerg.flag = ALTL_BIT;
117
    keyb_hook(emerg,fine);
118
 
119
  sig_act.sa_sigaction = (void *) signal_handler;
120
  sig_act.sa_flags = SA_SIGINFO;
121
  sigemptyset(&sig_act.sa_mask);
122
 
123
  sigaction(25, &sig_act, NULL);
124
  sigaction(26, &sig_act, NULL);
125
 
126
  // set ev25, ev26, evtask
127
  ev25.sigev_notify           = SIGEV_SIGNAL;
128
  ev25.sigev_signo            = 25;
129
  ev25.sigev_value.sival_int  = 555;
130
 
131
  ev26.sigev_notify           = SIGEV_SIGNAL;
132
  ev26.sigev_signo            = 26;
133
  ev26.sigev_value.sival_int  = 666;
134
 
135
  evtask.sigev_notify            = SIGEV_THREAD;
136
  evtask.sigev_value.sival_int   = 777;
137
  evtask.sigev_notify_function   = task_timer;
138
  evtask.sigev_notify_attributes = &task_attr;
139
 
140
  pthread_attr_init(&task_attr);
141
  pthread_attr_setdetachstate(&task_attr, PTHREAD_CREATE_DETACHED);
142
  pthread_attr_setschedpolicy(&task_attr, SCHED_FIFO);
143
  task_param.sched_priority = 10;
144
  pthread_attr_setschedparam(&task_attr, &task_param);
145
 
146
  // set timeout1, timeout2, nulltimeout
147
  timeout1.it_interval.tv_sec  = 0;
148
  timeout1.it_interval.tv_nsec = 500000000;
149
  timeout1.it_value.tv_sec     = 3;
150
  timeout1.it_value.tv_nsec    = 0;
151
 
152
  timeout2.it_interval.tv_sec  = 0;
153
  timeout2.it_interval.tv_nsec = 200000000;
154
  timeout2.it_value.tv_sec     = 7;
155
  timeout2.it_value.tv_nsec    = 0;
156
 
157
  timeout3.it_interval.tv_sec  = 0;
158
  timeout3.it_interval.tv_nsec = 300000000;
159
  timeout3.it_value.tv_sec     = 5;
160
  timeout3.it_value.tv_nsec    = 0;
161
 
162
  NULL_TIMESPEC(&nulltimeout.it_value);
163
  NULL_TIMESPEC(&nulltimeout.it_interval);
164
 
165
  // create the timers
166
  err = timer_create(CLOCK_REALTIME, &ev25, &timer1);
167
  if (err == -1) { cprintf("main: unable to create timer 1\n"); }
168
 
169
  err = timer_create(CLOCK_REALTIME, &ev26, &timer2);
170
  if (err == -1) { cprintf("main: unable to create timer 2\n"); }
171
 
172
  err = timer_create(CLOCK_REALTIME, &evtask, &timer3);
173
  if (err == -1) { cprintf("main: unable to create timer 3\n"); }
174
 
175
  // arm the timers
176
  err = timer_settime(timer1, TIMER_ABSTIME, &timeout1, NULL);
177
  if (err == -1) { cprintf("main: unable to set timer 1\n"); }
178
 
179
  err = timer_settime(timer2, 0, &timeout2, NULL);
180
  if (err == -1) { cprintf("main: unable to set timer 2\n"); }
181
 
182
  err = timer_settime(timer3, TIMER_ABSTIME, &timeout3, NULL);
183
  if (err == -1) { cprintf("main: unable to set timer 3\n"); }
184
 
185
  cprintf("main: waiting signals...\n");
186
  while (sys_gettime(NULL) < 8500000) {
187
    //kern_deliver_pending_signals();
188
  }
189
 
190
  cprintf("main: disarm the timer2\n");
191
  err = timer_settime(timer2, 0, &nulltimeout, &timeout2);
192
  if (err == -1) { cprintf("main: unable to disarm timer 2\n"); }
193
 
194
  cprintf("main: timer2 disarmed, itvalue=%ld.%ld\n",
195
              timeout2.it_value.tv_sec,timeout2.it_value.tv_nsec/1000);
196
 
197
  while (sys_gettime(NULL) < 10000000) {
198
    //kern_deliver_pending_signals();
199
  }
200
 
201
  cprintf("main: disarm the timer1\n");
202
  err = timer_settime(timer1, TIMER_ABSTIME, &nulltimeout, &timeout1);
203
  if (err == -1) { cprintf("main: unable to disarm timer 1\n"); }
204
 
205
  cprintf("main: timer1 disarmed, itvalue=%ld.%ld\n",
206
              timeout1.it_value.tv_sec,timeout1.it_value.tv_nsec/1000);
207
 
208
  while (sys_gettime(NULL) < 12000000) {
209
    //kern_deliver_pending_signals();
210
  }
211
 
212
  cprintf("main: arm timer1\n");
213
  timeout1.it_interval.tv_sec  = 0;
214
  timeout1.it_interval.tv_nsec = 0;
215
  timeout1.it_value.tv_sec     = 13;
216
  timeout1.it_value.tv_nsec    = 0;
217
  err = timer_settime(timer1, TIMER_ABSTIME, &timeout1, NULL);
218
  if (err == -1) { cprintf("main: unable to arm timer 1\n"); }
219
 
220
  while (sys_gettime(NULL) < 14000000) {
221
    //kern_deliver_pending_signals();
222
  }
223
 
224
 
225
 
226
  cprintf("main: ending...\n");
227
 
228
  return 0;
229
}