Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | 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 | ------------ |
||
22 | CVS : $Id: keys.c,v 1.1.1.1 2002-03-29 14:12:52 pj Exp $ |
||
23 | |||
24 | File: $File$ |
||
25 | Revision: $Revision: 1.1.1.1 $ |
||
26 | Last update: $Date: 2002-03-29 14:12:52 $ |
||
27 | ------------ |
||
28 | |||
29 | task_specific data |
||
30 | |||
31 | **/ |
||
32 | |||
33 | /* |
||
34 | * Copyright (C) 2000 Paolo Gai |
||
35 | * |
||
36 | * This program is free software; you can redistribute it and/or modify |
||
37 | * it under the terms of the GNU General Public License as published by |
||
38 | * the Free Software Foundation; either version 2 of the License, or |
||
39 | * (at your option) any later version. |
||
40 | * |
||
41 | * This program is distributed in the hope that it will be useful, |
||
42 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
43 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
44 | * GNU General Public License for more details. |
||
45 | * |
||
46 | * You should have received a copy of the GNU General Public License |
||
47 | * along with this program; if not, write to the Free Software |
||
48 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
49 | * |
||
50 | */ |
||
51 | |||
52 | #include <stdarg.h> |
||
53 | #include <ll/ll.h> |
||
54 | #include <ll/stdlib.h> |
||
55 | #include <ll/stdio.h> |
||
56 | #include <ll/string.h> |
||
57 | #include <kernel/config.h> |
||
58 | #include <kernel/model.h> |
||
59 | #include <kernel/const.h> |
||
60 | #include <sys/types.h> |
||
61 | #include <kernel/types.h> |
||
62 | #include <kernel/descr.h> |
||
63 | #include <errno.h> |
||
64 | #include <kernel/var.h> |
||
65 | #include <kernel/func.h> |
||
66 | |||
67 | |||
68 | /*+ This data contains key definition... +*/ |
||
69 | static struct key_data_struct { |
||
70 | void (*destr)(void *); |
||
71 | int next; |
||
72 | int used; |
||
73 | } key_data[PTHREAD_KEYS_MAX]; |
||
74 | |||
75 | /*+ This is the key free queue... used for alloc new keys +*/ |
||
76 | static int freekey; |
||
77 | |||
78 | void call_task_specific_data_destructors() |
||
79 | { |
||
80 | int keycount; /* number of times the key destructors are called */ |
||
81 | int key_exit; /* flag used to exit from the destruction process */ |
||
82 | int key_i; /* a counter */ |
||
83 | |||
84 | for (keycount = 0; |
||
85 | keycount < PTHREAD_DESTRUCTOR_ITERATIONS; |
||
86 | keycount++) { |
||
87 | |||
88 | key_exit = 0; |
||
89 | for (key_i=1; key_i<PTHREAD_KEYS_MAX; key_i++) { |
||
90 | if (key_data[key_i].used && |
||
91 | key_data[key_i].destr && |
||
92 | proc_table[exec_shadow].keys[key_i] ) { |
||
93 | kern_sti(); |
||
94 | key_data[key_i].destr(proc_table[exec_shadow].keys[key_i]); |
||
95 | kern_cli(); |
||
96 | key_exit |= (int)proc_table[exec_shadow].keys[key_i]; |
||
97 | } |
||
98 | } |
||
99 | if (!key_exit) break; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | void task_specific_data_init() |
||
104 | { |
||
105 | int j; |
||
106 | |||
107 | for (j=1; j<PTHREAD_KEYS_MAX; j++) { |
||
108 | key_data[j].destr = NULL; |
||
109 | key_data[j].next = j+1; |
||
110 | key_data[j].used = 0; |
||
111 | } |
||
112 | key_data[PTHREAD_KEYS_MAX-1].next = -1; |
||
113 | |||
114 | /* alloc the free key 0 for cleanup handlers */ |
||
115 | freekey = 1; |
||
116 | key_data[0].used = 1; |
||
117 | key_data[0].destr = NULL; |
||
118 | key_data[0].next = -1; |
||
119 | } |
||
120 | |||
121 | /*---------------------------------------------------------------------*/ |
||
122 | /* Task specific data Handling */ |
||
123 | /*---------------------------------------------------------------------*/ |
||
124 | |||
125 | /* look at the POSIX standard's Section 17 for more details... */ |
||
126 | |||
127 | int task_key_create(task_key_t *key, void (*d)(void *)) |
||
128 | { |
||
129 | PID p; |
||
130 | |||
131 | kern_cli(); |
||
132 | |||
133 | if (freekey == -1) { |
||
134 | kern_sti(); |
||
135 | return (EAGAIN); |
||
136 | } |
||
137 | |||
138 | /* alloc a free key */ |
||
139 | key_data[freekey].used = 1; |
||
140 | *key = freekey; |
||
141 | freekey = key_data[freekey].next; |
||
142 | |||
143 | /* fill the descriptor */ |
||
144 | key_data[*key].destr = d; |
||
145 | |||
146 | /* fill the task descriptor keys */ |
||
147 | for (p=0; p<MAX_PROC; p++) |
||
148 | proc_table[p].keys[*key] = NULL; |
||
149 | |||
150 | kern_sti(); |
||
151 | return 0; |
||
152 | } |
||
153 | |||
154 | /*---------------------------------------------------------------------*/ |
||
155 | /* 17.1.2 Thread Specific Data Management */ |
||
156 | /*---------------------------------------------------------------------*/ |
||
157 | |||
158 | void *task_getspecific(task_key_t key) |
||
159 | { |
||
160 | void *ret; |
||
161 | |||
162 | kern_cli(); |
||
163 | ret = proc_table[exec_shadow].keys[key]; |
||
164 | kern_sti(); |
||
165 | |||
166 | return ret; |
||
167 | } |
||
168 | |||
169 | int task_setspecific(task_key_t key, const void *value) |
||
170 | { |
||
171 | kern_cli(); |
||
172 | if (key < 0 || key >= PTHREAD_KEYS_MAX || !key_data[key].used) { |
||
173 | kern_sti(); |
||
174 | return (EINVAL); |
||
175 | } |
||
176 | proc_table[exec_shadow].keys[key] = (void *)value; |
||
177 | kern_sti(); |
||
178 | return 0; |
||
179 | } |
||
180 | |||
181 | /*---------------------------------------------------------------------*/ |
||
182 | /* 17.1.3 Thread-Specific Data key deletion */ |
||
183 | /*---------------------------------------------------------------------*/ |
||
184 | |||
185 | int task_key_delete(task_key_t key) |
||
186 | { |
||
187 | SYS_FLAGS f; /* task_key dete can be called in a destructor!!! */ |
||
188 | |||
189 | f = kern_fsave(); |
||
190 | if (key < 0 || key >= PTHREAD_KEYS_MAX || !key_data[key].used) { |
||
191 | kern_frestore(f); |
||
192 | return (EINVAL); |
||
193 | } |
||
194 | |||
195 | key_data[key].next = freekey; |
||
196 | key_data[key].used = 0; |
||
197 | |||
198 | freekey = key; |
||
199 | kern_frestore(f); |
||
200 | return 0; |
||
201 | } |