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 33 : 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 33 : TLS::TLS( ThreadDestructor_t dtorFunc )
38 33 : : impl_( new detail::TLS( dtorFunc ))
39 : {
40 33 : const int error = pthread_key_create( &impl_->key, dtorFunc );
41 33 : if( error )
42 : {
43 0 : LBERROR << "Can't create thread-specific key: "
44 0 : << strerror( error ) << std::endl;
45 0 : LBASSERT( !error );
46 : }
47 33 : }
48 :
49 33 : TLS::~TLS()
50 : {
51 33 : void* data = get();
52 33 : if( data && impl_->dtorFunc )
53 4 : impl_->dtorFunc( data );
54 :
55 33 : pthread_key_delete( impl_->key );
56 33 : delete impl_;
57 33 : }
58 :
59 11044 : void TLS::set( const void* data )
60 : {
61 11044 : pthread_setspecific( impl_->key, data );
62 11056 : }
63 :
64 32027 : void* TLS::get()
65 : {
66 32027 : 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 90 : }
|