Subversion Repositories shark

Rev

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