#include "thread_worker.h"
#include "zlog.h"
namespace vulkanapi {
	CommandThreadWorker::CommandThreadWorker(const string name, int buffer)
		: mName(name)
		, mChannel(buffer)
		, mSemaphore(0)
	{
		mThread = std::thread(&CommandThreadWorker::workloop, this);
	}
	CommandThreadWorker::~CommandThreadWorker() {
		zlog::info("~CommandThreadWorker");
		zlog::flush();
	}
	void CommandThreadWorker::workloop()
	{
		mThread.detach();
		while (true) {
			voidFn fn = mChannel.acquire();
			fn();
		}
	}
	void CommandThreadWorker::Invoke(voidFn fn)
	{
		mChannel.release(fn);
	}

	void CommandThreadWorker::SyncInvoke(voidFn fn)
	{
		Invoke([=]() {
			fn();
			mSemaphore.release();
		});
		mSemaphore.acquire();
	}
}
