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: ptest1.c,v 1.2 2004-05-23 09:05:50 giacomo Exp $
1085 pj 22
 
23
 File:        $File$
1449 giacomo 24
 Revision:    $Revision: 1.2 $
25
 Last update: $Date: 2004-05-23 09:05:50 $
1085 pj 26
 ------------
27
 
28
 Posix test 1:
29
 
30
   the main task create 4 tasks, J1, J2, J3, J4
31
     at t = 0.2 sec. it raise a signal to J1
32
     at t = 0.4 sec. it raise a signal to J2
33
     at t = 0.8 sec. it kill J4
34
 
35
   J1: it simply calls sigwait
36
 
37
   J2: it simply calls sigwaitinfo
38
 
39
   J3: it simply calls sigtimedwait with a timeout of 0.5 sec.
40
 
41
   J4: it simply calls sigtimedwait with a -long- timeout
42
       (J4 will be killed by main)
43
 
44
 non standard function used:
45
   cprintf
46
   sys_gettime
47
   keyboard stuffs
48
   sys_end
49
 
50
**/
51
 
52
/*
53
 * Copyright (C) 2000 Paolo Gai
54
 *
55
 * This program is free software; you can redistribute it and/or modify
56
 * it under the terms of the GNU General Public License as published by
57
 * the Free Software Foundation; either version 2 of the License, or
58
 * (at your option) any later version.
59
 *
60
 * This program is distributed in the hope that it will be useful,
61
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
62
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
63
 * GNU General Public License for more details.
64
 *
65
 * You should have received a copy of the GNU General Public License
66
 * along with this program; if not, write to the Free Software
67
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
68
 *
69
 */
70
 
71
#include <sys/types.h>
72
#include <pthread.h>
73
#include <signal.h>
74
 
75
#include <kernel/kern.h>
76
#include <drivers/keyb.h>
77
 
78
void *J1(void *arg)
79
{
80
  int sig, err;
81
  sigset_t mask;
82
 
83
  cprintf("J1 starts and call sigwait(31)\n");
84
 
85
  sigemptyset(&mask);
86
  sigaddset(&mask,31);
87
  err = sigwait(&mask,&sig);
88
 
89
  cprintf("J1 exit from sigwait(), err=%d, sig=%d\n", err, sig);
90
 
91
  return 0;
92
}
93
 
94
void *J2(void *arg)
95
{
96
  int err;
97
  siginfo_t info;
98
  sigset_t mask;
99
 
100
  cprintf("J2 starts and call sigwaitinfo(30)\n");
101
 
102
  sigemptyset(&mask);
103
  sigaddset(&mask,30);
104
  err = sigwaitinfo(&mask,&info);
105
 
106
  cprintf("J2 exit from sigwaitinfo(), err=%d, signo=%d code=%d value=%d\n",
107
              err, info.si_signo, info.si_code, info.si_value.sival_int);
108
 
109
  return 0;
110
}
111
 
112
void *J3(void *arg)
113
{
114
  int err;
115
  siginfo_t info;
116
  sigset_t mask;
117
  struct timespec t;
118
 
119
  cprintf("J3 starts and call sigtimedwait(29)\n");
120
 
121
  sigemptyset(&mask);
122
  sigaddset(&mask,29);
123
  t.tv_sec = 0;
124
  t.tv_nsec = 300000000;
125
  err = sigtimedwait(&mask,&info,&t);
126
 
127
  cprintf("J3 exit from sigtimedwait(), err=%d, signo=%d code=%d value=%d\n",
128
              err, info.si_signo, info.si_code, info.si_value.sival_int);
129
 
130
  return 0;
131
}
132
 
133
void uscitaJ4(void *arg)
134
{
135
  cprintf("J4: AAAARRRRGGGHHH!!! killed by someone...\n");
136
}
137
 
138
void *J4(void *arg)
139
{
140
  int err;
141
  siginfo_t info;
142
  sigset_t mask;
143
  struct timespec t;
144
 
145
  cprintf("J4 starts and call sigtimedwait(28)\n");
146
 
147
  sigemptyset(&mask);
148
  sigaddset(&mask,28);
149
  t.tv_sec = 10;
150
  t.tv_nsec = 0;
151
 
152
  pthread_cleanup_push(uscitaJ4,NULL);
153
  err = sigtimedwait(&mask,&info,&t);
154
  pthread_cleanup_pop(0);
155
 
156
  cprintf("J4 exit from sigtimedwait(), err=%d, signo=%d code=%d value=%d\n",
157
              err, info.si_signo, info.si_code, info.si_value.sival_int);
158
 
159
  return 0;
160
}
161
 
162
int main(int argc, char **argv)
163
{
164
  int err;
165
  sigset_t mask;
166
  pthread_t j1, j2, j3, j4;
167
  union sigval value;
168
 
169
 
170
  /* main blocks signals for all the tasks */
171
  sigfillset(&mask);
172
  pthread_sigmask(SIG_BLOCK, &mask, NULL);
173
 
174
  cprintf("main: creating J1\n");
175
  err = pthread_create(&j1, NULL, J1, NULL);
176
  if (err) cprintf("Error creating J1\n");
177
 
178
  cprintf("main: creating J2\n");
179
  err = pthread_create(&j2, NULL, J2, NULL);
180
  if (err) cprintf("Error creating J2\n");
181
 
182
  cprintf("main: creating J3\n");
183
  err = pthread_create(&j3, NULL, J3, NULL);
184
  if (err) cprintf("Error creating J3\n");
185
 
186
  cprintf("main: creating J4\n");
187
  err = pthread_create(&j4, NULL, J4, NULL);
188
  if (err) cprintf("Error creating J4\n");
189
 
190
  cprintf("main: waiting 0.2 sec\n");
191
  while (sys_gettime(NULL) < 200000);
192
 
193
  cprintf("main: kill(31), then wait until t=0.4 sec \n");
194
  kill(0, 31);
195
 
196
  while (sys_gettime(NULL) < 400000);
197
  cprintf("main: sigqueue(30), then wait until t=0.8 sec \n");
198
  value.sival_int = 300;
199
  sigqueue(0, 30, value);
200
 
201
  while (sys_gettime(NULL) < 800000);
202
  cprintf("main: kill(J4)\n");
203
  pthread_cancel(j4);
204
 
205
  cprintf("main: ending...\n");
206
 
207
  return 0;
208
}