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 <sys/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/** \cond */
58/* Retrieve the next key value (i.e, what key the next kthread_key_create will
59 use). This function is not meant for external use (although it won't really
60 hurt anything for you to call it). */
61kthread_key_t kthread_key_next(void);
62/** \endcond */
63
64/** \brief Create a new thread-local storage key.
65
66 This function creates a new thread-local storage key that shall be visible
67 to all threads. Each thread is then responsible for associating any data
68 with the key that it deems fit (by default a thread will have no data
69 associated with a newly created key).
70
71 \param key The key to use.
72 \param destructor A destructor for use with this key. If it is non-NULL,
73 and a value associated with the key is non-NULL at
74 thread exit, then the destructor will be called with the
75 value as its argument.
76 \retval -1 On failure, and sets errno to one of the following: EPERM if
77 called inside an interrupt and another call is in progress,
78 ENOMEM if out of memory.
79 \retval 0 On success.
80*/
81int kthread_key_create(kthread_key_t *key, void (*destructor)(void *));
82
83/** \brief Retrieve a value associated with a TLS key.
84
85 This function retrieves the thread-specific data associated with the given
86 key.
87
88 \param key The key to look up data for.
89 \return The data associated with the key, or NULL if the key is not valid or
90 no data has been set in the current thread.
91*/
93
94/** \brief Set thread specific data for a key.
95
96 This function sets the thread-specific data associated with the given key.
97
98 \param key The key to set data for.
99 \param value The thread-specific value to use.
100 \retval -1 On failure, and sets errno to one of the following: EINVAL
101 if the key is not valid, ENOMEM if out of memory, or EPERM
102 if called inside an interrupt and another call is in
103 progress.
104 \retval 0 On success.
105*/
106int kthread_setspecific(kthread_key_t key, const void *value);
107
108/** \brief Delete a TLS key.
109
110 This function deletes a TLS key, removing all threads' values for the given
111 key. This function <em>does not</em> cause any destructors to be called.
112
113 \param key The key to delete.
114 \retval -1 On failure, and sets errno to one of the following: EINVAL
115 if the key is invalid, EPERM if unsafe to call free.
116 \retval 0 On success.
117*/
119
120/** \cond */
121/* Delete the destructor for a given key. This function is for internal use
122 only! */
123void kthread_key_delete_destructor(kthread_key_t key);
124
125/* Initialization and shutdown. Once again, internal use only. */
126int kthread_tls_init(void);
127void kthread_tls_shutdown(void);
128/** \endcond */
129
130__END_DECLS
131
132#endif /* __KOS_TLS_H */
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