feat add benchmark
This commit is contained in:
@@ -5,12 +5,14 @@
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
#include "marl/conditionvariable.h"
|
||||
#ifndef SLED_SYNCHRONIZATION_MUTEX_H
|
||||
#define SLED_SYNCHRONIZATION_MUTEX_H
|
||||
|
||||
#include "sled/units/time_delta.h"
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <marl/mutex.h>
|
||||
#include <mutex>
|
||||
#include <type_traits>
|
||||
|
||||
@@ -31,24 +33,26 @@ struct HasLockAndUnlock {
|
||||
};
|
||||
}// namespace internal
|
||||
|
||||
class Mutex final {
|
||||
public:
|
||||
Mutex() = default;
|
||||
Mutex(const Mutex &) = delete;
|
||||
Mutex &operator=(const Mutex &) = delete;
|
||||
using Mutex = marl::mutex;
|
||||
|
||||
inline void Lock() { impl_.lock(); };
|
||||
|
||||
inline bool TryLock() { return impl_.try_lock(); }
|
||||
|
||||
inline void AssertHeld() {}
|
||||
|
||||
inline void Unlock() { impl_.unlock(); }
|
||||
|
||||
private:
|
||||
std::mutex impl_;
|
||||
friend class ConditionVariable;
|
||||
};
|
||||
// class Mutex final {
|
||||
// public:
|
||||
// Mutex() = default;
|
||||
// Mutex(const Mutex &) = delete;
|
||||
// Mutex &operator=(const Mutex &) = delete;
|
||||
//
|
||||
// inline void Lock() { impl_.lock(); };
|
||||
//
|
||||
// inline bool TryLock() { return impl_.try_lock(); }
|
||||
//
|
||||
// inline void AssertHeld() {}
|
||||
//
|
||||
// inline void Unlock() { impl_.unlock(); }
|
||||
//
|
||||
// private:
|
||||
// std::mutex impl_;
|
||||
// friend class ConditionVariable;
|
||||
// };
|
||||
|
||||
class RecursiveMutex final {
|
||||
public:
|
||||
@@ -85,8 +89,22 @@ private:
|
||||
friend class ConditionVariable;
|
||||
};
|
||||
|
||||
using MutexLock = LockGuard<Mutex>;
|
||||
using MutexGuard = LockGuard<Mutex>;
|
||||
class MutexGuard final {
|
||||
public:
|
||||
MutexGuard(Mutex *mutex) : lock_(*mutex) {}
|
||||
|
||||
MutexGuard(const MutexGuard &) = delete;
|
||||
MutexGuard &operator=(const MutexGuard &) = delete;
|
||||
|
||||
private:
|
||||
friend class ConditionVariable;
|
||||
marl::lock lock_;
|
||||
};
|
||||
|
||||
using MutexLock = MutexGuard;
|
||||
// using MutexGuard = marl::lock;
|
||||
// using MutexLock = LockGuard<Mutex>;
|
||||
// using MutexGuard = LockGuard<Mutex>;
|
||||
using RecursiveMutexLock = LockGuard<RecursiveMutex>;
|
||||
|
||||
// class MutexLock final {
|
||||
@@ -121,44 +139,77 @@ using RecursiveMutexLock = LockGuard<RecursiveMutex>;
|
||||
class ConditionVariable final {
|
||||
public:
|
||||
static constexpr TimeDelta kForever = TimeDelta::PlusInfinity();
|
||||
ConditionVariable() = default;
|
||||
ConditionVariable(const ConditionVariable &) = delete;
|
||||
ConditionVariable &operator=(const ConditionVariable &) = delete;
|
||||
|
||||
template<typename Predicate>
|
||||
inline bool Wait(LockGuard<Mutex> &guard, Predicate pred)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(guard.mutex_->impl_, std::adopt_lock);
|
||||
cv_.wait(lock, pred);
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename Predicate>
|
||||
inline bool
|
||||
WaitFor(LockGuard<Mutex> &guard, TimeDelta timeout, Predicate pred)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(guard.mutex_->impl_, std::adopt_lock);
|
||||
if (timeout == kForever) {
|
||||
cv_.wait(lock, pred);
|
||||
return true;
|
||||
} else {
|
||||
return cv_.wait_for(lock, std::chrono::milliseconds(timeout.ms()),
|
||||
pred);
|
||||
}
|
||||
}
|
||||
|
||||
// template<typename Predicate>
|
||||
// bool WaitUntil(Mutex *mutex, TimeDelta timeout, Predicate pred)
|
||||
// {}
|
||||
// inline ConditionVariable();
|
||||
|
||||
inline void NotifyOne() { cv_.notify_one(); }
|
||||
|
||||
inline void NotifyAll() { cv_.notify_all(); }
|
||||
|
||||
template<typename Predicate>
|
||||
inline void Wait(MutexLock &lock, Predicate &&pred)
|
||||
{
|
||||
cv_.wait(lock, std::forward<Predicate>(pred));
|
||||
}
|
||||
|
||||
template<typename Predicate>
|
||||
inline bool WaitFor(MutexLock &lock, TimeDelta timeout, Predicate &&pred)
|
||||
{
|
||||
if (timeout == TimeDelta::PlusInfinity()) {
|
||||
cv_.wait(lock.lock_, std::forward<Predicate>(pred));
|
||||
return true;
|
||||
} else {
|
||||
return cv_.wait_for(lock.lock_,
|
||||
std::chrono::milliseconds(timeout.ms()),
|
||||
std::forward<Predicate>(pred));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::condition_variable cv_;
|
||||
marl::ConditionVariable cv_;
|
||||
};
|
||||
|
||||
// class ConditionVariable final {
|
||||
// public:
|
||||
// static constexpr TimeDelta kForever = TimeDelta::PlusInfinity();
|
||||
// ConditionVariable() = default;
|
||||
// ConditionVariable(const ConditionVariable &) = delete;
|
||||
// ConditionVariable &operator=(const ConditionVariable &) = delete;
|
||||
//
|
||||
// template<typename Predicate>
|
||||
// inline bool Wait(LockGuard<Mutex> &guard, Predicate pred)
|
||||
// {
|
||||
// std::unique_lock<std::mutex> lock(guard.mutex_->impl_, std::adopt_lock);
|
||||
// cv_.wait(lock, pred);
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// template<typename Predicate>
|
||||
// inline bool
|
||||
// WaitFor(LockGuard<Mutex> &guard, TimeDelta timeout, Predicate pred)
|
||||
// {
|
||||
// std::unique_lock<std::mutex> lock(guard.mutex_->impl_, std::adopt_lock);
|
||||
// if (timeout == kForever) {
|
||||
// cv_.wait(lock, pred);
|
||||
// return true;
|
||||
// } else {
|
||||
// return cv_.wait_for(lock, std::chrono::milliseconds(timeout.ms()),
|
||||
// pred);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // template<typename Predicate>
|
||||
// // bool WaitUntil(Mutex *mutex, TimeDelta timeout, Predicate pred)
|
||||
// // {}
|
||||
//
|
||||
// inline void NotifyOne() { cv_.notify_one(); }
|
||||
//
|
||||
// inline void NotifyAll() { cv_.notify_all(); }
|
||||
//
|
||||
// private:
|
||||
// std::condition_variable cv_;
|
||||
// };
|
||||
|
||||
}// namespace sled
|
||||
|
||||
#endif// SLED_SYNCHRONIZATION_MUTEX_H
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef SLED_SYSTEM_FIBER_FIBER_WAIT_GROUP_H
|
||||
#define SLED_SYSTEM_FIBER_FIBER_WAIT_GROUP_H
|
||||
#include <marl/waitgroup.h>
|
||||
|
||||
namespace sled {
|
||||
using FiberWaitGroup = marl::WaitGroup;
|
||||
}
|
||||
|
||||
#endif// SLED_SYSTEM_FIBER_FIBER_WAIT_GROUP_H
|
||||
@@ -1,12 +1,12 @@
|
||||
#pragma once
|
||||
#ifndef SLED_SYSTEM_FIBER_FIBER_SCHEDULER_H
|
||||
#define SLED_SYSTEM_FIBER_FIBER_SCHEDULER_H
|
||||
#ifndef SLED_SYSTEM_FIBER_SCHEDULER_H
|
||||
#define SLED_SYSTEM_FIBER_SCHEDULER_H
|
||||
#include <marl/defer.h>
|
||||
#include <marl/scheduler.h>
|
||||
#include <marl/task.h>
|
||||
|
||||
namespace sled {
|
||||
using FiberScheduler = marl::Scheduler;
|
||||
using Scheduler = marl::Scheduler;
|
||||
|
||||
// schedule() schedules the task T to be asynchronously called using the
|
||||
// currently bound scheduler.
|
||||
@@ -42,4 +42,4 @@ Schedule(Function &&f)
|
||||
}
|
||||
}// namespace sled
|
||||
|
||||
#endif// SLED_SYSTEM_FIBER_FIBER_SCHEDULER_H
|
||||
#endif// SLED_SYSTEM_FIBER_SCHEDULER_H
|
||||
26
include/sled/system/fiber/wait_group.h
Normal file
26
include/sled/system/fiber/wait_group.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
#ifndef SLED_SYSTEM_FIBER_WAIT_GROUP_H
|
||||
#define SLED_SYSTEM_FIBER_WAIT_GROUP_H
|
||||
#include <marl/waitgroup.h>
|
||||
|
||||
namespace sled {
|
||||
|
||||
class WaitGroup final {
|
||||
public:
|
||||
inline WaitGroup(unsigned int count = 0,
|
||||
marl::Allocator *allocator = marl::Allocator::Default)
|
||||
: wg_(new marl::WaitGroup(count, allocator))
|
||||
{}
|
||||
|
||||
inline void Add(unsigned int count = 1) const { wg_->add(count); };
|
||||
|
||||
inline bool Done() const { return wg_->done(); }
|
||||
|
||||
inline void Wait() const { wg_->wait(); }
|
||||
|
||||
private:
|
||||
mutable std::shared_ptr<marl::WaitGroup> wg_;
|
||||
};
|
||||
}// namespace sled
|
||||
|
||||
#endif// SLED_SYSTEM_FIBER_WAIT_GROUP_H
|
||||
Reference in New Issue
Block a user