00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00047 #ifndef UNIXSTL_INCL_UNIXSTL_SYNCH_HPP_TSS_INDEX
00048 #define UNIXSTL_INCL_UNIXSTL_SYNCH_HPP_TSS_INDEX
00049
00050 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00051 # define UNIXSTL_VER_UNIXSTL_SYNCH_HPP_TSS_INDEX_MAJOR 3
00052 # define UNIXSTL_VER_UNIXSTL_SYNCH_HPP_TSS_INDEX_MINOR 1
00053 # define UNIXSTL_VER_UNIXSTL_SYNCH_HPP_TSS_INDEX_REVISION 3
00054 # define UNIXSTL_VER_UNIXSTL_SYNCH_HPP_TSS_INDEX_EDIT 49
00055 #endif
00056
00057
00058
00059
00060
00061 #ifndef UNIXSTL_INCL_UNIXSTL_H_UNIXSTL
00062 # include <unixstl/unixstl.h>
00063 #endif
00064 #ifndef UNIXSTL_INCL_UNIXSTL_SYNCH_UTIL_H_FEATURES
00065 # include <unixstl/synch/util/features.h>
00066 #endif
00067 #ifndef UNIXSTL_USING_PTHREADS
00068 # error unixstl/synch/tss_index.hpp cannot be included in non-multithreaded compilation. _REENTRANT and/or _POSIX_THREADS must be defined
00069 #endif
00070 #ifndef STLSOFT_CF_EXCEPTION_SUPPORT
00071 # error unixstl/synch/tss_index.hpp cannot be compiled without exception-support enabled
00072 #endif
00073 #ifndef UNIXSTL_INCL_UNIXSTL_SYNCH_ERROR_HPP_EXCEPTIONS
00074 # include <unixstl/synch/error/exceptions.hpp>
00075 #endif
00076
00077 #ifndef STLSOFT_INCL_H_PTHREAD
00078 # define STLSOFT_INCL_H_PTHREAD
00079 # include <pthread.h>
00080 #endif
00081
00082
00083
00084
00085
00086 #ifndef _UNIXSTL_NO_NAMESPACE
00087 # if defined(_STLSOFT_NO_NAMESPACE) || \
00088 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00089
00090 namespace unixstl
00091 {
00092 # else
00093
00094
00095 namespace stlsoft
00096 {
00097
00098 namespace unixstl_project
00099 {
00100
00101 # endif
00102 #endif
00103
00104
00105
00106
00107
00108 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00109
00115 class tss_exception
00116 : public synchronisation_exception
00117 {
00120 public:
00121 typedef synchronisation_exception parent_class_type;
00122 typedef tss_exception class_type;
00123 typedef parent_class_type::error_code_type error_code_type;
00125
00128 public:
00129 ss_explicit_k tss_exception(error_code_type err)
00130 : parent_class_type("", err)
00131 {}
00133
00136 public:
00137 virtual char const* what() const stlsoft_throw_0()
00138 {
00139 return "Failed to allocate a TSS key";
00140 }
00142
00145 private:
00146 class_type& operator =(class_type const&);
00148 };
00149 #endif
00150
00151
00156 class tss_index
00157 {
00160 public:
00162 typedef tss_index class_type;
00164 typedef pthread_key_t key_type;
00168 typedef key_type index_type;
00170 typedef void* value_type;
00172
00175 public:
00177 ss_explicit_k tss_index()
00178 : m_index(index_create_())
00179 {}
00181 ~tss_index() stlsoft_throw_0()
00182 {
00183 index_destroy_(m_index);
00184 }
00186
00189 public:
00191 void set_value(value_type value)
00192 {
00193 class_type::set_slot_value_(m_index, value);
00194 }
00196 value_type get_value() const
00197 {
00198 return class_type::get_slot_value_(m_index);
00199 }
00201
00204 public:
00206 operator key_type () const
00207 {
00208 return m_index;
00209 }
00211
00214 private:
00215 static key_type index_create_()
00216 {
00217 key_type key;
00218 int res = ::pthread_key_create(&key, NULL);
00219
00220 if(0 != res)
00221 {
00222 STLSOFT_THROW_X(tss_exception(res));
00223 }
00224
00225 return key;
00226 }
00227
00228 static void index_destroy_(key_type key)
00229 {
00230 ::pthread_key_delete(key);
00231 }
00232
00233 static void set_slot_value_(key_type key, value_type value)
00234 {
00235 ::pthread_setspecific(key, value);
00236 }
00237
00238 static value_type get_slot_value_(key_type key)
00239 {
00240 return ::pthread_getspecific(key);
00241 }
00243
00246 private:
00247 key_type m_index;
00249
00252 private:
00253 tss_index(class_type const&);
00254 class_type& operator =(class_type const&);
00256 };
00257
00258
00259
00260 #ifndef _UNIXSTL_NO_NAMESPACE
00261 # if defined(_STLSOFT_NO_NAMESPACE) || \
00262 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00263 }
00264 # else
00265 }
00266 }
00267 # endif
00268 #endif
00269
00270
00271
00272 #endif
00273
00274