Compare commits
3 Commits
6a28413ca0
...
ceff117781
Author | SHA1 | Date | |
---|---|---|---|
ceff117781 | |||
8a78450bcf | |||
b24e7d0ca5 |
@ -121,6 +121,7 @@ target_sources(nf7
|
||||
common/ring_buffer.hh
|
||||
common/sequencer.hh
|
||||
common/squashed_history.hh
|
||||
common/stopwatch.hh
|
||||
common/task.hh
|
||||
common/thread.hh
|
||||
common/timed_queue.hh
|
||||
|
61
common/stopwatch.hh
Normal file
61
common/stopwatch.hh
Normal file
@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
#include "nf7.hh"
|
||||
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
class Stopwatch final {
|
||||
public:
|
||||
struct Benchmark;
|
||||
|
||||
Stopwatch() = default;
|
||||
Stopwatch(const Stopwatch&) = default;
|
||||
Stopwatch(Stopwatch&&) = default;
|
||||
Stopwatch& operator=(const Stopwatch&) = default;
|
||||
Stopwatch& operator=(Stopwatch&&) = default;
|
||||
|
||||
void Begin() noexcept {
|
||||
begin_ = nf7::Env::Clock::now();
|
||||
}
|
||||
void End() noexcept {
|
||||
end_ = nf7::Env::Clock::now();
|
||||
}
|
||||
|
||||
auto dur() const noexcept {
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(end_ - begin_);
|
||||
}
|
||||
nf7::Env::Time beginTime() const noexcept { return begin_; }
|
||||
nf7::Env::Time endTime() const noexcept { return end_; }
|
||||
|
||||
private:
|
||||
nf7::Env::Time begin_;
|
||||
nf7::Env::Time end_;
|
||||
};
|
||||
inline std::ostream& operator << (std::ostream& out, const Stopwatch& sw) {
|
||||
return out << sw.dur().count() << " usecs";
|
||||
}
|
||||
|
||||
struct Stopwatch::Benchmark final {
|
||||
public:
|
||||
Benchmark(const char* name) noexcept : name_(name) {
|
||||
sw_.Begin();
|
||||
}
|
||||
~Benchmark() noexcept {
|
||||
sw_.End();
|
||||
std::cout << name_ << ": " << sw_ << std::endl;
|
||||
}
|
||||
Benchmark(const Benchmark&) = delete;
|
||||
Benchmark(Benchmark&&) = delete;
|
||||
Benchmark& operator=(const Benchmark&) = delete;
|
||||
Benchmark& operator=(Benchmark&&) = delete;
|
||||
|
||||
private:
|
||||
const char* name_;
|
||||
Stopwatch sw_;
|
||||
};
|
||||
|
||||
} // namespace nf7
|
@ -13,17 +13,18 @@
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
// a thread emulation using nf7::Env::ExecAsync
|
||||
// a thread emulation by tasks
|
||||
template <typename Runner, typename Task>
|
||||
class Thread final : public nf7::Context,
|
||||
public std::enable_shared_from_this<Thread<Runner, Task>> {
|
||||
public:
|
||||
Thread() = delete;
|
||||
Thread(nf7::File& f, Runner&& runner) noexcept :
|
||||
Thread(f.env(), f.id(), std::move(runner)) {
|
||||
Thread(nf7::File& f, Runner&& runner, nf7::Env::Executor exec = nf7::Env::kAsync) noexcept :
|
||||
Thread(f.env(), f.id(), std::move(runner), exec) {
|
||||
}
|
||||
Thread(nf7::Env& env, nf7::File::Id id, Runner&& runner) noexcept :
|
||||
nf7::Context(env, id), env_(&env), runner_(std::move(runner)) {
|
||||
Thread(nf7::Env& env, nf7::File::Id id, Runner&& runner, nf7::Env::Executor exec = nf7::Env::kAsync) noexcept :
|
||||
nf7::Context(env, id),
|
||||
runner_(std::move(runner)), exec_(exec) {
|
||||
}
|
||||
Thread(const Thread&) = delete;
|
||||
Thread(Thread&&) = delete;
|
||||
@ -40,8 +41,8 @@ class Thread final : public nf7::Context,
|
||||
private:
|
||||
using Pair = std::pair<std::shared_ptr<nf7::Context>, Task>;
|
||||
|
||||
Env* const env_;
|
||||
Runner runner_;
|
||||
Runner runner_;
|
||||
nf7::Env::Executor exec_;
|
||||
|
||||
nf7::TimedQueue<Pair> q_;
|
||||
|
||||
@ -59,14 +60,14 @@ class Thread final : public nf7::Context,
|
||||
auto self = shared_from_this();
|
||||
if (auto p = q_.Pop()) {
|
||||
k.unlock();
|
||||
env_->ExecAsync(p->first, [this, self, t = std::move(p->second)]() mutable {
|
||||
env().Exec(exec_, p->first, [this, self, t = std::move(p->second)]() mutable {
|
||||
runner_(std::move(t));
|
||||
++tasks_done_;
|
||||
HandleNext();
|
||||
});
|
||||
} else if (auto time = q_.next()) {
|
||||
working_ = false;
|
||||
env_->ExecAsync(self, [this]() mutable { HandleNext(); }, *time);
|
||||
env().Exec(exec_, self, [this]() mutable { HandleNext(); }, *time);
|
||||
} else {
|
||||
working_ = false;
|
||||
}
|
||||
|
@ -139,15 +139,15 @@ struct String {
|
||||
};
|
||||
|
||||
|
||||
template <double kMin, double kMax>
|
||||
template <int kMin, int kMax>
|
||||
struct SliderBase {
|
||||
public:
|
||||
nf7::Value GetValue() const noexcept {
|
||||
return nf7::Value {value_};
|
||||
}
|
||||
void Editor(EditorStatus& ed) noexcept {
|
||||
static const double max = kMax;
|
||||
static const double min = kMin;
|
||||
static const double max = static_cast<double>(kMax);
|
||||
static const double min = static_cast<double>(kMin);
|
||||
|
||||
if (!ed.autosize) {
|
||||
ImGui::SetNextItemWidth(8*ImGui::GetFontSize());
|
||||
@ -163,11 +163,11 @@ struct SliderBase {
|
||||
private:
|
||||
nf7::Value::Scalar value_ = 0;
|
||||
};
|
||||
struct Slider01 : public SliderBase<0., 1.> {
|
||||
struct Slider01 : public SliderBase<0, 1> {
|
||||
static constexpr const char* kName = "slider 0~1";
|
||||
static constexpr const char* kDesc = nullptr;
|
||||
};
|
||||
struct Slider11 : public SliderBase<-1., 1.> {
|
||||
struct Slider11 : public SliderBase<-1, 1> {
|
||||
static constexpr const char* kName = "slider -1~1";
|
||||
static constexpr const char* kDesc = nullptr;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user