2
Copyright (C) 2002 Paul Davis
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
26
#include "pbd/pthread_utils.h"
27
#ifdef WINE_THREAD_SUPPORT
33
typedef std::set<pthread_t> ThreadMap;
34
static ThreadMap all_threads;
35
static pthread_mutex_t thread_map_lock = PTHREAD_MUTEX_INITIALIZER;
36
static Glib::Threads::Private<char> thread_name (free);
39
PBD::Signal4<void,std::string, pthread_t,std::string,uint32_t> ThreadCreatedWithRequestSize;
44
static int thread_creator (pthread_t* thread_id, const pthread_attr_t* attr, void *(*function)(void*), void* arg)
46
#ifdef WINE_THREAD_SUPPORT
47
return wine_pthread_create (thread_id, attr, function, arg);
49
return pthread_create (thread_id, attr, function, arg);
54
PBD::notify_gui_about_thread_creation (std::string target_gui, pthread_t thread, std::string str, int request_count)
56
ThreadCreatedWithRequestSize (target_gui, thread, str, request_count);
59
struct ThreadStartWithName {
60
void* (*thread_work)(void*);
64
ThreadStartWithName (void* (*f)(void*), void* a, const std::string& s)
65
: thread_work (f), arg (a), name (s) {}
69
fake_thread_start (void* arg)
71
ThreadStartWithName* ts = (ThreadStartWithName*) arg;
72
void* (*thread_work)(void*) = ts->thread_work;
73
void* thread_arg = ts->arg;
75
/* name will be deleted by the default handler for GStaticPrivate, when the thread exits */
77
pthread_set_name (ts->name.c_str());
79
/* we don't need this object anymore */
83
/* actually run the thread's work function */
85
void* ret = thread_work (thread_arg);
89
pthread_mutex_lock (&thread_map_lock);
91
for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
92
if (pthread_equal ((*i), pthread_self())) {
93
all_threads.erase (i);
98
pthread_mutex_unlock (&thread_map_lock);
106
pthread_create_and_store (string name, pthread_t *thread, void * (*start_routine)(void *), void * arg)
108
pthread_attr_t default_attr;
111
// set default stack size to sensible default for memlocking
112
pthread_attr_init(&default_attr);
113
pthread_attr_setstacksize(&default_attr, 500000);
115
ThreadStartWithName* ts = new ThreadStartWithName (start_routine, arg, name);
117
if ((ret = thread_creator (thread, &default_attr, fake_thread_start, ts)) == 0) {
118
pthread_mutex_lock (&thread_map_lock);
119
all_threads.insert (*thread);
120
pthread_mutex_unlock (&thread_map_lock);
123
pthread_attr_destroy(&default_attr);
129
pthread_set_name (const char *str)
131
/* copy string and delete it when exiting */
133
thread_name.set (strdup (str));
139
const char* str = thread_name.get ();
148
pthread_kill_all (int signum)
150
pthread_mutex_lock (&thread_map_lock);
151
for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
152
if ((*i) != pthread_self()) {
153
pthread_kill ((*i), signum);
157
pthread_mutex_unlock (&thread_map_lock);
161
pthread_cancel_all ()
163
pthread_mutex_lock (&thread_map_lock);
164
for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ) {
166
ThreadMap::iterator nxt = i;
169
if (!pthread_equal ((*i), pthread_self())) {
170
pthread_cancel ((*i));
176
pthread_mutex_unlock (&thread_map_lock);
180
pthread_cancel_one (pthread_t thread)
182
pthread_mutex_lock (&thread_map_lock);
183
for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
184
if (pthread_equal ((*i), thread)) {
185
all_threads.erase (i);
190
pthread_cancel (thread);
191
pthread_mutex_unlock (&thread_map_lock);