Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00014 #include "thread-queue.h"
00015 #include "task-manager.h"
00016 #include "thread-pool.h"
00017 #include "assert.h"
00018 #include <boost/thread.hpp>
00019 #include <boost/lexical_cast.hpp>
00020 #include <boost/bind.hpp>
00021 #include <unistd.h>
00022 #include <iostream>
00023 #include "utils/io-lock.h"
00024 #include "utils/measure-time.h"
00025 #include "utils/atomic-variable.h"
00026 #include <systemc>
00027 #include "utils/big-lock.h"
00028
00029 using namespace std;
00030 using namespace sc_core;
00031
00032 atomic_variable<int> count_f(0);
00033
00034 void f() {
00035 count_f++;
00036 }
00037 void g() {
00038 count_f--;
00039 }
00040
00041 void send_requests(int n, boost::function<void()> fn) {
00042 for (int i = 0; i < n; i++) {
00043 big_lock<0> l;
00044 sync_task t(fn);
00045 l.unlock();
00046 thread_pool::get_instance()->queue(&t);
00047 t.wait_nosc();
00048 }
00049 }
00050
00051
00052 void test_pool(int nb_producers, int nb_req, boost::function<void()> fn) {
00053 boost::thread **ta = new boost::thread *[nb_producers];
00054
00055
00056 for (int i = 0; i < nb_producers; ++i)
00057 ta[i] = new boost::thread(send_requests, nb_req, fn);
00058 for (int i = 0; i < nb_producers; ++i)
00059 ta[i]->join();
00060 delete [] ta;
00061 }
00062
00063
00064 const int N = 10;
00065
00066 int sc_main (int argc, char **argv) {
00067 (void)argc; (void)argv;
00068 thread_pool::get_instance();
00069 L_COUT << "Increase count" << endl;
00070 for (int i = 1; i <= N; ++i)
00071 for (int j = 1; j <= N; ++j) {
00072 count_f.write(0);
00073 test_pool(i, j, boost::bind(f));
00074 assert(count_f.read() == i * j);
00075 }
00076 L_COUT << "Mixed ++ and --" << endl;
00077 for (int i = 1; i <= N; ++i)
00078 for (int j = 1; j <= N; ++j) {
00079 count_f.write(0);
00080 boost::function<void()> bf = boost::bind(g);
00081 boost::thread t(send_requests, i*j, bf);
00082 test_pool(i, j, boost::bind(f));
00083 t.join();
00084 assert(count_f.read() == 0);
00085 }
00086 L_COUT << "Done." << endl;
00087 return 0;
00088 }