summaryrefslogtreecommitdiff
path: root/glib/glibmm/threadpool.cc
blob: a007a4351217fa1e784ac37d86ec453aad1b4db1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// -*- c++ -*-

/* Copyright (C) 2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <glibmmconfig.h>
#include <glibmm/threadpool.h>
#include <glibmm/exceptionhandler.h>
#include <glibmm/threads.h>
#include <glib.h>
#include <list>

namespace Glib
{

// internal
class ThreadPool::SlotList
{
public:
  SlotList();
  ~SlotList();

  // noncopyable
  SlotList(const ThreadPool::SlotList&) = delete;
  ThreadPool::SlotList& operator=(const ThreadPool::SlotList&) = delete;

  sigc::slot<void>* push(const sigc::slot<void>& slot);
  sigc::slot<void>  pop(sigc::slot<void>* slot_ptr);

  void lock_and_unlock();

private:
  Glib::Threads::Mutex                     mutex_;
  std::list< sigc::slot<void> >  list_;
};

ThreadPool::SlotList::SlotList()
{}

ThreadPool::SlotList::~SlotList()
{}

sigc::slot<void>* ThreadPool::SlotList::push(const sigc::slot<void>& slot)
{
  Threads::Mutex::Lock lock (mutex_);

  list_.push_back(slot);
  return &list_.back();
}

sigc::slot<void> ThreadPool::SlotList::pop(sigc::slot<void>* slot_ptr)
{
  sigc::slot<void> slot;

  {
    Threads::Mutex::Lock lock (mutex_);

    std::list< sigc::slot<void> >::iterator pslot = list_.begin();
    while(pslot != list_.end() && slot_ptr != &*pslot)
      ++pslot;

    if(pslot != list_.end())
    {
      slot = *pslot;
      list_.erase(pslot);
    }
  }

  return slot;
}

void ThreadPool::SlotList::lock_and_unlock()
{
  mutex_.lock();
  mutex_.unlock();
}

} // namespace Glib


namespace
{

static void call_thread_entry_slot(void* data, void* user_data)
{
  try
  {
    Glib::ThreadPool::SlotList *const slot_list =
        static_cast<Glib::ThreadPool::SlotList*>(user_data);

    sigc::slot<void> slot (slot_list->pop(static_cast<sigc::slot<void>*>(data)));

    slot();
  }
  catch(Glib::Threads::Thread::Exit&)
  {
    // Just exit from the thread.  The Thread::Exit exception
    // is our sane C++ replacement of g_thread_exit().
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
}

} // anonymous namespace


namespace Glib
{

ThreadPool::ThreadPool(int max_threads, bool exclusive)
:
  gobject_   (0),
  slot_list_ (new SlotList())
{
  GError* error = nullptr;

  gobject_ = g_thread_pool_new(
      &call_thread_entry_slot, slot_list_, max_threads, exclusive, &error);

  if(error)
  {
    delete slot_list_;
    slot_list_ = nullptr;
    Glib::Error::throw_exception(error);
  }
}

ThreadPool::~ThreadPool()
{
  if(gobject_)
    g_thread_pool_free(gobject_, 1, 1);

  if(slot_list_)
  {
    slot_list_->lock_and_unlock();
    delete slot_list_;
  }
}

void ThreadPool::push(const sigc::slot<void>& slot)
{
  sigc::slot<void> *const slot_ptr = slot_list_->push(slot);

  GError* error = nullptr;
  g_thread_pool_push(gobject_, slot_ptr, &error);

  if(error)
  {
    slot_list_->pop(slot_ptr);
    Glib::Error::throw_exception(error);
  }
}

void ThreadPool::set_max_threads(int max_threads)
{
  GError* error = nullptr;
  g_thread_pool_set_max_threads(gobject_, max_threads, &error);

  if(error)
    Glib::Error::throw_exception(error);
}

int ThreadPool::get_max_threads() const
{
  return g_thread_pool_get_max_threads(gobject_);
}

unsigned int ThreadPool::get_num_threads() const
{
  return g_thread_pool_get_num_threads(gobject_);
}

unsigned int ThreadPool::unprocessed() const
{
  return g_thread_pool_unprocessed(gobject_);
}

bool ThreadPool::get_exclusive() const
{
  g_return_val_if_fail(gobject_ != nullptr, false);

  return gobject_->exclusive;
}

void ThreadPool::shutdown(bool immediately)
{
  if(gobject_)
  {
    g_thread_pool_free(gobject_, immediately, 1);
    gobject_ = nullptr;
  }

  if(slot_list_)
  {
    slot_list_->lock_and_unlock();
    delete slot_list_;
    slot_list_ = nullptr;
  }
}

// static
void ThreadPool::set_max_unused_threads(int max_threads)
{
  g_thread_pool_set_max_unused_threads(max_threads);
}

// static
int ThreadPool::get_max_unused_threads()
{
  return g_thread_pool_get_max_unused_threads();
}

// static
unsigned int ThreadPool::get_num_unused_threads()
{
  return g_thread_pool_get_num_unused_threads();
}

// static
void ThreadPool::stop_unused_threads()
{
  g_thread_pool_stop_unused_threads();
}

} // namespace Glib