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