almost done

This commit is contained in:
Dhanya Maliakal
2016-09-09 17:51:36 +02:00
parent f8b62bba64
commit 652d29f2d9
13 changed files with 441 additions and 197 deletions

View File

@ -16,6 +16,18 @@ using namespace std;
class slsDetector;
template<typename _Ret, typename _Class>
class func00_t{
public:
func00_t(_Ret (_Class::*fn)(),_Class* ptr):
m_fn(fn),m_ptr(ptr){}
~func00_t() {}
void operator()() const {((m_ptr->*m_fn)());}
private:
_Class* m_ptr;
_Ret (_Class::*m_fn)();
};
template<typename _Ret, typename _Class, typename _Store>
class func0_t{
public:
@ -94,27 +106,32 @@ private:
class Task: public virtual slsDetectorDefs{
public:
/* Return: int, Param: int */
Task(func1_t <int,slsDetector,int,int>* t): m1(t),m2(0),m3(0),m4(0),m5(0),m6(0),m7(0),m8(0),m9(0),m10(0),m11(0){};
Task(func1_t <int,slsDetector,int,int>* t): m1(t),m2(0),m3(0),m4(0),m5(0),m6(0),m7(0),m8(0),m9(0),m10(0),m11(0),m12(0){};
/* Return: int, Param: string,int */
Task(func2_t <int,slsDetector,string,int,int>* t): m1(0),m2(t),m3(0),m4(0),m5(0),m6(0),m7(0),m8(0),m9(0),m10(0),m11(0){};
Task(func2_t <int,slsDetector,string,int,int>* t): m1(0),m2(t),m3(0),m4(0),m5(0),m6(0),m7(0),m8(0),m9(0),m10(0),m11(0),m12(0){};
/* Return: string, Param: string */
Task(func1_t <string,slsDetector,string,string>* t): m1(0),m2(0),m3(t),m4(0),m5(0),m6(0),m7(0),m8(0),m9(0),m10(0),m11(0){};
Task(func1_t <string,slsDetector,string,string>* t): m1(0),m2(0),m3(t),m4(0),m5(0),m6(0),m7(0),m8(0),m9(0),m10(0),m11(0),m12(0){};
/* Return: char*, Param: char* */
Task(func1_t <char*,slsDetector,char*,string>* t): m1(0),m2(0),m3(0),m4(t),m5(0),m6(0),m7(0),m8(0),m9(0),m10(0),m11(0){};
Task(func1_t <char*,slsDetector,char*,string>* t): m1(0),m2(0),m3(0),m4(t),m5(0),m6(0),m7(0),m8(0),m9(0),m10(0),m11(0),m12(0){};
/* Return: detectorSettings, Param: int */
Task(func1_t <detectorSettings,slsDetector,int,int>* t): m1(0),m2(0),m3(0),m4(0),m5(t),m6(0),m7(0),m8(0),m9(0),m10(0),m11(0){};
Task(func1_t <detectorSettings,slsDetector,int,int>* t): m1(0),m2(0),m3(0),m4(0),m5(t),m6(0),m7(0),m8(0),m9(0),m10(0),m11(0),m12(0){};
/* Return: detectorSettings, Param: detectorSettings,int */
Task(func2_t <detectorSettings,slsDetector,detectorSettings,int,int>* t): m1(0),m2(0),m3(0),m4(0),m5(0),m6(t),m7(0),m8(0),m9(0),m10(0),m11(0){};
Task(func2_t <detectorSettings,slsDetector,detectorSettings,int,int>* t): m1(0),m2(0),m3(0),m4(0),m5(0),m6(t),m7(0),m8(0),m9(0),m10(0),m11(0),m12(0){};
/* Return: int, Param: int,int */
Task(func2_t <int,slsDetector,int,int,int>* t): m1(0),m2(0),m3(0),m4(0),m5(0),m6(0),m7(t),m8(0),m9(0),m10(0),m11(0){};
Task(func2_t <int,slsDetector,int,int,int>* t): m1(0),m2(0),m3(0),m4(0),m5(0),m6(0),m7(t),m8(0),m9(0),m10(0),m11(0),m12(0){};
/* Return: int, Param: int,int */
Task(func3_t <int,slsDetector,int,int,int,int>* t): m1(0),m2(0),m3(0),m4(0),m5(0),m6(0),m7(0),m8(t),m9(0),m10(0),m11(0){};
Task(func3_t <int,slsDetector,int,int,int,int>* t): m1(0),m2(0),m3(0),m4(0),m5(0),m6(0),m7(0),m8(t),m9(0),m10(0),m11(0),m12(0){};
/* Return: int, Param: trimMode,int,int,int */
Task(func4_t <int,slsDetector,trimMode,int,int,int,int>* t): m1(0),m2(0),m3(0),m4(0),m5(0),m6(0),m7(0),m8(0),m9(t),m10(0),m11(0){};
/* Return: int, Param: int */
Task(func0_t <int,slsDetector,int>* t): m1(0),m2(0),m3(0),m4(0),m5(0),m6(0),m7(0),m8(0),m9(0),m10(t),m11(0){};
Task(func4_t <int,slsDetector,trimMode,int,int,int,int>* t): m1(0),m2(0),m3(0),m4(0),m5(0),m6(0),m7(0),m8(0),m9(t),m10(0),m11(0),m12(0){};
/* Return: int, Param: none */
Task(func0_t <int,slsDetector,int>* t): m1(0),m2(0),m3(0),m4(0),m5(0),m6(0),m7(0),m8(0),m9(0),m10(t),m11(0),m12(0){};
/* Return: char*, Param: networkParameter,string,string */
Task(func2_t <char*,slsDetector,networkParameter,string,string>* t): m1(0),m2(0),m3(0),m4(0),m5(0),m6(0),m7(0),m8(0),m9(0),m10(0),m11(t){};
Task(func2_t <char*,slsDetector,networkParameter,string,string>* t): m1(0),m2(0),m3(0),m4(0),m5(0),m6(0),m7(0),m8(0),m9(0),m10(0),m11(t),m12(0){};
/* Return: void, Param: none */
Task(func00_t <void,slsDetector>* t): m1(0),m2(0),m3(0),m4(0),m5(0),m6(0),m7(0),m8(0),m9(0),m10(0),m11(0),m12(t){};
~Task(){}
void operator()(){
@ -129,6 +146,7 @@ public:
else if(m9) (*m9)();
else if(m10) (*m10)();
else if(m11) (*m11)();
else if(m12) (*m12)();
}
private:
@ -154,6 +172,8 @@ private:
func0_t <int,slsDetector,int>* m10;
/* Return: char*, Param: networkParameter,string,string */
func2_t <char*,slsDetector,networkParameter,string,string>* m11;
/* Return: void, Param: none */
func00_t <void,slsDetector>* m12;
};

View File

@ -7,8 +7,10 @@ ThreadPool::ThreadPool(int pool_size) : m_pool_size(pool_size){
#endif
m_tasks_loaded = false;
thread_started = false;
zmqthreadpool = false;
current_thread_number = -1;
number_of_ongoing_tasks = 0;
number_of_total_tasks = 0;
}
ThreadPool::~ThreadPool(){
@ -34,6 +36,7 @@ int ThreadPool::initialize_threadpool(){
m_pool_state = STARTED;
int ret = -1;
sem_init(&semStart,1,0);
sem_init(&semDone,1,0);
for (int i = 0; i < m_pool_size; i++) {
pthread_t tid;
thread_started = false;
@ -68,12 +71,15 @@ int ThreadPool::destroy_threadpool(){
for (int i = 0; i < m_pool_size; i++) {
void* result;
sem_post(&semStart);
sem_post(&semDone);
ret = pthread_join(m_threads[i], &result);
/*cout << "pthread_join() returned " << ret << ": " << strerror(errno) << endl;*/
m_task_cond_var.broadcast(); // try waking up a bunch of threads that are still waiting
}
sem_destroy(&semStart);
sem_destroy(&semDone);
number_of_ongoing_tasks = 0;
number_of_total_tasks = 0;
/* cout << m_pool_size << " threads exited from the thread pool" << endl;*/
return 0;
}
@ -109,25 +115,28 @@ void* ThreadPool::execute_thread(){
/*cout << "Unlocking: " << pthread_self() << endl;*/
m_task_mutex.unlock();
//if(zmqthreadpool) cout<<"***"<<ithread<<" semaphore start address wait:"<<&semStart<<endl;
sem_wait(&semStart);
//cout<<"***"<<ithread<<" checking out semaphore done address:"<<&semDone<<endl;
/*cout << ithread <<" Executing thread " << pthread_self() << endl;*/
// execute the task
(*task)(); // could also do task->run(arg);
/*cout << ithread <<" Done executing thread " << pthread_self() << endl;*/
m_all_tasks_mutex.lock();
m_task_mutex.lock();
number_of_ongoing_tasks--;
m_all_tasks_mutex.unlock();
m_task_mutex.unlock();
//if(zmqthreadpool) cout<<ithread <<" task done: "<<number_of_ongoing_tasks<<endl;
//last task and check m_tasks_loaded to ensure done only once
if((!number_of_ongoing_tasks) && m_tasks_loaded){
/*cout << ithread << " all tasks done."<<endl;*/
m_all_tasks_mutex.lock();
//if(zmqthreadpool) cout << ithread << " all tasks done."<<endl;
m_tasks_loaded = false;
m_all_tasks_cond_var.signal();// wake up thread that is waiting for all tasks to be complete
m_all_tasks_mutex.unlock();
}
//if(zmqthreadpool) cout<<"***"<<ithread<<" semaphore done address post:"<<&semDone<<endl;
sem_post(&semDone);
delete task;
/*cout << ithread << " task deleted" << endl;*/
}
@ -143,28 +152,37 @@ int ThreadPool::add_task(Task* task){
// TODO: put a limit on how many tasks can be added at most
m_tasks.push_back(task);
number_of_ongoing_tasks++;
number_of_total_tasks++;
m_task_cond_var.signal(); // wake up one thread that is waiting for a task to be available
m_task_mutex.unlock();
return 0;
}
void ThreadPool::startExecuting(){
if(m_pool_size == 1)
return;
/*cout << "waiting for tasks: locked. gonna wait" << endl;*/
m_tasks_loaded = true;
//giving all threads permission to start as all tasks have been added
//if(zmqthreadpool) cout<<"*** semaphore start address post:"<<&semStart<<endl;
for(int i=0;i<number_of_total_tasks;i++)
sem_post(&semStart);
}
void ThreadPool::wait_for_tasks_to_complete(){
if(m_pool_size == 1)
return;
m_all_tasks_mutex.lock();
/*cout << "waiting for tasks: locked. gonna wait" << endl;*/
m_tasks_loaded = true;
//giving all threads permission to start as all tasks have been added
//using a different variable as number_of_ongoing_tasks is likely to change during the loop
int totalnumtasks = number_of_ongoing_tasks;
for(int i=0;i<totalnumtasks;i++)
sem_post(&semStart);
while ((m_pool_state != STOPPED) && m_tasks_loaded) {
m_all_tasks_cond_var.wait(m_all_tasks_mutex.get_mutex_ptr());
}
/*cout << "waiting for tasks:totall out, must be locked again" << endl;*/
m_all_tasks_mutex.unlock();
//if(zmqthreadpool) cout<<"waiting for all tasks to be done "<<endl;
//if(zmqthreadpool) cout<<"*** semaphore done address wait:"<<&semDone<<endl;
for(int i=0;i<number_of_total_tasks;i++)
sem_wait(&semDone);
number_of_total_tasks = 0;
//if(zmqthreadpool) cout<<"complete"<<endl<<endl;
}
void ThreadPool::setzeromqThread(){
zmqthreadpool = true;
}

View File

@ -26,7 +26,9 @@ public:
int destroy_threadpool();
void* execute_thread();
int add_task(Task* task);
void startExecuting();
void wait_for_tasks_to_complete();
void setzeromqThread();
private:
int m_pool_size;
@ -36,16 +38,16 @@ private:
std::deque<Task*> m_tasks;
volatile int m_pool_state;
Mutex m_all_tasks_mutex;
CondVar m_all_tasks_cond_var;
bool m_tasks_loaded;
bool thread_started;
int current_thread_number;
//volatile uint64_t tasks_done_mask;
volatile int number_of_ongoing_tasks;
volatile int number_of_total_tasks;
sem_t semStart;
sem_t semDone;
bool zmqthreadpool;
};