LCOV - code coverage report
Current view: top level - snaplogger - guard.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 9 9 100.0 %
Date: 2019-08-13 00:35:33 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * License:
       3             :  *    Copyright (c) 2013-2019  Made to Order Software Corp.  All Rights Reserved
       4             :  *
       5             :  *    https://snapwebsites.org/
       6             :  *    contact@m2osw.com
       7             :  *
       8             :  *    This program is free software; you can redistribute it and/or modify
       9             :  *    it under the terms of the GNU General Public License as published by
      10             :  *    the Free Software Foundation; either version 2 of the License, or
      11             :  *    (at your option) any later version.
      12             :  *
      13             :  *    This program is distributed in the hope that it will be useful,
      14             :  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :  *    GNU General Public License for more details.
      17             :  *
      18             :  *    You should have received a copy of the GNU General Public License along
      19             :  *    with this program; if not, write to the Free Software Foundation, Inc.,
      20             :  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      21             :  *
      22             :  * Authors:
      23             :  *    Alexis Wilke   alexis@m2osw.com
      24             :  */
      25             : 
      26             : /** \file
      27             :  * \brief Create a guard in all functions that need to run on their own.
      28             :  *
      29             :  * This library is thread safe. It uses one mutex to guard any access
      30             :  * which requires safe access:
      31             :  *
      32             :  * \code
      33             :  *     {
      34             :  *         guard        g;
      35             :  *
      36             :  *         ...do multi-thread safe work...
      37             :  *     }
      38             :  * \endcode
      39             :  */
      40             : 
      41             : // self
      42             : //
      43             : #include    "snaplogger/guard.h"
      44             : 
      45             : 
      46             : // cppthread lib
      47             : //
      48             : #include    <cppthread/guard.h>
      49             : #include    <cppthread/mutex.h>
      50             : 
      51             : 
      52             : // last include
      53             : //
      54             : #include    <snapdev/poison.h>
      55             : 
      56             : 
      57             : 
      58             : namespace snaplogger
      59             : {
      60             : 
      61             : 
      62             : 
      63             : namespace
      64             : {
      65             : 
      66             : 
      67             : 
      68             : cppthread::mutex *      g_mutex = nullptr;
      69             : 
      70             : 
      71             : 
      72             : }
      73             : // no name namespace
      74             : 
      75             : 
      76             : 
      77             : 
      78             : 
      79      525932 : guard::guard()
      80             : {
      81             :     {
      82             :         // we know for sure that g_system_mutex was already initialized
      83             :         // so we can use it here
      84             :         //
      85     1051864 :         cppthread::guard lock(*cppthread::g_system_mutex);
      86             : 
      87      525932 :         if(g_mutex == nullptr)
      88             :         {
      89           2 :             g_mutex = new cppthread::mutex;
      90             :         }
      91             :     }
      92             : 
      93      525932 :     g_mutex->lock();
      94      525932 : }
      95             : 
      96             : 
      97      525932 : guard::~guard()
      98             : {
      99      525932 :     g_mutex->unlock();
     100      525932 : }
     101             : 
     102             : 
     103             : 
     104             : } // snaplogger namespace
     105             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12