update single-threaded invariant check for C++11 (#695)

This commit is contained in:
Arvid Norberg 2016-05-03 08:37:09 -04:00
parent 077e9bb10c
commit 8bf038eeb6
1 changed files with 12 additions and 15 deletions

View File

@ -36,8 +36,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/assert.hpp" #include "libtorrent/assert.hpp"
#if TORRENT_USE_ASSERTS && defined BOOST_HAS_PTHREADS #if TORRENT_USE_ASSERTS
#include <pthread.h> #include <thread>
#endif #endif
#if defined TORRENT_ASIO_DEBUGGING #if defined TORRENT_ASIO_DEBUGGING
@ -175,34 +175,31 @@ namespace libtorrent
namespace libtorrent namespace libtorrent
{ {
#if TORRENT_USE_ASSERTS && defined BOOST_HAS_PTHREADS #if TORRENT_USE_ASSERTS
struct single_threaded struct single_threaded
{ {
single_threaded(): m_single_thread(0) {} single_threaded(): m_id() {}
~single_threaded() { m_single_thread = 0; } ~single_threaded() { m_id = std::thread::id(); }
bool is_single_thread() const bool is_single_thread() const
{ {
if (m_single_thread == 0) if (m_id == std::thread::id())
{ {
m_single_thread = pthread_self(); m_id = std::this_thread::get_id();
return true; return true;
} }
return m_single_thread == pthread_self(); return m_id == std::this_thread::get_id();
} }
bool is_not_thread() const bool is_not_thread() const
{ {
if (m_single_thread == 0) return true; if (m_id == std::thread::id()) return true;
return m_single_thread != pthread_self(); return m_id != std::this_thread::get_id();
} }
void thread_started() void thread_started()
{ m_single_thread = pthread_self(); } { m_id = std::this_thread::get_id(); }
void transfer_ownership()
{ m_single_thread = 0; }
private: private:
mutable pthread_t m_single_thread; mutable std::thread::id m_id;
}; };
#else #else
struct single_threaded { struct single_threaded {