2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2016-09-19 07:43:24 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_CTX_MUTEX_H
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
|
|
|
class mutex;
|
|
|
|
}
|
2016-09-19 07:43:24 +02:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
//
|
|
|
|
// The mutex only allows one context to lock it and continue,
|
|
|
|
// additional contexts are queued. This can be used with std::
|
|
|
|
// locking concepts.
|
|
|
|
//
|
|
|
|
class ircd::ctx::mutex
|
2016-09-19 07:43:24 +02:00
|
|
|
{
|
2018-11-14 03:18:38 +01:00
|
|
|
dock q;
|
2018-11-16 03:44:54 +01:00
|
|
|
bool m;
|
2016-09-19 07:43:24 +02:00
|
|
|
|
2019-09-10 20:05:17 +02:00
|
|
|
void deadlock_assertion() const noexcept;
|
|
|
|
|
2016-09-19 07:43:24 +02:00
|
|
|
public:
|
2019-09-10 19:19:50 +02:00
|
|
|
bool locked() const noexcept;
|
|
|
|
size_t waiting() const noexcept;
|
2019-09-10 20:04:55 +02:00
|
|
|
bool waiting(const ctx &) const noexcept;
|
2018-03-06 10:09:37 +01:00
|
|
|
|
2019-09-10 19:19:50 +02:00
|
|
|
bool try_lock() noexcept;
|
2016-09-19 07:43:24 +02:00
|
|
|
template<class time_point> bool try_lock_until(const time_point &);
|
|
|
|
template<class duration> bool try_lock_for(const duration &);
|
|
|
|
|
|
|
|
void lock();
|
|
|
|
void unlock();
|
|
|
|
|
2019-09-10 19:19:50 +02:00
|
|
|
mutex() noexcept;
|
2018-01-17 11:58:57 +01:00
|
|
|
mutex(mutex &&) noexcept;
|
|
|
|
mutex(const mutex &) = delete;
|
|
|
|
mutex &operator=(mutex &&) noexcept;
|
|
|
|
mutex &operator=(const mutex &) = delete;
|
2016-09-19 07:43:24 +02:00
|
|
|
~mutex() noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline
|
2018-01-17 11:58:57 +01:00
|
|
|
ircd::ctx::mutex::mutex()
|
2019-09-10 19:19:50 +02:00
|
|
|
noexcept
|
2018-01-17 11:58:57 +01:00
|
|
|
:m{false}
|
2016-09-19 07:43:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-17 11:58:57 +01:00
|
|
|
inline
|
|
|
|
ircd::ctx::mutex::mutex(mutex &&o)
|
|
|
|
noexcept
|
2018-11-16 03:44:54 +01:00
|
|
|
:q{std::move(o.q)}
|
|
|
|
,m{std::move(o.m)}
|
2018-01-17 11:58:57 +01:00
|
|
|
{
|
|
|
|
o.m = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::ctx::mutex &
|
|
|
|
ircd::ctx::mutex::operator=(mutex &&o)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
this->~mutex();
|
2018-11-14 03:18:38 +01:00
|
|
|
q = std::move(o.q);
|
2018-11-16 03:44:54 +01:00
|
|
|
m = std::move(o.m);
|
2018-11-14 02:45:08 +01:00
|
|
|
o.m = false;
|
2018-01-17 11:58:57 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-09-19 07:43:24 +02:00
|
|
|
inline
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::mutex::~mutex()
|
2016-09-19 07:43:24 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
assert(!m);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::mutex::unlock()
|
2016-09-19 07:43:24 +02:00
|
|
|
{
|
2017-09-30 08:06:23 +02:00
|
|
|
assert(m);
|
2016-09-19 07:43:24 +02:00
|
|
|
m = false;
|
2018-11-14 03:18:38 +01:00
|
|
|
q.notify_one();
|
2016-09-19 07:43:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::mutex::lock()
|
2016-09-19 07:43:24 +02:00
|
|
|
{
|
2019-09-10 20:05:17 +02:00
|
|
|
deadlock_assertion();
|
|
|
|
|
2018-11-14 03:18:38 +01:00
|
|
|
q.wait([this]
|
|
|
|
{
|
|
|
|
return !locked();
|
|
|
|
});
|
2016-09-19 07:43:24 +02:00
|
|
|
|
2018-11-14 03:18:38 +01:00
|
|
|
m = true;
|
2016-09-19 07:43:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class duration>
|
|
|
|
bool
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::mutex::try_lock_for(const duration &d)
|
2016-09-19 07:43:24 +02:00
|
|
|
{
|
2019-09-23 05:02:23 +02:00
|
|
|
return try_lock_until(system_clock::now() + d);
|
2016-09-19 07:43:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class time_point>
|
|
|
|
bool
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::mutex::try_lock_until(const time_point &tp)
|
2016-09-19 07:43:24 +02:00
|
|
|
{
|
2019-09-10 20:05:17 +02:00
|
|
|
deadlock_assertion();
|
|
|
|
|
2018-11-14 03:18:38 +01:00
|
|
|
const bool success
|
2016-09-19 07:43:24 +02:00
|
|
|
{
|
2018-11-14 03:18:38 +01:00
|
|
|
q.wait_until(tp, [this]
|
2016-09-19 07:43:24 +02:00
|
|
|
{
|
2018-11-14 03:18:38 +01:00
|
|
|
return !locked();
|
|
|
|
})
|
|
|
|
};
|
2016-09-19 07:43:24 +02:00
|
|
|
|
2018-11-14 03:18:38 +01:00
|
|
|
if(likely(success))
|
|
|
|
m = true;
|
|
|
|
|
|
|
|
return success;
|
2016-09-19 07:43:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::mutex::try_lock()
|
2019-09-10 19:19:50 +02:00
|
|
|
noexcept
|
2016-09-19 07:43:24 +02:00
|
|
|
{
|
2019-09-10 20:05:17 +02:00
|
|
|
deadlock_assertion();
|
|
|
|
|
2018-11-14 03:18:38 +01:00
|
|
|
if(locked())
|
2016-09-19 07:43:24 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
m = true;
|
|
|
|
return true;
|
|
|
|
}
|
2017-12-23 23:25:52 +01:00
|
|
|
|
2019-09-10 20:04:55 +02:00
|
|
|
inline bool
|
|
|
|
ircd::ctx::mutex::waiting(const ctx &c)
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
return q.waiting(c);
|
|
|
|
}
|
|
|
|
|
2018-03-06 10:09:37 +01:00
|
|
|
inline size_t
|
|
|
|
ircd::ctx::mutex::waiting()
|
2019-09-10 19:19:50 +02:00
|
|
|
const noexcept
|
2018-03-06 10:09:37 +01:00
|
|
|
{
|
|
|
|
return q.size();
|
|
|
|
}
|
|
|
|
|
2018-06-01 16:48:23 +02:00
|
|
|
inline bool
|
|
|
|
ircd::ctx::mutex::locked()
|
2019-09-10 19:19:50 +02:00
|
|
|
const noexcept
|
2018-06-01 16:48:23 +02:00
|
|
|
{
|
|
|
|
return m;
|
|
|
|
}
|
2019-09-10 20:05:17 +02:00
|
|
|
|
|
|
|
inline void
|
|
|
|
__attribute__((always_inline, artificial))
|
|
|
|
ircd::ctx::mutex::deadlock_assertion()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
assert(!locked() || !waiting(cur()));
|
|
|
|
}
|