LCOV - code coverage report
Current view: top level - util/test - spin_lock_test.cpp (source / functions) Hit Total Coverage
Test: CoherentDB code coverage Lines: 21 21 100.0 %
Date: 2011-02-13 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * (C) Copyright 2010 Marek Dopiera
       3             :  * 
       4             :  * This file is part of CoherentDB.
       5             :  * 
       6             :  * CoherentDB is free software: you can redistribute it and/or modify it
       7             :  * under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation, either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  * 
      11             :  * CoherentDB is distributed in the hope that it will be useful, but
      12             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      14             :  * General Public License for more details.
      15             :  * 
      16             :  * You should have received a copy of the GNU General Public
      17             :  * License along with CoherentDB. If not, see
      18             :  * http://www.gnu.org/licenses/.
      19             :  */
      20             : 
      21             : #include <errno.h>
      22             : 
      23             : #include <util/misc.h>
      24             : #include <util/thread.h>
      25             : #include <debug/debug.h>
      26             : #include <config/config.h>
      27             : #include <log/log.h>
      28             : 
      29             : using namespace std;
      30             : using namespace coherent::util;
      31             : using namespace coherent::config;
      32             : using namespace coherent::log;
      33             : 
      34             : namespace coherent {
      35             : namespace util {
      36             : namespace unittests {
      37             : 
      38             : volatile int k = 0;
      39           1 : spin_mutex mutex;
      40             : 
      41             : unsigned const num_iteratrions = 10000000 / VALGRIND_SLOWDOWN / VALGRIND_SLOWDOWN;
      42             : 
      43           2 : void *incrementer(void *)
      44             : {
      45             :         LOG(TRACE, "Thread started");
      46    19998648 :         for (unsigned i = 0; i < num_iteratrions; ++i) {
      47    19998646 :                 spin_mutex::scoped_lock l(mutex);
      48    20000000 :                 volatile int j = k;
      49    20000000 :                 j++;
      50    20000000 :                 k = j;
      51             :         }
      52           2 :         return NULL;
      53             : }
      54             : 
      55           1 : int start_test(const int argc, const char *const *const argv)
      56             : {
      57           2 :         scoped_test_enabler test_setup(argc, argv);
      58             :         
      59           1 :         log4cxx::Logger::getLogger("coherent.util.unittests")->setLevel(coherent::log::log_TRACE); 
      60             : 
      61             :         int err;
      62           1 :         pthread_t thread1 = 0, thread2 = 0;
      63             :         {
      64           2 :                 spin_mutex::scoped_lock l(mutex);
      65           1 :                 err = pthread_create(&thread1, NULL, incrementer, NULL);
      66             :                 r_assert(err == 0, "failed to create thread, errno=" << errno);
      67           1 :                 err = pthread_create(&thread2, NULL, incrementer, NULL);
      68             :                 r_assert(err == 0, "failed to create thread, errno=" << errno);
      69             :         }
      70           1 :         err = pthread_join(thread1, NULL);
      71             :         r_assert(err == 0, "failed to join thread, errno=" << errno);
      72           1 :         err = pthread_join(thread2, NULL);
      73             :         r_assert(err == 0, "failed to join thread, errno=" << errno);
      74             : 
      75             :         r_assert(k == num_iteratrions * 2, "Expected k=" << num_iteratrions * 2 << ", but k=" << k);
      76           1 :         return 0;
      77             : }
      78             : 
      79             : } // namespace unittests
      80             : } // namespace util
      81             : } // namespace coherent
      82             : 
      83           1 : int main(const int argc, const char * const * const argv)
      84             : {
      85           1 :         return coherent::util::unittests::start_test(argc, argv);
      86           3 : }

Generated by: LCOV version 1.9