2013-11-27 23:40:37 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#pragma once
|
2013-12-09 22:56:48 +00:00
|
|
|
#include "util/thread.h"
|
2013-11-27 23:40:37 +00:00
|
|
|
namespace lean {
|
|
|
|
/**
|
|
|
|
\brief The class \c unlock_guard is a mutex wrapper that provides a
|
|
|
|
convenient RAII-style mechanism for releasing a mutex for the
|
|
|
|
duration of a scoped block.
|
|
|
|
|
|
|
|
It is the dual of lock_guard in the standard library.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
<code>
|
|
|
|
{
|
2013-12-09 22:56:48 +00:00
|
|
|
lock_guard<mutex> lock(m);
|
2013-11-27 23:40:37 +00:00
|
|
|
...
|
|
|
|
{
|
|
|
|
unlock_guard unlock(m);
|
|
|
|
...
|
|
|
|
}
|
|
|
|
...
|
|
|
|
}
|
|
|
|
</code>
|
|
|
|
|
|
|
|
\warning The calling thread must own the lock to m_mutex
|
|
|
|
*/
|
|
|
|
class unlock_guard {
|
2014-05-01 21:10:57 +00:00
|
|
|
mutex & m_mutex;
|
2013-11-27 23:40:37 +00:00
|
|
|
public:
|
2014-05-01 21:10:57 +00:00
|
|
|
explicit unlock_guard(mutex & m):m_mutex(m) { m_mutex.unlock(); }
|
2013-11-27 23:40:37 +00:00
|
|
|
unlock_guard(unlock_guard const &) = delete;
|
|
|
|
unlock_guard(unlock_guard &&) = delete;
|
|
|
|
unlock_guard & operator=(unlock_guard const &) = delete;
|
|
|
|
unlock_guard & operator=(unlock_guard &&) = delete;
|
|
|
|
~unlock_guard() { m_mutex.lock(); }
|
|
|
|
};
|
|
|
|
}
|