Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00014 #include "thread-pool.h"
00015 #include <stdlib.h>
00016 #include <boost/thread.hpp>
00017 #include <semaphore.h>
00018 #include "during.h"
00019 #include "utils/io-lock.h"
00020 #include "task-manager.h"
00021
00022 using namespace std;
00023
00024 thread_pool *thread_pool::m_instance = NULL;
00025
00026 thread_pool *thread_pool::get_instance() {
00027 int nb_threads = -1;
00028 const char *env = getenv("SC_DURING_NB_THREADS");
00029 if (env) {
00030 nb_threads = atoi(env);
00031 }
00032 if (nb_threads <= 0) {
00033 nb_threads = boost::thread::hardware_concurrency();
00034 }
00035 return thread_pool::get_instance(nb_threads);
00036 }
00037
00038 thread_pool *thread_pool::get_instance(int nb_threads) {
00039 if (m_instance == NULL) {
00040 m_instance = new thread_pool(nb_threads);
00041 }
00042 return m_instance;
00043 }
00044
00045 void thread_pool::delete_instance() {
00046 if (m_instance) {
00047 L_COUT << "// TEST-IGNORE: Delete thread pool instance" << endl;
00048 delete m_instance;
00049 m_instance = NULL;
00050 }
00051 }
00052
00053 thread_pool::thread_pool(int nb_threads) {
00054 assert(m_instance == NULL);
00055 L_COUT << "// TEST-IGNORE: Initialize producer consumer with " << nb_threads << " threads" << endl;
00056
00057 for (int i = 0; i < nb_threads; i++) {
00058 add_thread();
00059 }
00060 }
00061
00062 thread_pool::~thread_pool() {
00063 terminate_simulation();
00064 }