VST omnibus commit edition: use wine_pthread_create() everywhere instead of pthread_c...
[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 <map>
22 #include <iostream>
23 #include <string>
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::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;
37
38 namespace PBD {
39    sigc::signal<void,pthread_t>             ThreadLeaving;
40    sigc::signal<void,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 (pthread_t thread, std::string str, int request_count)
56 {
57         pthread_mutex_lock (&gui_notify_lock);
58         ThreadCreatedWithRequestSize (thread, str, request_count);
59         pthread_mutex_unlock (&gui_notify_lock);
60 }
61
62 void
63 PBD::notify_gui_about_thread_exit (pthread_t thread)
64 {
65         pthread_mutex_lock (&gui_notify_lock);
66         ThreadLeaving (thread);
67         pthread_mutex_unlock (&gui_notify_lock);
68 }
69
70 int  
71 pthread_create_and_store (string name, pthread_t  *thread, pthread_attr_t *attr, void * (*start_routine)(void *), void * arg)
72 {
73         int ret;
74
75         pthread_attr_t default_attr;
76         bool use_default_attr = (attr == NULL);
77         
78         if (use_default_attr) {
79                 // set default stack size to sensible default for memlocking
80                 pthread_attr_init(&default_attr);
81                 pthread_attr_setstacksize(&default_attr, 500000);
82                 attr = &default_attr;
83         }
84
85         if ((ret = thread_creator (thread, attr, start_routine, arg)) == 0) {
86                 std::pair<string,pthread_t> newpair;
87                 newpair.first = name;
88                 newpair.second = *thread;
89
90                 pthread_mutex_lock (&thread_map_lock);
91                 all_threads.insert (newpair);
92
93                 pthread_mutex_unlock (&thread_map_lock);
94         }
95
96         if (use_default_attr) {
97                 pthread_attr_destroy(&default_attr);
98         }
99         
100         return ret;
101 }
102
103 string
104 pthread_name ()
105 {
106         pthread_t self = pthread_self();
107         string str;
108
109         pthread_mutex_lock (&thread_map_lock);
110         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
111                 if (i->second == self) {
112                         str = i->first;
113                         pthread_mutex_unlock (&thread_map_lock);
114                         return str;
115                 }
116         }
117         pthread_mutex_unlock (&thread_map_lock);
118         return "unknown";
119 }
120
121 void
122 pthread_kill_all (int signum) 
123 {       
124         pthread_mutex_lock (&thread_map_lock);
125         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
126                 if (i->second != pthread_self()) {
127                         pthread_kill (i->second, signum);
128                 }
129         }
130         all_threads.clear();
131         pthread_mutex_unlock (&thread_map_lock);
132 }
133
134 void
135 pthread_cancel_all () 
136 {       
137         pthread_mutex_lock (&thread_map_lock);
138         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
139                 if (i->second != pthread_self()) {
140                         pthread_cancel (i->second);
141                 }
142         }
143         all_threads.clear();
144         pthread_mutex_unlock (&thread_map_lock);
145 }
146
147 void
148 pthread_cancel_one (pthread_t thread) 
149 {       
150         pthread_mutex_lock (&thread_map_lock);
151         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
152                 if (i->second == thread) {
153                         all_threads.erase (i);
154                         break;
155                 }
156         }
157
158         pthread_cancel (thread);
159         pthread_mutex_unlock (&thread_map_lock);
160 }
161
162 void
163 pthread_exit_pbd (void* status) 
164 {       
165         pthread_t thread = pthread_self();
166
167         pthread_mutex_lock (&thread_map_lock);
168         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
169                 if (i->second == thread) {
170                         all_threads.erase (i);
171                         break;
172                 }
173         }
174         pthread_mutex_unlock (&thread_map_lock);
175         pthread_exit (status);
176 }