1 #include "Executor.hxx"
3 #include "Scheduler.hxx"
7 using namespace YACS::ENGINE;
9 using YACS::BASES::Mutex;
10 using YACS::BASES::Thread;
11 using YACS::BASES::Semaphore;
13 Executor::Executor():_nbOfConcurrentThreads(0)//,_cond(PTHREAD_COND_INITIALIZER)
19 for(std::list<Thread *>::iterator iter=_groupOfAllThreadsCreated.begin();iter!=_groupOfAllThreadsCreated.end();iter++)
23 void Executor::RunW(Scheduler *graph)
29 std::vector<Task *> tasks;
30 std::vector<Task *>::iterator iter;
35 sleepWhileNoEventsFromAnyRunningTask();
37 _mutexForSchedulerUpdate.lock();
38 tasks=graph->getNextTasks(isMore);
39 graph->selectRunnableTasks(tasks);
40 _mutexForSchedulerUpdate.unlock();
41 }//End of critical section
42 for(iter=tasks.begin();iter!=tasks.end();iter++)
45 _mutexForSchedulerUpdate.lock();
46 toContinue=!graph->isFinished();
47 _mutexForSchedulerUpdate.unlock();
48 }//End of critical section
53 void Executor::launchTask(Task *task)
55 void **args=new void *[3];
56 _mutexForNbOfConcurrentThreads.lock();
57 _groupOfAllThreadsCreated.push_back(0);
58 std::list<Thread *>::iterator iter=_groupOfAllThreadsCreated.end();
60 _mutexForNbOfConcurrentThreads.unlock();
62 args[1]=(void *)_mainSched;
65 _mutexForSchedulerUpdate.lock();
67 _mainSched->notifyFrom(task,YACS::START);
68 _mutexForSchedulerUpdate.unlock();
69 }//End of critical section
70 _mutexForNbOfConcurrentThreads.lock();
71 //functionForTaskExecution(args);//MultiThreaded=NO
73 new Thread(functionForTaskExecution,args);//MultiThreaded=YES
74 _mutexForNbOfConcurrentThreads.unlock();
77 void Executor::sleepWhileNoEventsFromAnyRunningTask()
79 _semForNewTasksToPerform.wait();
82 void Executor::notifyEndOfThread(YACS::BASES::Thread *thread)
84 /*_mutexForNbOfConcurrentThreads.lock();
85 _groupOfAllThreadsCreated.remove(thread);
87 _mutexForNbOfConcurrentThreads.unlock();*/
90 void Executor::wakeUp()
92 int val=_semForNewTasksToPerform.getValue();
94 _semForNewTasksToPerform.post();
97 int Executor::getNbOfThreads()
100 _mutexForNbOfConcurrentThreads.lock();
101 ret=_groupOfAllThreadsCreated.size();
102 _mutexForNbOfConcurrentThreads.unlock();
106 void *Executor::functionForTaskExecution(void *arg)
108 void **argT=(void **)arg;
109 Task *task=(Task *)argT[0];
110 Scheduler *sched=(Scheduler *)argT[1];
111 Executor *execInst=(Executor *)argT[2];
115 execInst->_mutexForSchedulerUpdate.lock();
117 sched->notifyFrom(task,YACS::FINISH);
118 execInst->_mutexForSchedulerUpdate.unlock();
119 }//End of critical section
121 execInst->notifyEndOfThread(0);