init repo.
This commit is contained in:
42
src/synchronization/event.cc
Normal file
42
src/synchronization/event.cc
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "sled/synchronization/event.h"
|
||||
|
||||
namespace sled {
|
||||
constexpr TimeDelta Event::kForever;
|
||||
|
||||
Event::Event() : Event(false, false) {}
|
||||
|
||||
Event::Event(bool manual_reset, bool initially_signaled)
|
||||
: is_manual_reset_(manual_reset),
|
||||
event_status_(initially_signaled)
|
||||
{}
|
||||
|
||||
Event::~Event() {}
|
||||
|
||||
void
|
||||
Event::Set()
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
event_status_ = true;
|
||||
cv_.NotifyAll();
|
||||
}
|
||||
|
||||
void
|
||||
Event::Reset()
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
event_status_ = false;
|
||||
}
|
||||
|
||||
bool
|
||||
Event::Wait(TimeDelta give_up_after, TimeDelta warn_after)
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
bool wait_success =
|
||||
cv_.WaitFor(&mutex_, give_up_after, [&] { return event_status_; });
|
||||
if (!wait_success) { return false; }
|
||||
|
||||
if (!is_manual_reset_) { event_status_ = false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
}// namespace sled
|
||||
5
src/synchronization/mutex.cc
Normal file
5
src/synchronization/mutex.cc
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "sled/synchronization/mutex.h"
|
||||
|
||||
namespace sled {
|
||||
constexpr TimeDelta ConditionVariable::kForever;
|
||||
}
|
||||
13
src/synchronization/sequence_checker_internal.cc
Normal file
13
src/synchronization/sequence_checker_internal.cc
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "sled/synchronization/sequence_checker_internal.h"
|
||||
|
||||
namespace sled {
|
||||
SequenceCheckerImpl::SequenceCheckerImpl(bool attach_to_current_thread)
|
||||
: attached_(attach_to_current_thread)
|
||||
{}
|
||||
|
||||
bool
|
||||
SequenceCheckerImpl::IsCurrent() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}// namespace sled
|
||||
93
src/synchronization/thread_local.cc
Normal file
93
src/synchronization/thread_local.cc
Normal file
@@ -0,0 +1,93 @@
|
||||
#include "sled/synchronization/thread_local.h"
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace sled {
|
||||
namespace detail {
|
||||
|
||||
thread_local static std::unordered_map<ThreadId, std::unordered_map<ThreadLocalKey, void *>>
|
||||
thread_local_table_;
|
||||
thread_local static std::atomic<ThreadLocalKey> next_key_;
|
||||
|
||||
class ThreadLocalManager::Impl final {
|
||||
public:
|
||||
static ThreadId CurrentThreadId() { return std::this_thread::get_id(); }
|
||||
|
||||
static ThreadLocalKey NextKey() { return next_key_.fetch_add(1); }
|
||||
|
||||
void *Get(const ThreadId &thread_id, const ThreadLocalKey &key) const
|
||||
{
|
||||
auto iter = thread_local_table_.find(thread_id);
|
||||
if (iter == thread_local_table_.end()) { return nullptr; }
|
||||
auto &thread_local_map = iter->second;
|
||||
auto value_iter = thread_local_map.find(key);
|
||||
if (value_iter == thread_local_map.end()) { return nullptr; }
|
||||
return value_iter->second;
|
||||
}
|
||||
|
||||
void Delete(const ThreadId &thread_id, const ThreadLocalKey &key)
|
||||
{
|
||||
auto iter = thread_local_table_.find(thread_id);
|
||||
if (iter == thread_local_table_.end()) { return; }
|
||||
auto &thread_local_map = iter->second;
|
||||
thread_local_map.erase(key);
|
||||
}
|
||||
|
||||
void Set(const ThreadId &thread_id, const ThreadLocalKey &key, void *value)
|
||||
{
|
||||
auto iter = thread_local_table_.find(thread_id);
|
||||
if (iter == thread_local_table_.end()) {
|
||||
iter =
|
||||
thread_local_table_.emplace(thread_id, std::unordered_map<ThreadLocalKey, void *>())
|
||||
.first;
|
||||
}
|
||||
auto &thread_local_map = iter->second;
|
||||
thread_local_map[key] = value;
|
||||
}
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
ThreadLocalManager &
|
||||
ThreadLocalManager::Instance()
|
||||
{
|
||||
static ThreadLocalManager instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
ThreadLocalManager::ThreadLocalManager() : impl_(new Impl()) {}
|
||||
|
||||
ThreadLocalManager::~ThreadLocalManager() = default;
|
||||
|
||||
ThreadId
|
||||
ThreadLocalManager::CurrentThreadId()
|
||||
{
|
||||
return Impl::CurrentThreadId();
|
||||
}
|
||||
|
||||
ThreadLocalKey
|
||||
ThreadLocalManager::NextKey()
|
||||
{
|
||||
return Impl::NextKey();
|
||||
}
|
||||
|
||||
void *
|
||||
ThreadLocalManager::Get(const ThreadId &thread_id, const ThreadLocalKey &key) const
|
||||
{
|
||||
return impl_->Get(thread_id, key);
|
||||
}
|
||||
|
||||
void
|
||||
ThreadLocalManager::Delete(const ThreadId &thread_id, const ThreadLocalKey &key)
|
||||
{
|
||||
impl_->Delete(thread_id, key);
|
||||
}
|
||||
|
||||
void
|
||||
ThreadLocalManager::Set(const ThreadId &thread_id, const ThreadLocalKey &key, void *value)
|
||||
{
|
||||
impl_->Set(thread_id, key, value);
|
||||
}
|
||||
}// namespace detail
|
||||
}// namespace sled
|
||||
Reference in New Issue
Block a user