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>
30 typedef std::map<string,pthread_t> ThreadMap;
31 static ThreadMap all_threads;
32 static pthread_mutex_t thread_map_lock = PTHREAD_MUTEX_INITIALIZER;
35 sigc::signal<void,pthread_t,std::string> ThreadCreated;
36 sigc::signal<void,pthread_t,std::string,uint32_t> ThreadCreatedWithRequestSize;
42 pthread_create_and_store (string name, pthread_t *thread, pthread_attr_t *attr, void * (*start_routine)(void *), void * arg)
46 if ((ret = pthread_create (thread, attr, start_routine, arg)) == 0) {
47 std::pair<string,pthread_t> newpair;
49 newpair.second = *thread;
51 pthread_mutex_lock (&thread_map_lock);
52 all_threads.insert (newpair);
54 pthread_mutex_unlock (&thread_map_lock);
63 pthread_t self = pthread_self();
66 pthread_mutex_lock (&thread_map_lock);
67 for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
68 if (i->second == self) {
70 pthread_mutex_unlock (&thread_map_lock);
74 pthread_mutex_unlock (&thread_map_lock);
79 pthread_kill_all (int signum)
81 pthread_mutex_lock (&thread_map_lock);
82 for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
83 if (i->second != pthread_self()) {
84 pthread_kill (i->second, signum);
88 pthread_mutex_unlock (&thread_map_lock);
94 pthread_mutex_lock (&thread_map_lock);
95 for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
96 if (i->second != pthread_self()) {
97 pthread_cancel (i->second);
101 pthread_mutex_unlock (&thread_map_lock);
105 pthread_cancel_one (pthread_t thread)
107 pthread_mutex_lock (&thread_map_lock);
108 for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
109 if (i->second == thread) {
110 all_threads.erase (i);
115 pthread_cancel (thread);
116 pthread_mutex_unlock (&thread_map_lock);
120 pthread_exit_pbd (void* status)
122 pthread_t thread = pthread_self();
124 pthread_mutex_lock (&thread_map_lock);
125 for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
126 if (i->second == thread) {
127 all_threads.erase (i);
131 pthread_mutex_unlock (&thread_map_lock);
132 pthread_exit (status);