LCOV - code coverage report
Current view: top level - lunchbox - tls.cpp (source / functions) Hit Total Coverage
Test: lcov2.info Lines: 21 24 87.5 %
Date: 2014-08-05 Functions: 8 8 100.0 %

          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          32 :     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          32 : TLS::TLS( ThreadDestructor_t dtorFunc )
      38          32 :     : impl_( new detail::TLS( dtorFunc ))
      39             : {
      40          32 :     const int error = pthread_key_create( &impl_->key, dtorFunc );
      41          32 :     if( error )
      42             :     {
      43           0 :         LBERROR << "Can't create thread-specific key: "
      44           0 :                 << strerror( error ) << std::endl;
      45           0 :         LBASSERT( !error );
      46             :     }
      47          32 : }
      48             : 
      49          32 : TLS::~TLS()
      50             : {
      51          32 :     void* data = get();
      52          32 :     if( data && impl_->dtorFunc )
      53           4 :         impl_->dtorFunc( data );
      54             : 
      55          32 :     pthread_key_delete( impl_->key );
      56          32 :     delete impl_;
      57          32 : }
      58             : 
      59       11063 : void TLS::set( const void* data )
      60             : {
      61       11063 :     pthread_setspecific( impl_->key, data );
      62       11061 : }
      63             : 
      64       32041 : void* TLS::get()
      65             : {
      66       32041 :     return pthread_getspecific( impl_->key );
      67             : }
      68             : 
      69        2047 : const void* TLS::get() const
      70             : {
      71        2047 :     return pthread_getspecific( impl_->key );
      72             : }
      73             : 
      74          87 : }

Generated by: LCOV version 1.10