4 /* Copyright (C) 2002 The gtkmm Development Team
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <glibmm/threadpool.h>
22 #include <glibmm/exceptionhandler.h>
25 #include <glibmmconfig.h>
27 GLIBMM_USING_STD(list)
34 class ThreadPool::SlotList
40 sigc::slot<void>* push(const sigc::slot<void>& slot);
41 sigc::slot<void> pop(sigc::slot<void>* slot_ptr);
43 void lock_and_unlock();
47 std::list< sigc::slot<void> > list_;
50 SlotList(const ThreadPool::SlotList&);
51 ThreadPool::SlotList& operator=(const ThreadPool::SlotList&);
54 ThreadPool::SlotList::SlotList()
57 ThreadPool::SlotList::~SlotList()
60 sigc::slot<void>* ThreadPool::SlotList::push(const sigc::slot<void>& slot)
62 Mutex::Lock lock (mutex_);
64 list_.push_back(slot);
68 sigc::slot<void> ThreadPool::SlotList::pop(sigc::slot<void>* slot_ptr)
70 sigc::slot<void> slot;
73 Mutex::Lock lock (mutex_);
75 std::list< sigc::slot<void> >::iterator pslot = list_.begin();
76 while(pslot != list_.end() && slot_ptr != &*pslot)
79 if(pslot != list_.end())
89 void ThreadPool::SlotList::lock_and_unlock()
101 static void call_thread_entry_slot(void* data, void* user_data)
105 Glib::ThreadPool::SlotList *const slot_list =
106 static_cast<Glib::ThreadPool::SlotList*>(user_data);
108 sigc::slot<void> slot (slot_list->pop(static_cast<sigc::slot<void>*>(data)));
112 catch(Glib::Thread::Exit&)
114 // Just exit from the thread. The Thread::Exit exception
115 // is our sane C++ replacement of g_thread_exit().
119 Glib::exception_handlers_invoke();
123 } // anonymous namespace
129 ThreadPool::ThreadPool(int max_threads, bool exclusive)
132 slot_list_ (new SlotList())
136 gobject_ = g_thread_pool_new(
137 &call_thread_entry_slot, slot_list_, max_threads, exclusive, &error);
143 Glib::Error::throw_exception(error);
147 ThreadPool::~ThreadPool()
150 g_thread_pool_free(gobject_, 1, 1);
154 slot_list_->lock_and_unlock();
159 void ThreadPool::push(const sigc::slot<void>& slot)
161 sigc::slot<void> *const slot_ptr = slot_list_->push(slot);
164 g_thread_pool_push(gobject_, slot_ptr, &error);
168 slot_list_->pop(slot_ptr);
169 Glib::Error::throw_exception(error);
173 void ThreadPool::set_max_threads(int max_threads)
176 g_thread_pool_set_max_threads(gobject_, max_threads, &error);
179 Glib::Error::throw_exception(error);
182 int ThreadPool::get_max_threads() const
184 return g_thread_pool_get_max_threads(gobject_);
187 unsigned int ThreadPool::get_num_threads() const
189 return g_thread_pool_get_num_threads(gobject_);
192 unsigned int ThreadPool::unprocessed() const
194 return g_thread_pool_unprocessed(gobject_);
197 bool ThreadPool::get_exclusive() const
199 g_return_val_if_fail(gobject_ != 0, false);
201 return gobject_->exclusive;
204 void ThreadPool::shutdown(bool immediately)
208 g_thread_pool_free(gobject_, immediately, 1);
214 slot_list_->lock_and_unlock();
221 void ThreadPool::set_max_unused_threads(int max_threads)
223 g_thread_pool_set_max_unused_threads(max_threads);
227 int ThreadPool::get_max_unused_threads()
229 return g_thread_pool_get_max_unused_threads();
233 unsigned int ThreadPool::get_num_unused_threads()
235 return g_thread_pool_get_num_unused_threads();
239 void ThreadPool::stop_unused_threads()
241 g_thread_pool_stop_unused_threads();