blob: 67dd74aac1d6e886269e9585e8915fc8603d20d7 [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 *
18 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
22#include "executor.h"
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080023#include "logging.h"
24
25INIT_LOGGER ("Executor");
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080026
27using namespace std;
28using namespace boost;
29
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080030Executor::Executor (int poolSize)
31 : m_needStop (false)
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080032{
33 for (int i = 0; i < poolSize; i++)
34 {
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080035 m_group.create_thread (bind(&Executor::run, this));
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080036 }
37}
38
39Executor::~Executor()
40{
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080041 _LOG_DEBUG ("Enter destructor");
42 m_needStop = true;
43 m_group.interrupt_all ();
44 m_group.join_all ();
45 _LOG_DEBUG ("Exit destructor");
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080046}
47
48void
49Executor::execute(const Job &job)
50{
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080051 _LOG_DEBUG ("Add to job queue");
52
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080053 Lock lock(m_mutex);
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080054 bool queueWasEmpty = m_queue.empty ();
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080055 m_queue.push_back(job);
56
57 // notify working threads if the queue was empty
58 if (queueWasEmpty)
59 {
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080060 m_cond.notify_one ();
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080061 }
62}
63
64int
65Executor::poolSize()
66{
67 return m_group.size();
68}
69
70int
71Executor::jobQueueSize()
72{
73 Lock lock(m_mutex);
74 return m_queue.size();
75}
76
77void
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080078Executor::run ()
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080079{
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080080 _LOG_DEBUG ("Start thread");
81
82 while(!m_needStop)
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080083 {
84 Job job = waitForJob();
85
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080086 job (); // even if job is "null", nothing bad will happen
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080087 }
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080088
89 _LOG_DEBUG ("Thread finished");
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080090}
91
92Executor::Job
93Executor::waitForJob()
94{
95 Lock lock(m_mutex);
96
97 // wait until job queue is not empty
98 while (m_queue.empty())
99 {
100 m_cond.wait(lock);
101 }
102
Alexander Afanasyevab5dff72013-01-24 10:25:28 -0800103 _LOG_DEBUG ("Got signal on condition");
104
105 Job job;
106 if (!m_queue.empty ()) // this is not always guaranteed, especially after interruption from destructor
107 {
108 job = m_queue.front();
109 m_queue.pop_front();
110 }
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -0800111 return job;
112}