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: ptest2.c,v 1.3 2004-05-23 09:07:28 giacomo Exp $ |
1085 | pj | 22 | |
23 | File: $File$ |
||
1450 | giacomo | 24 | Revision: $Revision: 1.3 $ |
25 | Last update: $Date: 2004-05-23 09:07:28 $ |
||
1085 | pj | 26 | ------------ |
27 | |||
28 | Posix test 2: |
||
29 | |||
30 | pthread_once + thread_specific_data |
||
31 | |||
32 | the main task: |
||
33 | creates a key |
||
34 | creates 2 tasks, J1, J2 |
||
35 | at t = 0.4 sec. it kills J1 and J2 |
||
36 | |||
37 | J1 and J2 will set and check the thread specific data |
||
38 | and when the die, a destructor is called (twice, because the value |
||
39 | is not set to null...) |
||
40 | |||
41 | non standard function used: |
||
42 | cprintf |
||
43 | sys_gettime |
||
44 | keyboard stuffs |
||
45 | sys_end |
||
46 | |||
47 | **/ |
||
48 | |||
49 | /* |
||
50 | * Copyright (C) 2000 Paolo Gai |
||
51 | * |
||
52 | * This program is free software; you can redistribute it and/or modify |
||
53 | * it under the terms of the GNU General Public License as published by |
||
54 | * the Free Software Foundation; either version 2 of the License, or |
||
55 | * (at your option) any later version. |
||
56 | * |
||
57 | * This program is distributed in the hope that it will be useful, |
||
58 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
59 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
60 | * GNU General Public License for more details. |
||
61 | * |
||
62 | * You should have received a copy of the GNU General Public License |
||
63 | * along with this program; if not, write to the Free Software |
||
64 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
65 | * |
||
66 | */ |
||
67 | |||
68 | #include <sys/types.h> |
||
69 | #include <pthread.h> |
||
70 | |||
71 | #include <kernel/kern.h> |
||
72 | |||
73 | pthread_key_t prova_key; |
||
74 | |||
75 | pthread_once_t once = PTHREAD_ONCE_INIT; |
||
76 | |||
77 | void once_init() |
||
78 | { |
||
79 | cprintf("ONCE: (pid=%d)\n", exec_shadow); |
||
80 | } |
||
81 | |||
82 | void destr_key(void *arg) |
||
83 | { |
||
84 | cprintf("J (pid=%d) destructor called with value %d\n", exec_shadow,(int)arg); |
||
85 | pthread_setspecific(prova_key,(void *)((int)arg/100)); |
||
86 | } |
||
87 | |||
88 | void print_test() |
||
89 | { |
||
90 | int val; |
||
91 | |||
92 | val = (int)pthread_getspecific(prova_key); |
||
93 | cprintf("J (pid=%d) printtest value=%d\n", exec_shadow, val); |
||
94 | } |
||
95 | |||
96 | void *J(void *arg) |
||
97 | { |
||
98 | pthread_once(&once, once_init); |
||
99 | cprintf("J (pid=%d) starts and call setspecific\n", exec_shadow); |
||
100 | pthread_setspecific(prova_key,arg); |
||
101 | print_test(); |
||
102 | cprintf("J (pid=%d) exits\n", exec_shadow); |
||
103 | |||
104 | return 0; |
||
105 | } |
||
106 | |||
107 | int main(int argc, char **argv) |
||
108 | { |
||
109 | int err; |
||
110 | pthread_t j1, j2; |
||
111 | |||
112 | cprintf("main: creating prova_key\n"); |
||
113 | pthread_key_create(&prova_key, destr_key); |
||
114 | |||
115 | cprintf("main: provakey =%d\n", prova_key); |
||
116 | |||
117 | cprintf("main: creating J1\n"); |
||
118 | err = pthread_create(&j1, NULL, J, (void *)1414); |
||
119 | if (err) cprintf("Error creating J1\n"); |
||
120 | cprintf("main: J1 has PID %d\n",j1); |
||
121 | |||
122 | cprintf("main: creating J2\n"); |
||
123 | err = pthread_create(&j2, NULL, J, (void *)3141); |
||
124 | if (err) cprintf("Error creating J2\n"); |
||
125 | cprintf("main: J2 has PID %d\n",j2); |
||
126 | |||
127 | cprintf("main: waiting 0.4 sec\n"); |
||
128 | while (sys_gettime(NULL) < 400000); |
||
129 | |||
130 | cprintf("main: kill J1 and J2\n"); |
||
131 | pthread_cancel(j1); |
||
132 | pthread_cancel(j2); |
||
133 | |||
134 | cprintf("main: ending...\n"); |
||
135 | |||
136 | return 0; |
||
137 | } |