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