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: ptest2.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 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
void fine(KEY_EVT *e)
109
{
110
  sys_end();
111
}
112
 
113
 
114
int main(int argc, char **argv)
115
{
116
  int err;
117
  pthread_t j1, j2;
118
 
119
    KEY_EVT emerg;
120
    //keyb_set_map(itaMap);
121
    emerg.ascii = 'x';
122
    emerg.scan = KEY_X;
123
    emerg.flag = ALTL_BIT;
124
    keyb_hook(emerg,fine);
125
 
126
 
127
  cprintf("main: creating prova_key\n");
128
  pthread_key_create(&prova_key, destr_key);
129
 
130
  cprintf("main: provakey =%d\n", prova_key);
131
 
132
  cprintf("main: creating J1\n");
133
  err = pthread_create(&j1, NULL, J, (void *)1414);
134
  if (err) cprintf("Error creating J1\n");
135
  cprintf("main: J1 has PID %d\n",j1);
136
 
137
  cprintf("main: creating J2\n");
138
  err = pthread_create(&j2, NULL, J, (void *)3141);
139
  if (err) cprintf("Error creating J2\n");
140
  cprintf("main: J2 has PID %d\n",j2);
141
 
142
  cprintf("main: waiting 0.4 sec\n");
143
  while (sys_gettime(NULL) < 400000);
144
 
145
  cprintf("main: kill J1 and J2\n");
146
  pthread_cancel(j1);
147
  pthread_cancel(j2);
148
 
149
  cprintf("main: ending...\n");
150
 
151
  return 0;
152
}