/*
* This file is part of the Omni C++ framework
*
* Copyright (c) 2016, Zeriph Enterprises, LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of Zeriph, Zeriph Enterprises, LLC, nor the names
* of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ZERIPH AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ZERIPH AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if !defined(OMNI_THREADPOOL_HPP)
#define OMNI_THREADPOOL_HPP 1
#include <omni/defs/class_macros.hpp>
#include <omni/types/threadpool_t.hpp>
#include <omni/sync/basic_thread.hpp>
#include <omni/sync/basic_lock.hpp>
#include <list>
namespace omni {
namespace sync {
class threadpool
{
public:
threadpool();
threadpool(std::size_t thread_min, std::size_t thread_max);
~threadpool();
std::size_t available_threads()
const;
std::size_t active_threads()
const;
void clear_queue();
std::size_t max_threads()
const;
std::size_t min_threads()
const;
void queue(
const @parameterized_thread_start.html">omni::sync::parameterized_thread_start& task);
void queue(
const @parameterized_thread_start.html">omni::sync::parameterized_thread_start& task,
omni::generic_ptr param);
void queue(
const omni::sync::threadpool_task& task);
std::size_t queue_size()
const;
bool set_max_threads(std::size_t count);
bool set_min_threads(std::size_t count);
void wait_active_queue()
const;
OMNI_MEMBERS_FW(
omni::sync::threadpool)
// disposing,name,type(),hash()
private:
// TODO: does it make sense to copy?
threadpool(
const threadpool &cp);
// = delete
omni::sync::threadpool&
operator=(
const omni::sync::threadpool &other);
// = delete
typedef std::list<
omni::sync::basic_thread>::iterator omni_threadpool_itr;
// Methods
void _add_queue(
const omni::sync::threadpool_task& task);
omni_threadpool_itr _create_thread();
void _initialize_min(std::size_t tmin);
void _thread_fn();
// Members
#if defined(OMNI_TYPE_INFO)
omni::type<
omni::sync::threadpool> m_type;
#endif
std::size_t m_act;
// current active
std::size_t m_min;
// minimum thread count
std::size_t m_max;
// maximum thread count
mutable omni::sync::basic_lock m_mtx;
// threadpool lock
std::list<
omni::sync::basic_thread> m_threads;
std::list<
omni::sync::threadpool_task> m_tasks;
volatile bool m_isdestroy;
};
}
}
#endif // OMNI_THREADPOOL_HPP