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 | * |
| 18 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 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) |
| 31 | : m_needStop (false) |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 32 | { |
| 33 | for (int i = 0; i < poolSize; i++) |
| 34 | { |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame^] | 35 | m_group.create_thread (bind(&Executor::run, this)); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
| 39 | Executor::~Executor() |
| 40 | { |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame^] | 41 | _LOG_DEBUG ("Enter destructor"); |
| 42 | m_needStop = true; |
| 43 | m_group.interrupt_all (); |
| 44 | m_group.join_all (); |
| 45 | _LOG_DEBUG ("Exit destructor"); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void |
| 49 | Executor::execute(const Job &job) |
| 50 | { |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame^] | 51 | _LOG_DEBUG ("Add to job queue"); |
| 52 | |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 53 | Lock lock(m_mutex); |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame^] | 54 | bool queueWasEmpty = m_queue.empty (); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 55 | m_queue.push_back(job); |
| 56 | |
| 57 | // notify working threads if the queue was empty |
| 58 | if (queueWasEmpty) |
| 59 | { |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame^] | 60 | m_cond.notify_one (); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | int |
| 65 | Executor::poolSize() |
| 66 | { |
| 67 | return m_group.size(); |
| 68 | } |
| 69 | |
| 70 | int |
| 71 | Executor::jobQueueSize() |
| 72 | { |
| 73 | Lock lock(m_mutex); |
| 74 | return m_queue.size(); |
| 75 | } |
| 76 | |
| 77 | void |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame^] | 78 | Executor::run () |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 79 | { |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame^] | 80 | _LOG_DEBUG ("Start thread"); |
| 81 | |
| 82 | while(!m_needStop) |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 83 | { |
| 84 | Job job = waitForJob(); |
| 85 | |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame^] | 86 | job (); // even if job is "null", nothing bad will happen |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 87 | } |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame^] | 88 | |
| 89 | _LOG_DEBUG ("Thread finished"); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | Executor::Job |
| 93 | Executor::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 Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame^] | 103 | _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 Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 111 | return job; |
| 112 | } |