blob: b1d3593be0f32c4706663a489e99b39c30810549 [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#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)
Alexander Afanasyevfc720362013-01-24 21:49:48 -080031 : m_needStop (true)
32 , m_poolSize (poolSize)
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080033{
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080034}
35
36Executor::~Executor()
37{
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080038 _LOG_DEBUG ("Enter destructor");
Alexander Afanasyevfc720362013-01-24 21:49:48 -080039 shutdown ();
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080040 _LOG_DEBUG ("Exit destructor");
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080041}
42
43void
Alexander Afanasyevfc720362013-01-24 21:49:48 -080044Executor::start ()
45{
46 if (m_needStop)
47 {
48 m_needStop = false;
49 for (int i = 0; i < m_poolSize; i++)
50 {
51 m_group.create_thread (bind(&Executor::run, this));
52 }
53 }
54}
55
56void
57Executor::shutdown ()
58{
59 if (!m_needStop)
60 {
61 m_needStop = true;
62 _LOG_DEBUG ("Iterrupting all");
63 m_group.interrupt_all ();
64 _LOG_DEBUG ("Join all");
65 m_group.join_all ();
66 }
67}
68
69
70void
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080071Executor::execute(const Job &job)
72{
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080073 _LOG_DEBUG ("Add to job queue");
74
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080075 Lock lock(m_mutex);
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080076 bool queueWasEmpty = m_queue.empty ();
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080077 m_queue.push_back(job);
78
79 // notify working threads if the queue was empty
80 if (queueWasEmpty)
81 {
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080082 m_cond.notify_one ();
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080083 }
84}
85
86int
87Executor::poolSize()
88{
89 return m_group.size();
90}
91
92int
93Executor::jobQueueSize()
94{
95 Lock lock(m_mutex);
96 return m_queue.size();
97}
98
99void
Alexander Afanasyevab5dff72013-01-24 10:25:28 -0800100Executor::run ()
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -0800101{
Alexander Afanasyevab5dff72013-01-24 10:25:28 -0800102 _LOG_DEBUG ("Start thread");
103
104 while(!m_needStop)
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -0800105 {
106 Job job = waitForJob();
107
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800108 _LOG_DEBUG (">>> enter job");
Alexander Afanasyevab5dff72013-01-24 10:25:28 -0800109 job (); // even if job is "null", nothing bad will happen
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800110 _LOG_DEBUG ("<<< exit job");
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -0800111 }
Alexander Afanasyevab5dff72013-01-24 10:25:28 -0800112
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800113 _LOG_DEBUG ("Executor thread finished");
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -0800114}
115
116Executor::Job
117Executor::waitForJob()
118{
119 Lock lock(m_mutex);
120
121 // wait until job queue is not empty
122 while (m_queue.empty())
123 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800124 _LOG_DEBUG ("Unlocking mutex for wait");
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -0800125 m_cond.wait(lock);
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800126 _LOG_DEBUG ("Re-locking mutex after wait");
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -0800127 }
128
Alexander Afanasyevab5dff72013-01-24 10:25:28 -0800129 _LOG_DEBUG ("Got signal on condition");
130
131 Job job;
132 if (!m_queue.empty ()) // this is not always guaranteed, especially after interruption from destructor
133 {
134 job = m_queue.front();
135 m_queue.pop_front();
136 }
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -0800137 return job;
138}