erebus

changeset 24:4336acf8389d

implemented thread pool. not using it yet.
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 30 May 2014 06:56:44 +0300
parents 56d504cc555a
children 6ef4b10fa468
files liberebus/liberebus.vcxproj liberebus/liberebus.vcxproj.filters liberebus/src/erebus_impl.h liberebus/src/threadpool.cc liberebus/src/threadpool.h
diffstat 5 files changed, 101 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/liberebus/liberebus.vcxproj	Thu May 29 07:47:52 2014 +0300
     1.2 +++ b/liberebus/liberebus.vcxproj	Fri May 30 06:56:44 2014 +0300
     1.3 @@ -25,6 +25,7 @@
     1.4      <ClInclude Include="src\scene.h" />
     1.5      <ClInclude Include="src\snode.h" />
     1.6      <ClInclude Include="src\texture.h" />
     1.7 +    <ClInclude Include="src\threadpool.h" />
     1.8    </ItemGroup>
     1.9    <ItemGroup>
    1.10      <ClCompile Include="src\brdf.cc" />
    1.11 @@ -37,6 +38,7 @@
    1.12      <ClCompile Include="src\rt.cc" />
    1.13      <ClCompile Include="src\scene.cc" />
    1.14      <ClCompile Include="src\snode.cc" />
    1.15 +    <ClCompile Include="src\threadpool.cc" />
    1.16    </ItemGroup>
    1.17    <ItemGroup>
    1.18      <None Include="src\image.inl" />
    1.19 @@ -78,7 +80,7 @@
    1.20        <WarningLevel>Level3</WarningLevel>
    1.21        <Optimization>Disabled</Optimization>
    1.22        <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;_USE_MATH_DEFINES;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    1.23 -      <DisableSpecificWarnings>4244;4305</DisableSpecificWarnings>
    1.24 +      <DisableSpecificWarnings>4244;4305;4996</DisableSpecificWarnings>
    1.25      </ClCompile>
    1.26      <Link>
    1.27        <SubSystem>Windows</SubSystem>
    1.28 @@ -94,7 +96,7 @@
    1.29        <FunctionLevelLinking>true</FunctionLevelLinking>
    1.30        <IntrinsicFunctions>true</IntrinsicFunctions>
    1.31        <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;_USE_MATH_DEFINES;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    1.32 -      <DisableSpecificWarnings>4244;4305</DisableSpecificWarnings>
    1.33 +      <DisableSpecificWarnings>4244;4305;4996</DisableSpecificWarnings>
    1.34      </ClCompile>
    1.35      <Link>
    1.36        <SubSystem>Windows</SubSystem>
     2.1 --- a/liberebus/liberebus.vcxproj.filters	Thu May 29 07:47:52 2014 +0300
     2.2 +++ b/liberebus/liberebus.vcxproj.filters	Fri May 30 06:56:44 2014 +0300
     2.3 @@ -57,6 +57,9 @@
     2.4      <ClInclude Include="src\rt.h">
     2.5        <Filter>Header Files</Filter>
     2.6      </ClInclude>
     2.7 +    <ClInclude Include="src\threadpool.h">
     2.8 +      <Filter>Header Files</Filter>
     2.9 +    </ClInclude>
    2.10    </ItemGroup>
    2.11    <ItemGroup>
    2.12      <ClCompile Include="src\erebus.cc">
    2.13 @@ -89,6 +92,9 @@
    2.14      <ClCompile Include="src\rt.cc">
    2.15        <Filter>Source Files</Filter>
    2.16      </ClCompile>
    2.17 +    <ClCompile Include="src\threadpool.cc">
    2.18 +      <Filter>Source Files</Filter>
    2.19 +    </ClCompile>
    2.20    </ItemGroup>
    2.21    <ItemGroup>
    2.22      <None Include="src\image.inl">
     3.1 --- a/liberebus/src/erebus_impl.h	Thu May 29 07:47:52 2014 +0300
     3.2 +++ b/liberebus/src/erebus_impl.h	Fri May 30 06:56:44 2014 +0300
     3.3 @@ -6,6 +6,7 @@
     3.4  #include "erebus.h"
     3.5  #include "image.h"
     3.6  #include "scene.h"
     3.7 +#include "threadpool.h"
     3.8  
     3.9  struct Option {
    3.10  	enum Type { INT, FLOAT, VEC } type;
    3.11 @@ -28,6 +29,8 @@
    3.12  	Image<float> accum;	// sample accumulator per pixel
    3.13  	Option options[ERB_NUM_OPTIONS];
    3.14  
    3.15 +	ThreadPool tpool;
    3.16 +
    3.17  	// render state
    3.18  	float inv_gamma;
    3.19  	long cur_time;
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/liberebus/src/threadpool.cc	Fri May 30 06:56:44 2014 +0300
     4.3 @@ -0,0 +1,58 @@
     4.4 +#include "threadpool.h"
     4.5 +
     4.6 +ThreadPool::ThreadPool(int num_threads)
     4.7 +{
     4.8 +	quit = false;
     4.9 +
    4.10 +	if(num_threads == -1) {
    4.11 +		num_threads = std::thread::hardware_concurrency();
    4.12 +	}
    4.13 +
    4.14 +	printf("creating thread pool with %d threads\n", num_threads);
    4.15 +
    4.16 +	thread = new std::thread[num_threads];
    4.17 +	for(int i=0; i<num_threads; i++) {
    4.18 +		thread[i] = std::thread(&ThreadPool::thread_func, this);
    4.19 +	}
    4.20 +}
    4.21 +
    4.22 +ThreadPool::~ThreadPool()
    4.23 +{
    4.24 +	quit = true;
    4.25 +	condvar.notify_all();
    4.26 +
    4.27 +	printf("ThreadPool: waiting for %d worker threads to stop ", num_threads);
    4.28 +	fflush(stdout);
    4.29 +	for(int i=0; i<num_threads; i++) {
    4.30 +		thread[i].join();
    4.31 +		putchar('.');
    4.32 +		fflush(stdout);
    4.33 +	}
    4.34 +	putchar('\n');
    4.35 +}
    4.36 +
    4.37 +void ThreadPool::add_work(std::function<void ()> func)
    4.38 +{
    4.39 +	std::unique_lock<std::mutex> lock(workq_mutex);
    4.40 +	workq.push_back(func);
    4.41 +}
    4.42 +
    4.43 +void ThreadPool::thread_func()
    4.44 +{
    4.45 +	std::unique_lock<std::mutex> lock(workq_mutex);
    4.46 +	for(;;) {
    4.47 +		if(quit) break;
    4.48 +
    4.49 +		condvar.wait(lock);
    4.50 +
    4.51 +		if(!quit && !workq.empty()) {
    4.52 +			std::function<void ()> work = workq.front();
    4.53 +			workq.pop_front();
    4.54 +			lock.unlock();
    4.55 +
    4.56 +			work();
    4.57 +
    4.58 +			lock.lock();
    4.59 +		}
    4.60 +	}
    4.61 +}
    4.62 \ No newline at end of file
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/liberebus/src/threadpool.h	Fri May 30 06:56:44 2014 +0300
     5.3 @@ -0,0 +1,30 @@
     5.4 +#ifndef THREAD_POOL_H_
     5.5 +#define THREAD_POOL_H_
     5.6 +
     5.7 +#include <list>
     5.8 +#include <functional>
     5.9 +#include <thread>
    5.10 +#include <mutex>
    5.11 +#include <condition_variable>
    5.12 +
    5.13 +class ThreadPool {
    5.14 +private:
    5.15 +	int num_threads;
    5.16 +	std::thread *thread;	// array of threads
    5.17 +
    5.18 +	std::list<std::function<void ()>> workq;
    5.19 +	std::mutex workq_mutex;
    5.20 +	std::condition_variable condvar;
    5.21 +
    5.22 +	bool quit;
    5.23 +
    5.24 +	void thread_func();
    5.25 +
    5.26 +public:
    5.27 +	ThreadPool(int num_threads = -1);
    5.28 +	~ThreadPool();
    5.29 +
    5.30 +	void add_work(std::function<void ()> func);
    5.31 +};
    5.32 +
    5.33 +#endif	// THREAD_POOL_H_
    5.34 \ No newline at end of file