blob: 6ed3d62cdc94ae67b867aa3bcb4286ccd1016caa [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 Afanasyev47cf2ef2013-01-28 15:13:47 -080033#include "logging.h"
34
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080035/* A very simple executor to execute submitted tasks immediately or
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080036 * in the future (depending on whether there is idle thread)
37 * A fixed number of threads are created for executing tasks;
38 * The policy is FIFO
39 * No cancellation of submitted tasks
40 */
41
42class Executor
43{
44public:
45 typedef boost::function<void ()> Job;
46
47 Executor(int poolSize);
48 ~Executor();
49
50 // execute the job immediately or sometime in the future
51 void
52 execute(const Job &job);
53
54 int
55 poolSize();
56
57// only for test
58 int
59 jobQueueSize();
60
Alexander Afanasyevfc720362013-01-24 21:49:48 -080061 void
62 start ();
63
64 void
65 shutdown ();
66
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080067private:
68 void
69 run();
70
71 Job
72 waitForJob();
73
74private:
75 typedef std::deque<Job> JobQueue;
76 typedef boost::mutex Mutex;
77 typedef boost::unique_lock<Mutex> Lock;
78 typedef boost::condition_variable Cond;
79 typedef boost::thread Thread;
80 typedef boost::thread_group ThreadGroup;
81 JobQueue m_queue;
82 Mutex m_mutex;
83 Cond m_cond;
84 ThreadGroup m_group;
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080085
86 volatile bool m_needStop;
Alexander Afanasyevfc720362013-01-24 21:49:48 -080087 int m_poolSize;
Alexander Afanasyev47cf2ef2013-01-28 15:13:47 -080088
89 MEMBER_LOGGER
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080090};
Zhenkai Zhu1888f742013-01-28 12:47:33 -080091
92typedef boost::shared_ptr<Executor> ExecutorPtr;
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080093#endif // EXECUTOR_H