~ubuntu-branches/ubuntu/utopic/ardour3/utopic

« back to all changes in this revision

Viewing changes to libs/pbd/pthread_utils.cc

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2013-09-21 19:05:02 UTC
  • Revision ID: package-import@ubuntu.com-20130921190502-8gsftrku6jnzhd7v
Tags: upstream-3.4~dfsg
ImportĀ upstreamĀ versionĀ 3.4~dfsg

Show diffs side-by-side

added added

removed removed

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