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
32 DECLARE_DEFAULT_COMPARISONS(pthread_t) // Needed for 'DECLARE_DEFAULT_COMPARISONS'. Objects in an STL container can be
33 // searched and sorted. Thus, when instantiating the container, MSVC complains
34 // if the type of object being contained has no appropriate comparison operators
35 // defined (specifically, if operators '<' and '==' are undefined). This seems
36 // to be the case with ptw32 'pthread_t' which is a simple struct.
41 typedef std::list<pthread_t> ThreadMap;
42 static ThreadMap all_threads;
43 static pthread_mutex_t thread_map_lock = PTHREAD_MUTEX_INITIALIZER;
44 static Glib::Threads::Private<char> thread_name (free);
47 PBD::Signal3<void,pthread_t,std::string,uint32_t> ThreadCreatedWithRequestSize;
52 static int thread_creator (pthread_t* thread_id, const pthread_attr_t* attr, void *(*function)(void*), void* arg)
54 #ifdef WINE_THREAD_SUPPORT
55 return wine_pthread_create (thread_id, attr, function, arg);
57 return pthread_create (thread_id, attr, function, arg);
63 PBD::notify_event_loops_about_thread_creation (pthread_t thread, const std::string& emitting_thread_name, int request_count)
65 /* notify threads that may exist in the future (they may also exist
66 * already, in which case they will catch the
67 * ThreadCreatedWithRequestSize signal)
69 EventLoop::pre_register (emitting_thread_name, request_count);
71 /* notify all existing threads */
72 ThreadCreatedWithRequestSize (thread, emitting_thread_name, request_count);
75 struct ThreadStartWithName {
76 void* (*thread_work)(void*);
80 ThreadStartWithName (void* (*f)(void*), void* a, const std::string& s)
81 : thread_work (f), arg (a), name (s) {}
85 fake_thread_start (void* arg)
87 ThreadStartWithName* ts = (ThreadStartWithName*) arg;
88 void* (*thread_work)(void*) = ts->thread_work;
89 void* thread_arg = ts->arg;
91 /* name will be deleted by the default handler for GStaticPrivate, when the thread exits */
93 pthread_set_name (ts->name.c_str());
95 /* we don't need this object anymore */
99 /* actually run the thread's work function */
101 void* ret = thread_work (thread_arg);
105 pthread_mutex_lock (&thread_map_lock);
107 for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
108 if (pthread_equal ((*i), pthread_self())) {
109 all_threads.erase (i);
114 pthread_mutex_unlock (&thread_map_lock);
122 pthread_create_and_store (string name, pthread_t *thread, void * (*start_routine)(void *), void * arg)
124 pthread_attr_t default_attr;
127 // set default stack size to sensible default for memlocking
128 pthread_attr_init(&default_attr);
129 pthread_attr_setstacksize(&default_attr, 500000);
131 ThreadStartWithName* ts = new ThreadStartWithName (start_routine, arg, name);
133 if ((ret = thread_creator (thread, &default_attr, fake_thread_start, ts)) == 0) {
134 pthread_mutex_lock (&thread_map_lock);
135 all_threads.push_back (*thread);
136 pthread_mutex_unlock (&thread_map_lock);
139 pthread_attr_destroy(&default_attr);
145 pthread_set_name (const char *str)
147 /* copy string and delete it when exiting */
149 thread_name.set (strdup (str)); // leaks
155 const char* str = thread_name.get ();
164 pthread_kill_all (int signum)
166 pthread_mutex_lock (&thread_map_lock);
167 for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
168 if (!pthread_equal ((*i), pthread_self())) {
169 pthread_kill ((*i), signum);
173 pthread_mutex_unlock (&thread_map_lock);
177 pthread_cancel_all ()
179 pthread_mutex_lock (&thread_map_lock);
181 for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ) {
183 ThreadMap::iterator nxt = i;
186 if (!pthread_equal ((*i), pthread_self())) {
187 pthread_cancel ((*i));
193 pthread_mutex_unlock (&thread_map_lock);
197 pthread_cancel_one (pthread_t thread)
199 pthread_mutex_lock (&thread_map_lock);
200 for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
201 if (pthread_equal ((*i), thread)) {
202 all_threads.erase (i);
207 pthread_cancel (thread);
208 pthread_mutex_unlock (&thread_map_lock);