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::map<string,pthread_t> ThreadMap;
34 static ThreadMap all_threads;
35 static pthread_mutex_t thread_map_lock = PTHREAD_MUTEX_INITIALIZER;
36 static pthread_mutex_t gui_notify_lock = PTHREAD_MUTEX_INITIALIZER;
39 sigc::signal<void,pthread_t> ThreadLeaving;
40 sigc::signal<void,std::string, pthread_t,std::string,uint32_t> ThreadCreatedWithRequestSize;
45 static int thread_creator (pthread_t* thread_id, const pthread_attr_t* attr, void *(*function)(void*), void* arg)
47 #ifdef WINE_THREAD_SUPPORT
48 return wine_pthread_create (thread_id, attr, function, arg);
50 return pthread_create (thread_id, attr, function, arg);
55 PBD::notify_gui_about_thread_creation (std::string target_gui, pthread_t thread, std::string str, int request_count)
57 pthread_mutex_lock (&gui_notify_lock);
58 ThreadCreatedWithRequestSize (target_gui, thread, str, request_count);
59 pthread_mutex_unlock (&gui_notify_lock);
63 PBD::notify_gui_about_thread_exit (pthread_t thread)
65 pthread_mutex_lock (&gui_notify_lock);
66 ThreadLeaving (thread);
67 pthread_mutex_unlock (&gui_notify_lock);
71 pthread_create_and_store (string name, pthread_t *thread, void * (*start_routine)(void *), void * arg)
73 pthread_attr_t default_attr;
76 // set default stack size to sensible default for memlocking
77 pthread_attr_init(&default_attr);
78 pthread_attr_setstacksize(&default_attr, 500000);
80 if ((ret = thread_creator (thread, &default_attr, start_routine, arg)) == 0) {
81 std::pair<string,pthread_t> newpair;
83 newpair.second = *thread;
85 pthread_mutex_lock (&thread_map_lock);
86 all_threads.insert (newpair);
87 pthread_mutex_unlock (&thread_map_lock);
90 pthread_attr_destroy(&default_attr);
98 pthread_t self = pthread_self();
101 pthread_mutex_lock (&thread_map_lock);
102 for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
103 if (i->second == self) {
105 pthread_mutex_unlock (&thread_map_lock);
109 pthread_mutex_unlock (&thread_map_lock);
114 pthread_kill_all (int signum)
116 pthread_mutex_lock (&thread_map_lock);
117 for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
118 if (i->second != pthread_self()) {
119 pthread_kill (i->second, signum);
123 pthread_mutex_unlock (&thread_map_lock);
127 pthread_cancel_one (pthread_t thread)
129 pthread_mutex_lock (&thread_map_lock);
130 for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
131 if (i->second == thread) {
132 all_threads.erase (i);
137 pthread_cancel (thread);
138 pthread_mutex_unlock (&thread_map_lock);
142 pthread_exit_pbd (void* status)
144 pthread_t thread = pthread_self();
146 pthread_mutex_lock (&thread_map_lock);
147 for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
148 if (i->second == thread) {
149 all_threads.erase (i);
153 pthread_mutex_unlock (&thread_map_lock);
154 pthread_exit (status);