tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
thread_local.hpp
Go to the documentation of this file.
1 /**
2  * Copyright (C) 2011
3  * University of Rochester Department of Computer Science
4  * and
5  * Lehigh University Department of Computer Science and Engineering
6  *
7  * License: Modified BSD
8  * Please see the file LICENSE.RSTM for licensing information
9  */
10 
11 #ifndef THREADLOCAL_HPP__
12 #define THREADLOCAL_HPP__
13 
14 /**
15  * To use a ThreadLocalPointer, declare one in a header like this:
16  * extern ThreadLocalPointer<foo> my_foo;
17  *
18  * Then you must back the pointer in a cpp file like this:
19  * ThreadLocalPointer<foo> currentHeap;
20  * #if defined(LOCAL_POINTER_ANNOTATION)
21  * template <> LOCAL_POINTER_ANNOTATION foo*
22  * ThreadLocalPointer<foo>::thr_local_key = NULL;
23  * #endif
24  */
25 
26 /**
27  * This #if block serves three roles:
28  * - It includes files as necessary for different pthread implementations
29  * - It hides differences between VisualC++ and GCC syntax
30  * - It makes sure that a valid thread-local option is specified so that
31  * subsequent #ifs don't have to do error checking
32  */
33 
34 #if defined(__GNUC__)
35 #define LOCAL_POINTER_ANNOTATION __thread
36 #elif defined(_MSC_VER)
37 #include <windows.h>
38 #define LOCAL_POINTER_ANNOTATION __declspec(thread)
39 #else
40 #include <pthread.h>
41 #endif
42 
43  /**
44  * Hide the fact that some of our platforms use pthread_getspecific while
45  * others use os-specific thread local storage mechanisms. In all cases, if
46  * you instantiate one of these with a T, then you'll get a thread-local
47  * pointer to a T that is easy to access and platform independent
48  */
49  template<class T>
51  {
52  /**
53  * either declare a key for interfacing to PTHREAD thread local
54  * storage, or else declare a static thread-local var of type T*
55  */
56 #if defined(TLS_PTHREAD)
57  pthread_key_t thr_local_key;
58 #else
59  static LOCAL_POINTER_ANNOTATION T* thr_local_key;
60 #endif
61 
62  public:
63  /**
64  * Perform global initialization of the thread key, if applicable. Note
65  * that we have a default value of NULL. You can't store a NULL value
66  * in this thread-local pointer if you want to do subsequent tests to
67  * ensure that the variable is initialized for your thread.
68  */
70  {
71 #if defined(TLS_PTHREAD)
72  pthread_key_create(&thr_local_key, NULL);
73 #else
74  thr_local_key = NULL;
75 #endif
76  }
77 
78  /*** Free the thread local key or set the pointer to NULL */
80  {
81 #if defined(TLS_PTHREAD)
82  pthread_key_delete(thr_local_key);
83 #else
84  thr_local_key = NULL;
85 #endif
86  }
87 
88  /*** Get the pointer stored for this thread */
89  T* get() const
90  {
91 #if defined(TLS_PTHREAD)
92  return static_cast<T* const>(pthread_getspecific(thr_local_key));
93 #else
94  return thr_local_key;
95 #endif
96  }
97 
98  /*** Set this thread's pointer */
99  void set(T* val)
100  {
101 #if defined(TLS_PTHREAD)
102  pthread_setspecific(thr_local_key, (void*)val);
103 #else
104  thr_local_key = val;
105 #endif
106  }
107 
108  /*** operators for dereferencing */
109  T* operator->() const { return get(); }
110  T& operator*() const { return *get(); }
111  };
112 
113 #endif // THREADLOCAL_HPP__
ThreadLocalPointer()
Definition: thread_local.hpp:69
~ThreadLocalPointer()
Definition: thread_local.hpp:79
static LOCAL_POINTER_ANNOTATION T * thr_local_key
Definition: thread_local.hpp:59
T * operator->() const
Definition: thread_local.hpp:109
void set(T *val)
Definition: thread_local.hpp:99
T & operator*() const
Definition: thread_local.hpp:110
Definition: thread_local.hpp:50