1  /*
   2   * \brief  Lock guard
   3   * \author Norman Feske
   4   * \date   2006-07-26
   5   */

   6  
   7  /*
   8   * Copyright (C) 2006-2013 Genode Labs GmbH
   9   *
  10   * This file is part of the Genode OS framework, which is distributed
  11   * under the terms of the GNU General Public License version 2.
  12   */

  13  
  14  #ifndef _INCLUDE__BASE__LOCK_GUARD_H_
  15  #define _INCLUDE__BASE__LOCK_GUARD_H_
  16  
  17  namespace Genode { template <typename> class Lock_guard; }
  18  
  19  
  20  /**
  21   * Lock guard template
  22   *
  23   * \param LT  lock type
  24   *
  25   * A lock guard is instantiated as a local variable. When a lock guard is
  26   * constructed, it acquires the lock that is specified as constructor argument.
  27   * When the control flow leaves the scope of the lock-guard variable via a
  28   * return statement or an exception, the lock guard`s destructor gets called,
  29   * freeing the lock.
  30   */

  31  template <typename LT>
  32  class Genode::Lock_guard
  33  {
  34     private:
  35  
  36        LT &_lock;

  37  
  38     public:
  39  
  40        explicit Lock_guard(LT &lock) : _lock(lock) { _lock.lock(); }
  41  
  42        ~Lock_guard() { _lock.unlock(); }

  43  }
;

  44  
  45  #endif /* _INCLUDE__BASE__LOCK_GUARD_H_ */