Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame^] | 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "executor.h" |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 23 | #include "logging.h" |
| 24 | |
| 25 | INIT_LOGGER ("Executor"); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 26 | |
| 27 | using namespace std; |
| 28 | using namespace boost; |
| 29 | |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 30 | Executor::Executor (int poolSize) |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 31 | : m_needStop (true) |
| 32 | , m_poolSize (poolSize) |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 33 | { |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | Executor::~Executor() |
| 37 | { |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 38 | _LOG_DEBUG ("Enter destructor"); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 39 | shutdown (); |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 40 | _LOG_DEBUG ("Exit destructor"); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 44 | Executor::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 | |
| 56 | void |
| 57 | Executor::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 | |
| 70 | void |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 71 | Executor::execute(const Job &job) |
| 72 | { |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 73 | _LOG_DEBUG ("Add to job queue"); |
| 74 | |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 75 | Lock lock(m_mutex); |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 76 | bool queueWasEmpty = m_queue.empty (); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 77 | m_queue.push_back(job); |
| 78 | |
| 79 | // notify working threads if the queue was empty |
| 80 | if (queueWasEmpty) |
| 81 | { |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 82 | m_cond.notify_one (); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | int |
| 87 | Executor::poolSize() |
| 88 | { |
| 89 | return m_group.size(); |
| 90 | } |
| 91 | |
| 92 | int |
| 93 | Executor::jobQueueSize() |
| 94 | { |
| 95 | Lock lock(m_mutex); |
| 96 | return m_queue.size(); |
| 97 | } |
| 98 | |
| 99 | void |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 100 | Executor::run () |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 101 | { |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 102 | _LOG_DEBUG ("Start thread"); |
| 103 | |
| 104 | while(!m_needStop) |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 105 | { |
| 106 | Job job = waitForJob(); |
| 107 | |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 108 | _LOG_DEBUG (">>> enter job"); |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 109 | job (); // even if job is "null", nothing bad will happen |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 110 | _LOG_DEBUG ("<<< exit job"); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 111 | } |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 113 | _LOG_DEBUG ("Executor thread finished"); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | Executor::Job |
| 117 | Executor::waitForJob() |
| 118 | { |
| 119 | Lock lock(m_mutex); |
| 120 | |
| 121 | // wait until job queue is not empty |
| 122 | while (m_queue.empty()) |
| 123 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 124 | _LOG_DEBUG ("Unlocking mutex for wait"); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 125 | m_cond.wait(lock); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 126 | _LOG_DEBUG ("Re-locking mutex after wait"); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 129 | _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 Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 137 | return job; |
| 138 | } |