blob: c8e6cb0373a85501d35a45e8170e46d93c400978 [file] [log] [blame]
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080018 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080020 */
21
22#ifndef EXECUTOR_H
23#define EXECUTOR_H
24
25#include <boost/function.hpp>
Zhenkai Zhu1888f742013-01-28 12:47:33 -080026#include <boost/shared_ptr.hpp>
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080027#include <boost/thread/condition_variable.hpp>
28#include <boost/thread/mutex.hpp>
29#include <boost/thread/locks.hpp>
30#include <boost/thread/thread.hpp>
31#include <deque>
32
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080033/* A very simple executor to execute submitted tasks immediately or
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080034 * in the future (depending on whether there is idle thread)
35 * A fixed number of threads are created for executing tasks;
36 * The policy is FIFO
37 * No cancellation of submitted tasks
38 */
39
40class Executor
41{
42public:
43 typedef boost::function<void ()> Job;
44
45 Executor(int poolSize);
46 ~Executor();
47
48 // execute the job immediately or sometime in the future
49 void
50 execute(const Job &job);
51
52 int
53 poolSize();
54
55// only for test
56 int
57 jobQueueSize();
58
Alexander Afanasyevfc720362013-01-24 21:49:48 -080059 void
60 start ();
61
62 void
63 shutdown ();
64
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080065private:
66 void
67 run();
68
69 Job
70 waitForJob();
71
72private:
73 typedef std::deque<Job> JobQueue;
74 typedef boost::mutex Mutex;
75 typedef boost::unique_lock<Mutex> Lock;
76 typedef boost::condition_variable Cond;
77 typedef boost::thread Thread;
78 typedef boost::thread_group ThreadGroup;
79 JobQueue m_queue;
80 Mutex m_mutex;
81 Cond m_cond;
82 ThreadGroup m_group;
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080083
84 volatile bool m_needStop;
Alexander Afanasyevfc720362013-01-24 21:49:48 -080085 int m_poolSize;
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080086};
Zhenkai Zhu1888f742013-01-28 12:47:33 -080087
88typedef boost::shared_ptr<Executor> ExecutorPtr;
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080089#endif // EXECUTOR_H