-
Notifications
You must be signed in to change notification settings - Fork 0
/
pth_data.c
133 lines (124 loc) · 4.08 KB
/
pth_data.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
** GNU Pth - The GNU Portable Threads
** Copyright (c) 1999-2006 Ralf S. Engelschall <[email protected]>
**
** This file is part of GNU Pth, a non-preemptive thread scheduling
** library which can be found at http://www.gnu.org/software/pth/.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA, or contact Ralf S. Engelschall <[email protected]>.
**
** pth_data.c: Pth per-thread specific data
*/
/* ``Breakthrough ideas
are not from teams.''
--- Hans von Ohain */
#include "pth_p.h"
struct pth_keytab_st {
int used;
void (*destructor)(void *);
};
static struct pth_keytab_st pth_keytab[PTH_KEY_MAX];
int pth_key_create(pth_key_t *key, void (*func)(void *))
{
if (key == NULL)
return pth_error(FALSE, EINVAL);
for ((*key) = 0; (*key) < PTH_KEY_MAX; (*key)++) {
if (pth_keytab[(*key)].used == FALSE) {
pth_keytab[(*key)].used = TRUE;
pth_keytab[(*key)].destructor = func;
return TRUE;
}
}
return pth_error(FALSE, EAGAIN);
}
int pth_key_delete(pth_key_t key)
{
if (key < 0 || key >= PTH_KEY_MAX)
return pth_error(FALSE, EINVAL);
if (!pth_keytab[key].used)
return pth_error(FALSE, ENOENT);
pth_keytab[key].used = FALSE;
return TRUE;
}
int pth_key_setdata(pth_key_t key, const void *value)
{
if (key < 0 || key >= PTH_KEY_MAX)
return pth_error(FALSE, EINVAL);
if (!pth_keytab[key].used)
return pth_error(FALSE, ENOENT);
if (pth_current->data_value == NULL) {
pth_current->data_value = (const void **)calloc(1, sizeof(void *)*PTH_KEY_MAX);
if (pth_current->data_value == NULL)
return pth_error(FALSE, ENOMEM);
}
if (pth_current->data_value[key] == NULL) {
if (value != NULL)
pth_current->data_count++;
}
else {
if (value == NULL)
pth_current->data_count--;
}
pth_current->data_value[key] = value;
return TRUE;
}
void *pth_key_getdata(pth_key_t key)
{
if (key < 0 || key >= PTH_KEY_MAX)
return pth_error((void *)NULL, EINVAL);
if (!pth_keytab[key].used)
return pth_error((void *)NULL, ENOENT);
if (pth_current->data_value == NULL)
return (void *)NULL;
return (void *)pth_current->data_value[key];
}
intern void pth_key_destroydata(pth_t t)
{
void *data;
int key;
int itr;
void (*destructor)(void *);
if (t == NULL)
return;
if (t->data_value == NULL)
return;
/* POSIX thread iteration scheme */
for (itr = 0; itr < PTH_DESTRUCTOR_ITERATIONS; itr++) {
for (key = 0; key < PTH_KEY_MAX; key++) {
if (t->data_count > 0) {
destructor = NULL;
data = NULL;
if (pth_keytab[key].used) {
if (t->data_value[key] != NULL) {
data = (void *)t->data_value[key];
t->data_value[key] = NULL;
t->data_count--;
destructor = pth_keytab[key].destructor;
}
}
if (destructor != NULL)
destructor(data);
}
if (t->data_count == 0)
break;
}
if (t->data_count == 0)
break;
}
free(t->data_value);
t->data_value = NULL;
return;
}