LCOV - code coverage report
Current view: top level - tests - perThread.cpp (source / functions) Hit Total Coverage
Test: Lunchbox Lines: 37 37 100.0 %
Date: 2016-03-29 17:09:06 Functions: 7 8 87.5 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2011-2014, Stefan Eilemann <eile@eyescale.ch>
       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             : #ifdef WARN_DEPRECATED
      19             : #  undef WARN_DEPRECATED
      20             : #endif
      21             : 
      22             : #include <pthread.h>
      23             : #include <test.h>
      24             : 
      25             : #include <lunchbox/atomic.h>
      26             : #include <lunchbox/perThread.h>
      27             : #include <lunchbox/sleep.h>
      28             : #include <lunchbox/rng.h>
      29             : 
      30             : #define NTHREADS 1024
      31             : 
      32           1 : lunchbox::a_int32_t _threads;
      33             : 
      34        1024 : void destructor( size_t* ) { --_threads; }
      35             : 
      36           1 : lunchbox::PerThread< size_t, &destructor > _tls1;
      37           1 : lunchbox::PerThread< size_t > _tls2;
      38           1 : lunchbox::PerThread< size_t, &destructor > _tls3;
      39             : 
      40             : 
      41        1024 : class Thread : public lunchbox::Thread
      42             : {
      43             : public:
      44        1024 :     virtual ~Thread() {}
      45             : 
      46        1024 :     virtual void run()
      47             :         {
      48        1024 :             ++_threads;
      49        1024 :             lunchbox::RNG rng;
      50             : 
      51        1024 :             size_t i = rng.get< size_t >();
      52        1024 :             size_t* data = new size_t( i );
      53        1024 :             _tls1 = data;
      54        1024 :             _tls2 = data;
      55        1024 :             _tls3 = _tls1;
      56             : 
      57        1024 :             TEST( _tls1.get() == data );
      58        1024 :             TEST( *_tls1 == i );
      59        1024 :             TEST( _tls2.get() == data );
      60        1024 :             TEST( *_tls2 == i );
      61        1024 :             TEST( *_tls1 == *_tls2 );
      62        1024 :             TEST( *_tls1 == *_tls3 );
      63             : 
      64        1024 :             _tls3 = 0;
      65        1024 :             TEST( _tls3 == 0 );
      66        1024 :         }
      67             : };
      68             : 
      69             : 
      70           1 : int main( int argc, char **argv )
      71             : {
      72           1 :     TEST( lunchbox::init( argc, argv ));
      73             : 
      74        1025 :     Thread threads[NTHREADS];
      75           1 :     TESTINFO( _threads == 0, _threads );
      76             : 
      77        1025 :     for( size_t i=0; i<NTHREADS; ++i )
      78        1024 :         TEST( threads[i].start( ));
      79        1025 :     for( size_t i=0; i<NTHREADS; ++i )
      80        1024 :         TEST( threads[i].join( ));
      81             : 
      82           1 :     lunchbox::sleep( 10 ); // ms, needed for tls exit handlers to run
      83           1 :     TESTINFO( _threads == 0, _threads );
      84           1 :     TEST( lunchbox::exit( ));
      85        1025 :     return EXIT_SUCCESS;
      86           3 : }

Generated by: LCOV version 1.11