]> git.sesse.net Git - vlc/blob - src/misc/threads.c
Merge branch 1.0-bugfix
[vlc] / src / misc / threads.c
1 /*****************************************************************************
2  * threads.c : threads implementation for the VideoLAN client
3  *****************************************************************************
4  * Copyright (C) 1999-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *          Clément Sténac
11  *          Rémi Denis-Courmont
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33
34 #include "libvlc.h"
35 #include <assert.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39
40 #if defined( LIBVLC_USE_PTHREAD )
41 # include <sched.h>
42 #endif
43
44
45 struct vlc_thread_boot
46 {
47     void * (*entry) (vlc_object_t *);
48     vlc_object_t *object;
49 };
50
51 static void *thread_entry (void *data)
52 {
53     vlc_object_t *obj = ((struct vlc_thread_boot *)data)->object;
54     void *(*func) (vlc_object_t *) = ((struct vlc_thread_boot *)data)->entry;
55
56     free (data);
57     msg_Dbg (obj, "thread started");
58     func (obj);
59     msg_Dbg (obj, "thread ended");
60
61     return NULL;
62 }
63
64 #undef vlc_thread_create
65 /*****************************************************************************
66  * vlc_thread_create: create a thread
67  *****************************************************************************
68  * Note that i_priority is only taken into account on platforms supporting
69  * userland real-time priority threads.
70  *****************************************************************************/
71 int vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line,
72                        const char *psz_name, void *(*func) ( vlc_object_t * ),
73                        int i_priority )
74 {
75     int i_ret;
76     vlc_object_internals_t *p_priv = vlc_internals( p_this );
77
78     struct vlc_thread_boot *boot = malloc (sizeof (*boot));
79     if (boot == NULL)
80         return errno;
81     boot->entry = func;
82     boot->object = p_this;
83
84     /* Make sure we don't re-create a thread if the object has already one */
85     assert( !p_priv->b_thread );
86
87 #if defined( LIBVLC_USE_PTHREAD )
88 #ifndef __APPLE__
89     if( config_GetInt( p_this, "rt-priority" ) > 0 )
90 #endif
91     {
92         /* Hack to avoid error msg */
93         if( config_GetType( p_this, "rt-offset" ) )
94             i_priority += config_GetInt( p_this, "rt-offset" );
95     }
96 #endif
97
98     p_priv->b_thread = true;
99     i_ret = vlc_clone( &p_priv->thread_id, thread_entry, boot, i_priority );
100     if( i_ret == 0 )
101         msg_Dbg( p_this, "thread (%s) created at priority %d (%s:%d)",
102                  psz_name, i_priority, psz_file, i_line );
103     else
104     {
105         p_priv->b_thread = false;
106         errno = i_ret;
107         msg_Err( p_this, "%s thread could not be created at %s:%d (%m)",
108                          psz_name, psz_file, i_line );
109     }
110
111     return i_ret;
112 }
113
114 /*****************************************************************************
115  * vlc_thread_set_priority: set the priority of the current thread when we
116  * couldn't set it in vlc_thread_create (for instance for the main thread)
117  *****************************************************************************/
118 int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
119                                int i_line, int i_priority )
120 {
121     vlc_object_internals_t *p_priv = vlc_internals( p_this );
122
123     if( !p_priv->b_thread )
124     {
125         msg_Err( p_this, "couldn't set priority of non-existent thread" );
126         return ESRCH;
127     }
128
129 #if defined( LIBVLC_USE_PTHREAD )
130 # ifndef __APPLE__
131     if( config_GetInt( p_this, "rt-priority" ) > 0 )
132 # endif
133     {
134         int i_error, i_policy;
135         struct sched_param param;
136
137         memset( &param, 0, sizeof(struct sched_param) );
138         if( config_GetType( p_this, "rt-offset" ) )
139             i_priority += config_GetInt( p_this, "rt-offset" );
140         if( i_priority <= 0 )
141         {
142             param.sched_priority = (-1) * i_priority;
143             i_policy = SCHED_OTHER;
144         }
145         else
146         {
147             param.sched_priority = i_priority;
148             i_policy = SCHED_RR;
149         }
150         if( (i_error = pthread_setschedparam( p_priv->thread_id,
151                                               i_policy, &param )) )
152         {
153             errno = i_error;
154             msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
155                       psz_file, i_line );
156             i_priority = 0;
157         }
158     }
159
160 #elif defined( WIN32 ) || defined( UNDER_CE )
161     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
162
163     if( !SetThreadPriority(p_priv->thread_id->handle, i_priority) )
164     {
165         msg_Warn( p_this, "couldn't set a faster priority" );
166         return 1;
167     }
168
169 #endif
170
171     return 0;
172 }
173
174 /*****************************************************************************
175  * vlc_thread_join: wait until a thread exits, inner version
176  *****************************************************************************/
177 void __vlc_thread_join( vlc_object_t *p_this )
178 {
179     vlc_object_internals_t *p_priv = vlc_internals( p_this );
180
181 #if defined( LIBVLC_USE_PTHREAD )
182     vlc_join (p_priv->thread_id, NULL);
183
184 #elif defined( UNDER_CE ) || defined( WIN32 )
185     HANDLE hThread;
186     FILETIME create_ft, exit_ft, kernel_ft, user_ft;
187     int64_t real_time, kernel_time, user_time;
188
189 #ifndef UNDER_CE
190     if( ! DuplicateHandle(GetCurrentProcess(),
191             p_priv->thread_id->handle,
192             GetCurrentProcess(),
193             &hThread,
194             0,
195             FALSE,
196             DUPLICATE_SAME_ACCESS) )
197     {
198         p_priv->b_thread = false;
199         return; /* We have a problem! */
200     }
201 #else
202     hThread = p_priv->thread_id->handle;
203 #endif
204
205     vlc_join( p_priv->thread_id, NULL );
206
207     if( GetThreadTimes( hThread, &create_ft, &exit_ft, &kernel_ft, &user_ft ) )
208     {
209         real_time =
210           ((((int64_t)exit_ft.dwHighDateTime)<<32)| exit_ft.dwLowDateTime) -
211           ((((int64_t)create_ft.dwHighDateTime)<<32)| create_ft.dwLowDateTime);
212         real_time /= 10;
213
214         kernel_time =
215           ((((int64_t)kernel_ft.dwHighDateTime)<<32)|
216            kernel_ft.dwLowDateTime) / 10;
217
218         user_time =
219           ((((int64_t)user_ft.dwHighDateTime)<<32)|
220            user_ft.dwLowDateTime) / 10;
221
222         msg_Dbg( p_this, "thread times: "
223                  "real %"PRId64"m%fs, kernel %"PRId64"m%fs, user %"PRId64"m%fs",
224                  real_time/60/1000000,
225                  (double)((real_time%(60*1000000))/1000000.0),
226                  kernel_time/60/1000000,
227                  (double)((kernel_time%(60*1000000))/1000000.0),
228                  user_time/60/1000000,
229                  (double)((user_time%(60*1000000))/1000000.0) );
230     }
231     CloseHandle( hThread );
232
233 #else
234     vlc_join( p_priv->thread_id, NULL );
235
236 #endif
237
238     p_priv->b_thread = false;
239 }
240
241 void vlc_thread_cancel (vlc_object_t *obj)
242 {
243     vlc_object_internals_t *priv = vlc_internals (obj);
244
245     if (priv->b_thread)
246         vlc_cancel (priv->thread_id);
247 }