LCOV - code coverage report
Current view: top level - lunchbox - spinLock.cpp (source / functions) Hit Total Coverage
Test: Lunchbox Lines: 26 65 40.0 %
Date: 2015-07-07 14:54:45 Functions: 12 24 50.0 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2012, 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             : #include "spinLock.h"
      19             : #include <lunchbox/atomic.h>
      20             : #include <lunchbox/thread.h>
      21             : 
      22             : namespace lunchbox
      23             : {
      24             : namespace
      25             : {
      26             : static const long _writelocked = -1;
      27             : static const long _unlocked = 0;
      28             : }
      29             : namespace detail
      30             : {
      31             : class SpinLock
      32             : {
      33             : public:
      34           1 :     SpinLock() : _state( _unlocked ) {}
      35           1 :     ~SpinLock() { _state = _unlocked; }
      36             : 
      37          26 :     inline void set()
      38             :         {
      39             :             while( true )
      40             :             {
      41          26 :                 if( trySet( ))
      42          52 :                     return;
      43           0 :                 lunchbox::Thread::yield();
      44             :             }
      45             :         }
      46             : 
      47          26 :     inline void unset()
      48             :         {
      49          26 :             LBASSERT( _state == _writelocked );
      50          26 :             _state = _unlocked;
      51          26 :         }
      52             : 
      53          26 :     inline bool trySet()
      54             :         {
      55          26 :             if( !_state.compareAndSwap( _unlocked, _writelocked ))
      56           0 :                 return false;
      57          26 :             LBASSERTINFO( isSetWrite(), _state );
      58          26 :             return true;
      59             :         }
      60             : 
      61           0 :     inline void setRead()
      62             :         {
      63             :             while( true )
      64             :             {
      65           0 :                 if( trySetRead( ))
      66           0 :                     return;
      67           0 :                 lunchbox::Thread::yield();
      68             :             }
      69             :         }
      70             : 
      71           0 :     inline void unsetRead()
      72             :         {
      73             :             while( true )
      74             :             {
      75           0 :                 LBASSERT( _state > _unlocked );
      76           0 :                 memoryBarrier();
      77           0 :                 const int32_t expected = _state;
      78           0 :                 if( _state.compareAndSwap( expected, expected-1 ))
      79           0 :                     return;
      80           0 :             }
      81             :         }
      82             : 
      83           0 :     inline bool trySetRead()
      84             :         {
      85           0 :             memoryBarrier();
      86           0 :             const int32_t state = _state;
      87             :             // Note: 0 used here since using _unlocked unexplicably gives
      88             :             //       'undefined reference to lunchbox::SpinLock::_unlocked'
      89           0 :             const int32_t expected = (state==_writelocked) ? 0 : state;
      90             : 
      91           0 :             if( !_state.compareAndSwap( expected, expected+1 ))
      92           0 :                 return false;
      93             : 
      94           0 :             LBASSERTINFO( isSetRead(), _state << ", " << expected );
      95           0 :             return true;
      96             :         }
      97             : 
      98           0 :     inline bool isSet() { return ( _state != _unlocked ); }
      99          26 :     inline bool isSetWrite() { return ( _state == _writelocked ); }
     100           0 :     inline bool isSetRead() { return ( _state > _unlocked ); }
     101             : 
     102             : private:
     103             :     a_int32_t _state;
     104             : };
     105             : }
     106             : 
     107           1 : SpinLock::SpinLock()
     108           1 :         : _impl( new detail::SpinLock ) {}
     109             : 
     110           1 : SpinLock::~SpinLock()
     111             : {
     112           1 :     delete _impl;
     113           1 : }
     114             : 
     115          26 : void SpinLock::set()
     116             : {
     117          26 :     _impl->set();
     118          26 : }
     119             : 
     120          26 : void SpinLock::unset()
     121             : {
     122          26 :     _impl->unset();
     123          26 : }
     124             : 
     125           0 : bool SpinLock::trySet()
     126             : {
     127           0 :     return _impl->trySet();
     128             : }
     129             : 
     130           0 : void SpinLock::setRead()
     131             : {
     132           0 :     _impl->setRead();
     133           0 : }
     134             : 
     135           0 : void SpinLock::unsetRead()
     136             : {
     137           0 :     _impl->unsetRead();
     138           0 : }
     139             : 
     140           0 : bool SpinLock::trySetRead()
     141             : {
     142           0 :     return _impl->trySetRead();
     143             : }
     144             : 
     145           0 : bool SpinLock::isSet()
     146             : {
     147           0 :     return _impl->isSet();
     148             : }
     149             : 
     150           0 : bool SpinLock::isSetWrite()
     151             : {
     152           0 :     return _impl->isSetWrite();
     153             : }
     154             : 
     155           0 : bool SpinLock::isSetRead()
     156             : {
     157           0 :     return _impl->isSetRead();
     158             : }
     159             : 
     160          81 : }

Generated by: LCOV version 1.11