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: ptest2.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 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
#include <drivers/keyb.h>
73
 
74
pthread_key_t prova_key;
75
 
76
pthread_once_t once = PTHREAD_ONCE_INIT;
77
 
78
void once_init()
79
{
80
  cprintf("ONCE: (pid=%d)\n", exec_shadow);
81
}
82
 
83
void destr_key(void *arg)
84
{
85
  cprintf("J (pid=%d) destructor called with value %d\n", exec_shadow,(int)arg);
86
  pthread_setspecific(prova_key,(void *)((int)arg/100));
87
}
88
 
89
void print_test()
90
{
91
  int val;
92
 
93
  val = (int)pthread_getspecific(prova_key);
94
  cprintf("J (pid=%d) printtest value=%d\n", exec_shadow, val);
95
}
96
 
97
void *J(void *arg)
98
{
99
  pthread_once(&once, once_init);
100
  cprintf("J (pid=%d) starts and call setspecific\n", exec_shadow);
101
  pthread_setspecific(prova_key,arg);
102
  print_test();
103
  cprintf("J (pid=%d) exits\n", exec_shadow);
104
 
105
  return 0;
106
}
107
 
108
int main(int argc, char **argv)
109
{
110
  int err;
111
  pthread_t j1, j2;
112
 
113
  cprintf("main: creating prova_key\n");
114
  pthread_key_create(&prova_key, destr_key);
115
 
116
  cprintf("main: provakey =%d\n", prova_key);
117
 
118
  cprintf("main: creating J1\n");
119
  err = pthread_create(&j1, NULL, J, (void *)1414);
120
  if (err) cprintf("Error creating J1\n");
121
  cprintf("main: J1 has PID %d\n",j1);
122
 
123
  cprintf("main: creating J2\n");
124
  err = pthread_create(&j2, NULL, J, (void *)3141);
125
  if (err) cprintf("Error creating J2\n");
126
  cprintf("main: J2 has PID %d\n",j2);
127
 
128
  cprintf("main: waiting 0.4 sec\n");
129
  while (sys_gettime(NULL) < 400000);
130
 
131
  cprintf("main: kill J1 and J2\n");
132
  pthread_cancel(j1);
133
  pthread_cancel(j2);
134
 
135
  cprintf("main: ending...\n");
136
 
137
  return 0;
138
}