Subversion Repositories shark

Rev

Rev 318 | Details | Compare with Previous | 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
 ------------
318 giacomo 22
 CVS :        $Id: keys.c,v 1.2 2003-11-05 15:05:11 giacomo Exp $
2 pj 23
 
24
 File:        $File$
318 giacomo 25
 Revision:    $Revision: 1.2 $
26
 Last update: $Date: 2003-11-05 15:05:11 $
2 pj 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>
1689 fabio 54
#include <arch/stdlib.h>
55
#include <arch/stdio.h>
56
#include <arch/string.h>
2 pj 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;
318 giacomo 130
  SYS_FLAGS f;
2 pj 131
 
318 giacomo 132
  f = kern_fsave();
2 pj 133
 
134
  if (freekey == -1) {
318 giacomo 135
    kern_frestore(f);
2 pj 136
    return (EAGAIN);
137
  }
138
 
139
  /* alloc a free key */
140
  key_data[freekey].used = 1;
141
  *key = freekey;
142
  freekey = key_data[freekey].next;
143
 
144
  /* fill the descriptor */
145
  key_data[*key].destr = d;
146
 
147
  /* fill the task descriptor keys */
148
  for (p=0; p<MAX_PROC; p++)
149
    proc_table[p].keys[*key] = NULL;
150
 
318 giacomo 151
  kern_frestore(f);
2 pj 152
  return 0;
153
}
154
 
155
/*---------------------------------------------------------------------*/
156
/* 17.1.2 Thread Specific Data Management                              */
157
/*---------------------------------------------------------------------*/
158
 
159
void *task_getspecific(task_key_t key)
160
{
161
  void *ret;
318 giacomo 162
  SYS_FLAGS f;
2 pj 163
 
318 giacomo 164
  f = kern_fsave();
2 pj 165
  ret = proc_table[exec_shadow].keys[key];
318 giacomo 166
  kern_frestore(f);
2 pj 167
 
168
  return ret;
169
}
170
 
171
int task_setspecific(task_key_t key, const void *value)
172
{
318 giacomo 173
  SYS_FLAGS f;
174
 
175
  f = kern_fsave();
2 pj 176
  if (key < 0 || key >= PTHREAD_KEYS_MAX || !key_data[key].used) {
318 giacomo 177
    kern_frestore(f);
2 pj 178
    return (EINVAL);
179
  }
180
  proc_table[exec_shadow].keys[key] = (void *)value;
318 giacomo 181
  kern_frestore(f);
2 pj 182
  return 0;
183
}
184
 
185
/*---------------------------------------------------------------------*/
186
/* 17.1.3 Thread-Specific Data key deletion                            */
187
/*---------------------------------------------------------------------*/
188
 
189
int task_key_delete(task_key_t key)
190
{
191
  SYS_FLAGS f; /* task_key dete can be called in a destructor!!! */
192
 
193
  f = kern_fsave();
194
  if (key < 0 || key >= PTHREAD_KEYS_MAX || !key_data[key].used) {
195
    kern_frestore(f);
196
    return (EINVAL);
197
  }
198
 
199
  key_data[key].next = freekey;
200
  key_data[key].used = 0;
201
 
202
  freekey = key;
203
  kern_frestore(f);
204
  return 0;
205
}