1 /* $Id: ports.c,v 1.15 2005/10/15 18:05:03 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
16 #include <kernel/OS.h>
19 #if defined(SYS_DARWIN) || defined(SYS_FREEBSD)
20 #include <sys/types.h>
21 #include <sys/sysctl.h>
25 #include <sys/param.h>
26 #include <sys/sysctl.h>
27 #include <machine/cpu.h>
40 #include <sys/processor.h>
51 #include <sys/types.h>
52 #include <sys/socket.h>
54 #include <netinet/in.h>
61 /************************************************************************
63 ************************************************************************
64 * Returns the current date in milliseconds.
65 * On Win32, we implement a gettimeofday emulation here because
66 * libdvdread and libmp4v2 use it without checking.
67 ************************************************************************/
74 int gettimeofday( struct timeval * tv, struct timezone * tz )
77 tick = GetTickCount();
78 tv->tv_sec = tick / 1000;
79 tv->tv_usec = ( tick % 1000 ) * 1000;
85 uint64_t hb_get_date()
88 gettimeofday( &tv, NULL );
89 return( (uint64_t) tv.tv_sec * 1000 + (uint64_t) tv.tv_usec / 1000 );
92 /************************************************************************
94 ************************************************************************
95 * Waits <delay> milliseconds.
96 ************************************************************************/
97 void hb_snooze( int delay )
103 #if defined( SYS_BEOS )
104 snooze( 1000 * delay );
105 #elif defined( SYS_DARWIN ) || defined( SYS_LINUX ) || defined( SYS_FREEBSD) || defined( SYS_SunOS )
106 usleep( 1000 * delay );
107 #elif defined( SYS_CYGWIN ) || defined( SYS_MINGW )
112 /************************************************************************
114 ************************************************************************
115 * Whenever possible, returns the number of CPUs on the current
116 * computer. Returns 1 otherwise.
117 * The detection is actually only performed on the first call.
118 ************************************************************************/
119 int hb_get_cpu_count()
121 static int cpu_count = 0;
129 #if defined(SYS_CYGWIN) || defined(SYS_MINGW)
131 GetSystemInfo( &cpuinfo );
132 cpu_count = cpuinfo.dwNumberOfProcessors;
134 #elif defined(SYS_LINUX)
137 memset( &p_aff, 0, sizeof(p_aff) );
138 sched_getaffinity( 0, sizeof(p_aff), &p_aff );
139 for( cpu_count = 0, bit = 0; bit < sizeof(p_aff); bit++ )
140 cpu_count += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
142 #elif defined(SYS_BEOS)
144 get_system_info( &info );
145 cpu_count = info.cpu_count;
147 #elif defined(SYS_DARWIN) || defined(SYS_FREEBSD) || defined(SYS_OPENBSD)
148 size_t length = sizeof( cpu_count );
150 int mib[2] = { CTL_HW, HW_NCPU };
151 if( sysctl(mib, 2, &cpu_count, &length, NULL, 0) )
153 if( sysctlbyname("hw.ncpu", &cpu_count, &length, NULL, 0) )
159 #elif defined( SYS_SunOS )
161 processorid_t cpumax;
164 cpumax = sysconf(_SC_CPUID_MAX);
166 for(i = 0; i <= cpumax; i++ )
168 if(p_online(i, P_STATUS) != -1)
177 cpu_count = MAX( 1, cpu_count );
178 cpu_count = MIN( cpu_count, 8 );
183 /************************************************************************
184 * Get a tempory directory for HB
185 ***********************************************************************/
186 void hb_get_tempory_directory( hb_handle_t * h, char path[512] )
190 /* Create the base */
191 #if defined( SYS_CYGWIN ) || defined( SYS_MINGW )
193 int i_size = GetTempPath( 512, base );
194 if( i_size <= 0 || i_size >= 512 )
196 if( getcwd( base, 512 ) == NULL )
197 strcpy( base, "c:" ); /* Bad fallback but ... */
200 /* c:/path/ works like a charm under cygwin(win32?) so use it */
201 while( ( p = strchr( base, '\\' ) ) )
204 strcpy( base, "/tmp" );
206 /* I prefer to remove evntual last '/' (for cygwin) */
207 if( base[strlen(base)-1] == '/' )
208 base[strlen(base)-1] = '\0';
210 snprintf( path, 512, "%s/hb.%d", base, hb_get_pid( h ) );
213 /************************************************************************
214 * Get a tempory filename for HB
215 ***********************************************************************/
216 void hb_get_tempory_filename( hb_handle_t * h, char name[1024],
221 hb_get_tempory_directory( h, name );
224 va_start( args, fmt );
225 vsnprintf( &name[strlen(name)], 1024 - strlen(name), fmt, args );
229 /************************************************************************
231 ************************************************************************
232 * Wrapper to the real mkdir, needed only because it doesn't take a
233 * second argument on Win32. Grrr.
234 ***********************************************************************/
235 void hb_mkdir( char * name )
244 /************************************************************************
245 * Portable thread implementation
246 ***********************************************************************/
251 void (* function) ( void * );
257 #if defined( SYS_BEOS )
261 //#elif defined( SYS_CYGWIN )
266 /* Get a unique identifier to thread and represent as 64-bit unsigned.
267 * If unsupported, the value 0 is be returned.
268 * Caller should use result only for display/log purposes.
270 static uint64_t hb_thread_to_integer( const hb_thread_t* t )
272 #if defined( USE_PTHREAD )
273 #if defined( _WIN32 ) || defined( __MINGW32__ )
274 return (uint64_t)(ptrdiff_t)t->thread.p;
275 #elif defined( SYS_DARWIN )
276 return (uint64_t)(ptrdiff_t)t->thread;
278 return (uint64_t)t->thread;
285 /************************************************************************
287 ************************************************************************
288 * We use it as the root routine for any thread, for two reasons:
289 * + To set the thread priority on OS X (pthread_setschedparam() could
290 * be called from hb_thread_init(), but it's nicer to do it as we
291 * are sure it is done before the real routine starts)
292 * + Get informed when the thread exits, so we know whether
293 * hb_thread_close() will block or not.
294 ***********************************************************************/
295 static void hb_thread_func( void * _t )
297 hb_thread_t * t = (hb_thread_t *) _t;
299 #if defined( SYS_DARWIN )
300 /* Set the thread priority */
301 struct sched_param param;
302 memset( ¶m, 0, sizeof( struct sched_param ) );
303 param.sched_priority = t->priority;
304 pthread_setschedparam( pthread_self(), SCHED_OTHER, ¶m );
307 #if defined( SYS_BEOS )
308 signal( SIGINT, SIG_IGN );
311 /* Start the actual routine */
312 t->function( t->arg );
314 /* Inform that the thread can be joined now */
315 hb_deep_log( 2, "thread %"PRIx64" exited (\"%s\")", hb_thread_to_integer( t ), t->name );
318 hb_unlock( t->lock );
321 /************************************************************************
323 ************************************************************************
324 * name: user-friendly name
325 * function: the thread routine
326 * arg: argument of the routine
327 * priority: HB_LOW_PRIORITY or HB_NORMAL_PRIORITY
328 ***********************************************************************/
329 hb_thread_t * hb_thread_init( char * name, void (* function)(void *),
330 void * arg, int priority )
332 hb_thread_t * t = calloc( sizeof( hb_thread_t ), 1 );
334 t->name = strdup( name );
335 t->function = function;
337 t->priority = priority;
339 t->lock = hb_lock_init();
341 /* Create and start the thread */
342 #if defined( SYS_BEOS )
343 t->thread = spawn_thread( (thread_func) hb_thread_func,
345 resume_thread( t->thread );
348 pthread_create( &t->thread, NULL,
349 (void * (*)( void * )) hb_thread_func, t );
351 //#elif defined( SYS_CYGWIN )
352 // t->thread = CreateThread( NULL, 0,
353 // (LPTHREAD_START_ROUTINE) hb_thread_func, t, 0, NULL );
355 // /* Maybe use THREAD_PRIORITY_LOWEST instead */
356 // if( priority == HB_LOW_PRIORITY )
357 // SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL );
360 hb_deep_log( 2, "thread %"PRIx64" started (\"%s\")", hb_thread_to_integer( t ), t->name );
364 /************************************************************************
366 ************************************************************************
367 * Joins the thread and frees memory.
368 ***********************************************************************/
369 void hb_thread_close( hb_thread_t ** _t )
371 hb_thread_t * t = *_t;
373 /* Join the thread */
374 #if defined( SYS_BEOS )
376 wait_for_thread( t->thread, &exit_value );
379 pthread_join( t->thread, NULL );
381 //#elif defined( SYS_CYGWIN )
382 // WaitForSingleObject( t->thread, INFINITE );
385 hb_deep_log( 2, "thread %"PRIx64" joined (\"%s\")", hb_thread_to_integer( t ), t->name );
387 hb_lock_close( &t->lock );
393 /************************************************************************
394 * hb_thread_has_exited()
395 ************************************************************************
396 * Returns 1 if the thread can be joined right away, 0 otherwise.
397 ***********************************************************************/
398 int hb_thread_has_exited( hb_thread_t * t )
404 hb_unlock( t->lock );
409 /************************************************************************
410 * Portable mutex implementation
411 ***********************************************************************/
414 #if defined( SYS_BEOS )
417 pthread_mutex_t mutex;
418 //#elif defined( SYS_CYGWIN )
423 /************************************************************************
428 ************************************************************************
429 * Basic wrappers to OS-specific semaphore or mutex functions.
430 ***********************************************************************/
431 hb_lock_t * hb_lock_init()
433 hb_lock_t * l = calloc( sizeof( hb_lock_t ), 1 );
435 #if defined( SYS_BEOS )
436 l->sem = create_sem( 1, "sem" );
438 pthread_mutexattr_t mta;
440 pthread_mutexattr_init(&mta);
442 #if defined( SYS_CYGWIN )
443 pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_NORMAL);
446 pthread_mutex_init( &l->mutex, &mta );
447 //#elif defined( SYS_CYGWIN )
448 // l->mutex = CreateMutex( 0, FALSE, 0 );
454 void hb_lock_close( hb_lock_t ** _l )
458 #if defined( SYS_BEOS )
459 delete_sem( l->sem );
461 pthread_mutex_destroy( &l->mutex );
462 //#elif defined( SYS_CYGWIN )
463 // CloseHandle( l->mutex );
470 void hb_lock( hb_lock_t * l )
472 #if defined( SYS_BEOS )
473 acquire_sem( l->sem );
475 pthread_mutex_lock( &l->mutex );
476 //#elif defined( SYS_CYGWIN )
477 // WaitForSingleObject( l->mutex, INFINITE );
481 void hb_unlock( hb_lock_t * l )
483 #if defined( SYS_BEOS )
484 release_sem( l->sem );
486 pthread_mutex_unlock( &l->mutex );
487 //#elif defined( SYS_CYGWIN )
488 // ReleaseMutex( l->mutex );
492 /************************************************************************
493 * Portable condition variable implementation
494 ***********************************************************************/
497 #if defined( SYS_BEOS )
501 //#elif defined( SYS_CYGWIN )
506 /************************************************************************
511 ************************************************************************
512 * Win9x is not supported by this implementation (SignalObjectAndWait()
513 * only available on Windows 2000/XP).
514 ***********************************************************************/
515 hb_cond_t * hb_cond_init()
517 hb_cond_t * c = calloc( sizeof( hb_cond_t ), 1 );
519 #if defined( SYS_BEOS )
522 pthread_cond_init( &c->cond, NULL );
523 //#elif defined( SYS_CYGWIN )
524 // c->event = CreateEvent( NULL, FALSE, FALSE, NULL );
530 void hb_cond_close( hb_cond_t ** _c )
534 #if defined( SYS_BEOS )
536 pthread_cond_destroy( &c->cond );
537 //#elif defined( SYS_CYGWIN )
538 // CloseHandle( c->event );
545 void hb_cond_wait( hb_cond_t * c, hb_lock_t * lock )
547 #if defined( SYS_BEOS )
548 c->thread = find_thread( NULL );
549 release_sem( lock->sem );
550 suspend_thread( c->thread );
551 acquire_sem( lock->sem );
554 pthread_cond_wait( &c->cond, &lock->mutex );
555 //#elif defined( SYS_CYGWIN )
556 // SignalObjectAndWait( lock->mutex, c->event, INFINITE, FALSE );
557 // WaitForSingleObject( lock->mutex, INFINITE );
561 void hb_cond_signal( hb_cond_t * c )
563 #if defined( SYS_BEOS )
564 while( c->thread != -1 )
567 get_thread_info( c->thread, &info );
568 if( info.state == B_THREAD_SUSPENDED )
570 resume_thread( c->thread );
573 /* Looks like we have been called between hb_cond_wait's
574 release_sem() and suspend_thread() lines. Wait until the
575 thread is actually suspended before we resume it */
579 pthread_cond_signal( &c->cond );
580 //#elif defined( SYS_CYGWIN )
581 // PulseEvent( c->event );
585 /************************************************************************
587 ***********************************************************************/
594 hb_net_t * hb_net_open( char * address, int port )
596 hb_net_t * n = calloc( sizeof( hb_net_t ), 1 );
598 struct sockaddr_in sock;
599 struct hostent * host;
601 /* TODO: find out why this doesn't work on Win32 */
602 if( !( host = gethostbyname( address ) ) )
604 hb_log( "gethostbyname failed (%s)", address );
609 memset( &sock, 0, sizeof( struct sockaddr_in ) );
610 sock.sin_family = host->h_addrtype;
611 sock.sin_port = htons( port );
612 memcpy( &sock.sin_addr, host->h_addr, host->h_length );
614 if( ( n->socket = socket( host->h_addrtype, SOCK_STREAM, 0 ) ) < 0 )
616 hb_log( "socket failed" );
621 if( connect( n->socket, (struct sockaddr *) &sock,
622 sizeof( struct sockaddr_in ) ) < 0 )
624 hb_log( "connect failed" );
632 int hb_net_send( hb_net_t * n, char * buffer )
634 return send( n->socket, buffer, strlen( buffer ), 0 );
637 int hb_net_recv( hb_net_t * n, char * buffer, int size )
639 return recv( n->socket, buffer, size - 1, 0 );
642 void hb_net_close( hb_net_t ** _n )
644 hb_net_t * n = (hb_net_t *) *_n;