KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
tls.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/tls.h
4 Copyright (C) 2009, 2010 Lawrence Sebald
5
6*/
7
8/** \file kos/tls.h
9 \brief Thread-local storage support.
10 \ingroup kthreads
11
12 This file contains the definitions used to support key/value pairs of
13 thread-local storage in KOS.
14
15 \author Lawrence Sebald
16*/
17
18#ifndef __KOS_TLS_H
19#define __KOS_TLS_H
20
21#include <kos/cdefs.h>
22
23__BEGIN_DECLS
24
25#include <sys/queue.h>
26
27/** \brief Thread-local storage key type. */
28typedef int kthread_key_t;
29
30/** \brief Thread-local storage key-value pair.
31
32 This is the structure that is actually used to store the specific value for
33 a thread for a single TLS key.
34
35 You will not end up using these directly at all in programs, as they are
36 only used internally.
37*/
38typedef struct kthread_tls_kv {
39 /** \brief List handle -- NOT a function. */
40 LIST_ENTRY(kthread_tls_kv) kv_list;
41
42 /** \brief The key associated with this data. */
44
45 /** \brief The value of the data. */
46 void *data;
47
48 /** \brief Optional destructor for the value (set per key). */
49 void (*destructor)(void *);
51
52/** \cond */
53/* TLS Key-Value pair list type. */
54LIST_HEAD(kthread_tls_kv_list, kthread_tls_kv);
55/** \endcond */
56
57/** \brief Create a new thread-local storage key.
58
59 This function creates a new thread-local storage key that shall be visible
60 to all threads. Each thread is then responsible for associating any data
61 with the key that it deems fit (by default a thread will have no data
62 associated with a newly created key).
63
64 \param key The key to use.
65 \param destructor A destructor for use with this key. If it is non-NULL,
66 and a value associated with the key is non-NULL at
67 thread exit, then the destructor will be called with the
68 value as its argument.
69
70 \retval 0 On success.
71 \retval -1 On error, sets errno as appropriate.
72
73 \par Error Conditions:
74 \em EPERM - Called inside an interrupt, destructor is not NULL, and there
75 is already a malloc call or destructor operation in progress.
76 \em ENOMEM - Out of memory.
77
78*/
80
81/** \brief Retrieve a value associated with a TLS key.
82
83 This function retrieves the thread-specific data associated with the given
84 key.
85
86 \param key The key to look up data for.
87 \return The data associated with the key, or NULL if the key is not valid or
88 no data has been set in the current thread.
89*/
91
92/** \brief Set thread specific data for a key.
93
94 This function sets the thread-specific data associated with the given key.
95
96 \param key The key to set data for.
97 \param value The thread-specific value to use.
98
99 \retval 0 On success.
100 \retval -1 On error, sets errno as appropriate.
101
102 \par Error Conditions:
103 \em EINVAL - The key is not valid.
104 \em EPERM - Called inside an interrupt, the key needs to be created, and there
105 is already a malloc call or destructor operation in progress.
106 \em ENOMEM - Out of memory.
107*/
108int kthread_setspecific(kthread_key_t key, const void *value);
109
110/** \brief Delete a TLS key.
111
112 This function deletes a TLS key, removing all threads' values for the given
113 key. This function <em>does not</em> cause any destructors to be called.
114
115 \param key The key to delete.
116
117 \retval 0 On success.
118 \retval -1 On error, sets errno as appropriate.
119
120 \par Error Conditions:
121 \em EINVAL - The key is not valid.
122 \em EPERM - Called inside an interrupt, and there is already a malloc
123 call or destructor operation in progress.
124*/
126
127/** \cond */
128/* Initialization and shutdown. Once again, internal use only. */
129int kthread_tls_init(void);
130void kthread_tls_shutdown(void);
131/** \endcond */
132
133__END_DECLS
134
135#endif /* __KOS_TLS_H */
Various common macros used throughout the codebase.
int key
Definition nehe26.c:48
typedef LIST_HEAD(nmmgr_list, nmmgr_handler) nmmgr_list_t
Name handler list type.
Thread-local storage key-value pair.
Definition tls.h:38
kthread_key_t key
The key associated with this data.
Definition tls.h:43
void * data
The value of the data.
Definition tls.h:46
LIST_ENTRY(kthread_tls_kv) kv_list
List handle – NOT a function.
void * kthread_getspecific(kthread_key_t key)
Retrieve a value associated with a TLS key.
int kthread_key_create(kthread_key_t *key, void(*destructor)(void *))
Create a new thread-local storage key.
int kthread_key_delete(kthread_key_t key)
Delete a TLS key.
int kthread_setspecific(kthread_key_t key, const void *value)
Set thread specific data for a key.
int kthread_key_t
Thread-local storage key type.
Definition tls.h:28
static void destructor(void *data)
Definition tls_test.c:35