1/* 2 * Copyright 2007 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Niels Sascha Reedijk <niels.reedijk@gmail.com> 7 * Corresponds to: 8 * /trunk/headers/os/support/TLS.h rev 19972 9 */ 10 11/*! 12 \file TLS.h 13 \ingroup support 14 \brief Functions to use Thread Local Storage. 15 16 The Thread Local Storage API provides convenient methods to transform global 17 variables in to thread-context sensetive variables. Some applications rely on 18 global variables as a way of intercommunicating between functions and 19 objects, but one of your demands might be that the contents of that variable 20 differs between threads. 21 22 The following example demonstrates how an imaginary thread manager that 23 stores per thread data would function. The constructor of this 24 \c ThreadManager allocates the TLS variables using tls_allocate(). This only 25 has to be done once, and not in every spawned thread! Then, every spawned 26 thread that interacts with this thread manager, should call the 27 \c InitThread() function. This one associates the supplied thread data with 28 the TLS index using tls_set(). Each thread can get their associated data with 29 \c GetCurrentThreadData(), which uses tls_get() to retrieve the associated 30 thread data at the provided index. 31 32 \code 33int32 gThreadName; 34int32 gThreadData; 35 36class ThreadManager 37{ 38public: 39 // General initialisation 40 ThreadManager() { 41 gThreadName = tls_allocate(); 42 gThreadStatus = tls_allocate(); 43 }; 44 45 // Called from the thread entry function 46 void InitThread(const char *name, void *data) { 47 tls_set(gThreadName, (void *)name); 48 tls_set(gThreadData, data); 49 }; 50 51 // Can be called from any of the threads. The returned data will be that 52 // which the thread explicitly set in the InitThread() function 53 void *GetCurrentThreadData() { 54 printf("Thread %s asked for its data.\n", (const char*)tls_get(gThreadName)); 55 return tls_get(gThreadData); 56 }; 57}; 58 \endcode 59 60 \note 61 -# It is impossible to get data other than from your thread. 62 -# There is a limit to the number of TLS variables you can allocate. This 63 limit is define by #TLS_MAX_KEYS, but do realize that you share this 64 limit with all the libraries your application is linked to. 65 -# The actual global variables, in the example \c gThreadName and 66 \c gThreadData, are only indexes. You cannot use these variables to 67 access data without the TLS API. 68*/ 69 70/*! 71 \def TLS_MAX_KEYS 72 \brief The maximum number of thread local storage variables. This number is 73 process wide. 74*/ 75 76/*! 77 \fn int32 tls_allocate(void) 78 \brief Allocate a unique index to use for storing variables. 79 80 You should only have to do this once to allocate the global index, which you 81 can reuse in every thread. 82 83 \return A unique index to which you can associate per thread data. If we 84 overrun the maximum number of keys, as defined by #TLS_MAX_KEYS, the 85 function will return \c B_NO_MEMORY. 86 87 \see tls_get(), tls_set(), tls_address() 88*/ 89 90/*! 91 \fn void *tls_get(int32 index) 92 \brief Retrieve the data stored for this thread at the provided \a index. 93 94 \param index The \a index that you retrieved with tls_allocate(). 95 \return The data you set using tls_set() for this thread, or \c NULL if there 96 is no data set, or the \a index is invalid. 97 \see tls_allocate(), tls_set() 98*/ 99 100/*! 101 \fn void **tls_address(int32 index) 102 \brief Retrieve the pointer that refers to the data of this thread at the 103 provided \a index. 104 105 You can use this pointer to directly manipulate your thread data. 106 107 \param index The \a index that you retrieved with tls_allocate(). 108 \return The pointer to where your thread's data is, or \c NULL if the index 109 is invalid. 110 \see tls_allocate(), tls_set(), tls_get() 111*/ 112 113/*! 114 \fn void tls_set(int32 index, void *value) 115 \brief Set the data of this thread at the provided \a index. 116 117 It is up to you to make sure the \a index is valid. Any invalid indeces can 118 lead to unpredicable results. 119 120 \param index The \a index that you retrieved with tls_allocate(). 121 \param value The data that should be associated with the index for this 122 thread. 123 \see tls_allocate(), tls_get() 124*/ 125