Vinayak Ambigapathy ago%!(EXTRA string=2 months)
parent 603f89baa6
commit 8dcca18674
  1. 2
      premake5.lua
  2. 6
      src/bakatools/file_system/dir_entry.cpp
  3. 3
      src/bakatools/file_system/dir_entry.h
  4. 79
      src/bakatools/thread/task_pool.h
  5. 4
      src/bakatoolspch.h

@ -23,7 +23,7 @@ project "bakatools"
}
filter "system:windows"
buildoptions "/MT"
buildoptions { "/MT", "/utf-8" }
defines
{
"BK_PLATFORM_WINDOWS"

@ -1,5 +1,7 @@
#include "dir_entry.h"
#include <bits/chrono.h>
#ifdef BK_PLATFORM_LINUX
#include <bits/chrono.h>
#endif
#include <chrono>
namespace Bk
@ -29,7 +31,7 @@ namespace Bk
return ent.file_size();
}
std::chrono::time_point<std::filesystem::__file_clock> DirEntry::last_write()
std::filesystem::file_time_type DirEntry::last_write()
{
return ent.last_write_time();
}

@ -2,7 +2,6 @@
#include <bakatoolspch.h>
#include "bakatools/container/types.h"
#include <bits/chrono.h>
#include <chrono>
namespace Bk
@ -35,7 +34,7 @@ namespace Bk
std::string name();
u64 size();
std::chrono::time_point<std::filesystem::__file_clock> last_write();
std::filesystem::file_time_type last_write();
bool exists();
FilePerms perms();
uintmax_t hard_links();

@ -0,0 +1,79 @@
#pragma once
#include <condition_variable>
#include <functional>
#include <mutex>
#include <queue>
#include <thread>
namespace Bk
{
class ThreadPool
{
public:
ThreadPool(size_t num_threads
= std::thread::hardware_concurrency())
{
for (size_t i = 0; i < num_threads; ++i) {
threads_.emplace_back([this] {
while (true) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(queue_mutex_);
cv_.wait(lock, [this] {
return !tasks_.empty() || stop_;
});
if (stop_ && tasks_.empty()) {
return;
}
task = std::move(tasks_.front());
tasks_.pop();
}
task();
}
});
}
}
~ThreadPool()
{
{
std::unique_lock<std::mutex> lock(queue_mutex_);
stop_ = true;
}
cv_.notify_all();
for (auto& thread : threads_) {
thread.join();
}
}
void queue(std::function<void()> task)
{
{
std::unique_lock<std::mutex> lock(queue_mutex_);
tasks_.emplace(std::move(task));
}
cv_.notify_one();
}
void stop()
{
std::unique_lock<std::mutex> lock(queue_mutex_);
stop_ = true;
}
private:
std::vector<std::thread> threads_;
std::queue<std::function<void()> > tasks_;
std::mutex queue_mutex_;
std::condition_variable cv_;
bool stop_ = false;
};
}

@ -20,5 +20,5 @@
#include <thread>
#include <chrono>
#include <bakatools/base.h>
#include <bakatools/logging/log.h>
#include "bakatools/base.h"
#include "bakatools/logging/log.h"
Loading…
Cancel
Save