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