Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00013 #ifndef THREAD_POOL_ONDEMAND_H
00014 #define THREAD_POOL_ONDEMAND_H
00015
00016 #include <systemc>
00017 #include "thread-pool-base.h"
00018 #include "sync-task.h"
00019 #include "utils/io-lock.h"
00020
00024 class thread_pool_ondemand : public thread_pool_base {
00025 public:
00026 static thread_pool_ondemand *get_instance();
00027 static void delete_instance();
00028
00029 void queue(sync_task *r, sc_core::sc_process_b *current = NULL) {
00030 D_COUT << "// TEST-IGNORE: Queuing task " << r << std::endl;
00031 boost::unique_lock<boost::mutex> lock(m_mut);
00032 if (current == NULL) {
00033 current = sc_core::sc_get_curr_simcontext()
00034 ->get_curr_proc_info()
00035 ->process_handle;
00036 }
00037 assert(current != NULL);
00038 task_and_consumer &tac = m_task_map[current];
00039 tac.m_task = r;
00040 tac.m_done = (r == NULL);
00041 if (tac.m_thread == NULL)
00042 tac.m_thread = add_thread(current);
00043
00044
00045
00046
00047 m_cond.notify_all();
00048 }
00049 private:
00050 sync_task *pop_task(sc_core::sc_process_b *current) {
00051 boost::unique_lock<boost::mutex> lock(m_mut);
00052 assert(current != NULL);
00053 task_and_consumer &tac = m_task_map[current];
00054 assert(tac.m_thread->get_id() == boost::this_thread::get_id());
00055 while(tac.m_task == NULL
00056 && tac.m_done == false) {
00057 m_cond.wait(lock);
00058 }
00059 sync_task *res = tac.m_task;
00060 tac.m_task = NULL;
00061 D_COUT << "// TEST-IGNORE: Got a task " << res << std::endl;
00062 return res;
00063 }
00064
00065 private:
00066 static thread_pool_ondemand *m_instance;
00067 thread_pool_ondemand();
00068 ~thread_pool_ondemand();
00069
00070 sync_task *m_pending_task;
00071
00072 struct task_and_consumer {
00073 sync_task *m_task;
00074 thread_ptr m_thread;
00075 bool m_done;
00076 task_and_consumer()
00077 : m_task(NULL), m_thread(NULL), m_done(false) {}
00078 };
00079
00080 typedef std::map<sc_core::sc_process_b *, task_and_consumer> task_map_t;
00081 task_map_t m_task_map;
00082
00083 boost::condition_variable m_cond;
00084 boost::mutex m_mut;
00085 };
00086
00087 #endif // THREAD_POOL_ONDEMAND_H