Merge branch 'master' into windows
[ardour.git] / libs / pbd / pthread_utils.cc
1 /*
2     Copyright (C) 2002 Paul Davis 
3
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.
8
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.
13
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.
17
18     $Id$
19 */
20
21 #include <set>
22 #include <string>
23 #include <cstring>
24 #include <stdint.h>
25
26 #include "pbd/pthread_utils.h"
27 #ifdef WINE_THREAD_SUPPORT
28 #include <fst.h>
29 #endif
30
31 using namespace std;
32
33 typedef std::list<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);
37
38 namespace PBD {
39         PBD::Signal4<void,std::string, pthread_t,std::string,uint32_t> ThreadCreatedWithRequestSize;
40 }
41
42 using namespace PBD;
43
44 static int thread_creator (pthread_t* thread_id, const pthread_attr_t* attr, void *(*function)(void*), void* arg)
45 {
46 #ifdef WINE_THREAD_SUPPORT
47        return wine_pthread_create (thread_id, attr, function, arg);
48 #else
49        return pthread_create (thread_id, attr, function, arg);
50 #endif
51 }
52
53 void
54 PBD::notify_gui_about_thread_creation (std::string target_gui, pthread_t thread, std::string str, int request_count)
55 {
56         ThreadCreatedWithRequestSize (target_gui, thread, str, request_count);
57 }
58
59 struct ThreadStartWithName {
60     void* (*thread_work)(void*);
61     void* arg;
62     std::string name;
63     
64     ThreadStartWithName (void* (*f)(void*), void* a, const std::string& s)
65             : thread_work (f), arg (a), name (s) {}
66 };
67
68 static void*
69 fake_thread_start (void* arg)
70 {
71         ThreadStartWithName* ts = (ThreadStartWithName*) arg;
72         void* (*thread_work)(void*) = ts->thread_work;
73         void* thread_arg = ts->arg;
74
75         pthread_set_name (ts->name.c_str());
76
77         delete ts;
78         /* name will be deleted by the default handler for GStaticPrivate, when the thread exits */
79
80         return thread_work (thread_arg);
81 }
82
83 int  
84 pthread_create_and_store (string name, pthread_t  *thread, void * (*start_routine)(void *), void * arg)
85 {
86         pthread_attr_t default_attr;
87         int ret;
88
89         // set default stack size to sensible default for memlocking
90         pthread_attr_init(&default_attr);
91         pthread_attr_setstacksize(&default_attr, 500000);
92
93         ThreadStartWithName* ts = new ThreadStartWithName (start_routine, arg, name);
94
95         if ((ret = thread_creator (thread, &default_attr, fake_thread_start, ts)) == 0) {
96                 pthread_mutex_lock (&thread_map_lock);
97                 all_threads.push_back (*thread);
98                 pthread_mutex_unlock (&thread_map_lock);
99         }
100
101         pthread_attr_destroy(&default_attr);
102
103         return ret;
104 }
105
106 void
107 pthread_set_name (const char *str)
108 {
109         /* copy string and delete it when exiting */
110         
111         thread_name.set (strdup (str));
112 }
113
114 const char *
115 pthread_name ()
116 {
117         const char* str = thread_name.get ();
118
119         if (str) {
120                 return str;
121         } 
122         return "unknown";
123 }
124
125 void
126 pthread_kill_all (int signum) 
127 {       
128         pthread_mutex_lock (&thread_map_lock);
129         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
130                 if (!pthread_equal ((*i), pthread_self())) {
131                         pthread_kill ((*i), signum);
132                 }
133         }
134         all_threads.clear();
135         pthread_mutex_unlock (&thread_map_lock);
136 }
137
138 void
139 pthread_cancel_all () 
140 {       
141         pthread_mutex_lock (&thread_map_lock);
142         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
143                 if (!pthread_equal ((*i), pthread_self())) {
144                         pthread_cancel ((*i));
145                 }
146         }
147         all_threads.clear();
148         pthread_mutex_unlock (&thread_map_lock);
149 }
150
151 void
152 pthread_cancel_one (pthread_t thread) 
153 {       
154         pthread_mutex_lock (&thread_map_lock);
155         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
156                 if (pthread_equal ((*i), thread)) {
157                         all_threads.erase (i);
158                         break;
159                 }
160         }
161
162         pthread_cancel (thread);
163         pthread_mutex_unlock (&thread_map_lock);
164 }
165
166 void
167 pthread_exit_pbd (void* status) 
168 {       
169         pthread_t thread = pthread_self();
170
171         pthread_mutex_lock (&thread_map_lock);
172         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
173                 if (pthread_equal ((*i), thread)) {
174                         all_threads.erase (i);
175                         break;
176                 }
177         }
178         pthread_mutex_unlock (&thread_map_lock);
179         pthread_exit (status);
180 }