ca636034468d68021cb6b70d96d0c82be120391f
[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
37 namespace PBD {
38         PBD::Signal4<void,std::string, pthread_t,std::string,uint32_t> ThreadCreatedWithRequestSize;
39 }
40
41 using namespace PBD;
42
43 static int thread_creator (pthread_t* thread_id, const pthread_attr_t* attr, void *(*function)(void*), void* arg)
44 {
45 #ifdef WINE_THREAD_SUPPORT
46        return wine_pthread_create (thread_id, attr, function, arg);
47 #else
48        return pthread_create (thread_id, attr, function, arg);
49 #endif
50 }
51
52 void
53 PBD::notify_gui_about_thread_creation (std::string target_gui, pthread_t thread, std::string str, int request_count)
54 {
55         ThreadCreatedWithRequestSize (target_gui, thread, str, request_count);
56 }
57
58 int  
59 pthread_create_and_store (string name, pthread_t  *thread, void * (*start_routine)(void *), void * arg)
60 {
61         pthread_attr_t default_attr;
62         int ret;
63         
64         // set default stack size to sensible default for memlocking
65         pthread_attr_init(&default_attr);
66         pthread_attr_setstacksize(&default_attr, 500000);
67
68         if ((ret = thread_creator (thread, &default_attr, start_routine, arg)) == 0) {
69                 std::pair<string,pthread_t> newpair;
70                 newpair.first = name;
71                 newpair.second = *thread;
72
73                 pthread_mutex_lock (&thread_map_lock);
74                 all_threads.insert (newpair);
75                 pthread_mutex_unlock (&thread_map_lock);
76         }
77
78         pthread_attr_destroy(&default_attr);
79
80         return ret;
81 }
82
83 string
84 pthread_name ()
85 {
86         pthread_t self = pthread_self();
87         string str;
88
89         pthread_mutex_lock (&thread_map_lock);
90         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
91                 if (i->second == self) {
92                         str = i->first;
93                         pthread_mutex_unlock (&thread_map_lock);
94                         return str;
95                 }
96         }
97         pthread_mutex_unlock (&thread_map_lock);
98         return "unknown";
99 }
100
101 void
102 pthread_kill_all (int signum) 
103 {       
104         pthread_mutex_lock (&thread_map_lock);
105         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
106                 if (i->second != pthread_self()) {
107                         pthread_kill (i->second, signum);
108                 }
109         }
110         all_threads.clear();
111         pthread_mutex_unlock (&thread_map_lock);
112 }
113
114 void
115 pthread_cancel_all () 
116 {       
117         pthread_mutex_lock (&thread_map_lock);
118         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
119                 if (i->second != pthread_self()) {
120                         pthread_cancel (i->second);
121                 }
122         }
123         all_threads.clear();
124         pthread_mutex_unlock (&thread_map_lock);
125 }
126
127 void
128 pthread_cancel_one (pthread_t thread) 
129 {       
130         pthread_mutex_lock (&thread_map_lock);
131         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
132                 if (i->second == thread) {
133                         all_threads.erase (i);
134                         break;
135                 }
136         }
137
138         pthread_cancel (thread);
139         pthread_mutex_unlock (&thread_map_lock);
140 }
141
142 void
143 pthread_exit_pbd (void* status) 
144 {       
145         pthread_t thread = pthread_self();
146
147         pthread_mutex_lock (&thread_map_lock);
148         for (ThreadMap::iterator i = all_threads.begin(); i != all_threads.end(); ++i) {
149                 if (i->second == thread) {
150                         all_threads.erase (i);
151                         break;
152                 }
153         }
154         pthread_mutex_unlock (&thread_map_lock);
155         pthread_exit (status);
156 }