Line data Source code
1 :
2 : /* Copyright (c) 2013, Stefan Eilemann <eile@equalizergraphics.com>
3 : *
4 : * This library is free software; you can redistribute it and/or modify it under
5 : * the terms of the GNU Lesser General Public License version 2.1 as published
6 : * by the Free Software Foundation.
7 : *
8 : * This library is distributed in the hope that it will be useful, but WITHOUT
9 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 : * details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this library; if not, write to the Free Software Foundation, Inc.,
15 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 : */
17 :
18 : #include "tls.h"
19 : #include "debug.h"
20 : #include <cstring>
21 : #include <pthread.h>
22 :
23 : namespace lunchbox
24 : {
25 : namespace detail
26 : {
27 : class TLS
28 : {
29 : public:
30 27 : explicit TLS( lunchbox::TLS::ThreadDestructor_t df ) : dtorFunc ( df ) {}
31 :
32 : pthread_key_t key;
33 : const lunchbox::TLS::ThreadDestructor_t dtorFunc;
34 : };
35 : }
36 :
37 27 : TLS::TLS( ThreadDestructor_t dtorFunc )
38 27 : : impl_( new detail::TLS( dtorFunc ))
39 : {
40 27 : const int error = pthread_key_create( &impl_->key, dtorFunc );
41 27 : if( error )
42 : {
43 0 : LBERROR << "Can't create thread-specific key: "
44 0 : << strerror( error ) << std::endl;
45 0 : LBASSERT( !error );
46 : }
47 27 : }
48 :
49 54 : TLS::~TLS()
50 : {
51 27 : void* data = get();
52 27 : if( data && impl_->dtorFunc )
53 3 : impl_->dtorFunc( data );
54 :
55 27 : pthread_key_delete( impl_->key );
56 27 : delete impl_;
57 27 : }
58 :
59 7370 : void TLS::set( const void* data )
60 : {
61 7370 : pthread_setspecific( impl_->key, data );
62 7386 : }
63 :
64 22624 : void* TLS::get()
65 : {
66 22624 : return pthread_getspecific( impl_->key );
67 : }
68 :
69 2048 : const void* TLS::get() const
70 : {
71 2048 : return pthread_getspecific( impl_->key );
72 : }
73 :
74 72 : }
|