File: pth_data.c

package info (click to toggle)
pth 1.4.1-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,292 kB
  • ctags: 714
  • sloc: sh: 8,966; ansic: 6,903; makefile: 490; perl: 112
file content (131 lines) | stat: -rw-r--r-- 4,032 bytes parent folder | download
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
/*
**  GNU Pth - The GNU Portable Threads
**  Copyright (c) 1999-2002 Ralf S. Engelschall <rse@engelschall.com>
**
**  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 <rse@engelschall.com>.
**
**  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 *))
{
    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_errno(FALSE, EAGAIN);
}

int pth_key_delete(pth_key_t key)
{
    if (key >= PTH_KEY_MAX)
        return_errno(FALSE, EINVAL);
    if (!pth_keytab[key].used)
        return_errno(FALSE, EINVAL);
    pth_keytab[key].used = FALSE;
    return TRUE;
}

int pth_key_setdata(pth_key_t key, const void *value)
{
    if (key >= PTH_KEY_MAX)
        return_errno(FALSE, EINVAL);
    if (!pth_keytab[key].used)
        return_errno(FALSE, EINVAL);
    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_errno(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 >= PTH_KEY_MAX)
        return_errno(NULL, EINVAL);
    if (!pth_keytab[key].used)
        return_errno(NULL, EINVAL);
    if (pth_current->data_value == NULL)
        return 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;
}