LCOV - code coverage report
Current view: top level - lunchbox - tls.cpp (source / functions) Hit Total Coverage
Test: Lunchbox Lines: 23 26 88.5 %
Date: 2018-10-03 05:33:11 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          25 :     explicit TLS(lunchbox::TLS::ThreadDestructor_t df)
      31          25 :         : dtorFunc(df)
      32             :     {
      33          25 :     }
      34             : 
      35             :     pthread_key_t key;
      36             :     const lunchbox::TLS::ThreadDestructor_t dtorFunc;
      37             : };
      38             : }
      39             : 
      40          25 : TLS::TLS(ThreadDestructor_t dtorFunc)
      41          25 :     : impl_(new detail::TLS(dtorFunc))
      42             : {
      43          25 :     const int error = pthread_key_create(&impl_->key, dtorFunc);
      44          25 :     if (error)
      45             :     {
      46           0 :         LBERROR << "Can't create thread-specific key: " << strerror(error)
      47           0 :                 << std::endl;
      48           0 :         LBASSERT(!error);
      49             :     }
      50          25 : }
      51             : 
      52          50 : TLS::~TLS()
      53             : {
      54          25 :     void* data = get();
      55          25 :     if (data && impl_->dtorFunc)
      56           3 :         impl_->dtorFunc(data);
      57             : 
      58          25 :     pthread_key_delete(impl_->key);
      59          25 :     delete impl_;
      60          25 : }
      61             : 
      62        7398 : void TLS::set(const void* data)
      63             : {
      64        7398 :     pthread_setspecific(impl_->key, data);
      65        7403 : }
      66             : 
      67       22628 : void* TLS::get()
      68             : {
      69       22628 :     return pthread_getspecific(impl_->key);
      70             : }
      71             : 
      72        2048 : const void* TLS::get() const
      73             : {
      74        2048 :     return pthread_getspecific(impl_->key);
      75             : }
      76          75 : }

Generated by: LCOV version 1.11