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( SYS_CYGWIN )
274 return (uint64_t)t->thread;
275 #elif defined( _WIN32 ) || defined( __MINGW32__ )
276 return (uint64_t)(ptrdiff_t)t->thread.p;
277 #elif defined( SYS_DARWIN )
278 return (unsigned long)t->thread;
280 return (uint64_t)t->thread;
287 /************************************************************************
289 ************************************************************************
290 * We use it as the root routine for any thread, for two reasons:
291 * + To set the thread priority on OS X (pthread_setschedparam() could
292 * be called from hb_thread_init(), but it's nicer to do it as we
293 * are sure it is done before the real routine starts)
294 * + Get informed when the thread exits, so we know whether
295 * hb_thread_close() will block or not.
296 ***********************************************************************/
297 static void hb_thread_func( void * _t )
299 hb_thread_t * t = (hb_thread_t *) _t;
301 #if defined( SYS_DARWIN )
302 /* Set the thread priority */
303 struct sched_param param;
304 memset( ¶m, 0, sizeof( struct sched_param ) );
305 param.sched_priority = t->priority;
306 pthread_setschedparam( pthread_self(), SCHED_OTHER, ¶m );
309 #if defined( SYS_BEOS )
310 signal( SIGINT, SIG_IGN );
313 /* Start the actual routine */
314 t->function( t->arg );
316 /* Inform that the thread can be joined now */
317 hb_deep_log( 2, "thread %"PRIx64" exited (\"%s\")", hb_thread_to_integer( t ), t->name );
320 hb_unlock( t->lock );
323 /************************************************************************
325 ************************************************************************
326 * name: user-friendly name
327 * function: the thread routine
328 * arg: argument of the routine
329 * priority: HB_LOW_PRIORITY or HB_NORMAL_PRIORITY
330 ***********************************************************************/
331 hb_thread_t * hb_thread_init( char * name, void (* function)(void *),
332 void * arg, int priority )
334 hb_thread_t * t = calloc( sizeof( hb_thread_t ), 1 );
336 t->name = strdup( name );
337 t->function = function;
339 t->priority = priority;
341 t->lock = hb_lock_init();
343 /* Create and start the thread */
344 #if defined( SYS_BEOS )
345 t->thread = spawn_thread( (thread_func) hb_thread_func,
347 resume_thread( t->thread );
350 pthread_create( &t->thread, NULL,
351 (void * (*)( void * )) hb_thread_func, t );
353 //#elif defined( SYS_CYGWIN )
354 // t->thread = CreateThread( NULL, 0,
355 // (LPTHREAD_START_ROUTINE) hb_thread_func, t, 0, NULL );
357 // /* Maybe use THREAD_PRIORITY_LOWEST instead */
358 // if( priority == HB_LOW_PRIORITY )
359 // SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL );
362 hb_deep_log( 2, "thread %"PRIx64" started (\"%s\")", hb_thread_to_integer( t ), t->name );
366 /************************************************************************
368 ************************************************************************
369 * Joins the thread and frees memory.
370 ***********************************************************************/
371 void hb_thread_close( hb_thread_t ** _t )
373 hb_thread_t * t = *_t;
375 /* Join the thread */
376 #if defined( SYS_BEOS )
378 wait_for_thread( t->thread, &exit_value );
381 pthread_join( t->thread, NULL );
383 //#elif defined( SYS_CYGWIN )
384 // WaitForSingleObject( t->thread, INFINITE );
387 hb_deep_log( 2, "thread %"PRIx64" joined (\"%s\")", hb_thread_to_integer( t ), t->name );
389 hb_lock_close( &t->lock );
395 /************************************************************************
396 * hb_thread_has_exited()
397 ************************************************************************
398 * Returns 1 if the thread can be joined right away, 0 otherwise.
399 ***********************************************************************/
400 int hb_thread_has_exited( hb_thread_t * t )
406 hb_unlock( t->lock );
411 /************************************************************************
412 * Portable mutex implementation
413 ***********************************************************************/
416 #if defined( SYS_BEOS )
419 pthread_mutex_t mutex;
420 //#elif defined( SYS_CYGWIN )
425 /************************************************************************
430 ************************************************************************
431 * Basic wrappers to OS-specific semaphore or mutex functions.
432 ***********************************************************************/
433 hb_lock_t * hb_lock_init()
435 hb_lock_t * l = calloc( sizeof( hb_lock_t ), 1 );
437 #if defined( SYS_BEOS )
438 l->sem = create_sem( 1, "sem" );
440 pthread_mutexattr_t mta;
442 pthread_mutexattr_init(&mta);
444 #if defined( SYS_CYGWIN )
445 pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_NORMAL);
448 pthread_mutex_init( &l->mutex, &mta );
449 //#elif defined( SYS_CYGWIN )
450 // l->mutex = CreateMutex( 0, FALSE, 0 );
456 void hb_lock_close( hb_lock_t ** _l )
460 #if defined( SYS_BEOS )
461 delete_sem( l->sem );
463 pthread_mutex_destroy( &l->mutex );
464 //#elif defined( SYS_CYGWIN )
465 // CloseHandle( l->mutex );
472 void hb_lock( hb_lock_t * l )
474 #if defined( SYS_BEOS )
475 acquire_sem( l->sem );
477 pthread_mutex_lock( &l->mutex );
478 //#elif defined( SYS_CYGWIN )
479 // WaitForSingleObject( l->mutex, INFINITE );
483 void hb_unlock( hb_lock_t * l )
485 #if defined( SYS_BEOS )
486 release_sem( l->sem );
488 pthread_mutex_unlock( &l->mutex );
489 //#elif defined( SYS_CYGWIN )
490 // ReleaseMutex( l->mutex );
494 /************************************************************************
495 * Portable condition variable implementation
496 ***********************************************************************/
499 #if defined( SYS_BEOS )
503 //#elif defined( SYS_CYGWIN )
508 /************************************************************************
513 ************************************************************************
514 * Win9x is not supported by this implementation (SignalObjectAndWait()
515 * only available on Windows 2000/XP).
516 ***********************************************************************/
517 hb_cond_t * hb_cond_init()
519 hb_cond_t * c = calloc( sizeof( hb_cond_t ), 1 );
521 #if defined( SYS_BEOS )
524 pthread_cond_init( &c->cond, NULL );
525 //#elif defined( SYS_CYGWIN )
526 // c->event = CreateEvent( NULL, FALSE, FALSE, NULL );
532 void hb_cond_close( hb_cond_t ** _c )
536 #if defined( SYS_BEOS )
538 pthread_cond_destroy( &c->cond );
539 //#elif defined( SYS_CYGWIN )
540 // CloseHandle( c->event );
547 void hb_cond_wait( hb_cond_t * c, hb_lock_t * lock )
549 #if defined( SYS_BEOS )
550 c->thread = find_thread( NULL );
551 release_sem( lock->sem );
552 suspend_thread( c->thread );
553 acquire_sem( lock->sem );
556 pthread_cond_wait( &c->cond, &lock->mutex );
557 //#elif defined( SYS_CYGWIN )
558 // SignalObjectAndWait( lock->mutex, c->event, INFINITE, FALSE );
559 // WaitForSingleObject( lock->mutex, INFINITE );
563 void hb_cond_signal( hb_cond_t * c )
565 #if defined( SYS_BEOS )
566 while( c->thread != -1 )
569 get_thread_info( c->thread, &info );
570 if( info.state == B_THREAD_SUSPENDED )
572 resume_thread( c->thread );
575 /* Looks like we have been called between hb_cond_wait's
576 release_sem() and suspend_thread() lines. Wait until the
577 thread is actually suspended before we resume it */
581 pthread_cond_signal( &c->cond );
582 //#elif defined( SYS_CYGWIN )
583 // PulseEvent( c->event );
587 /************************************************************************
589 ***********************************************************************/
596 hb_net_t * hb_net_open( char * address, int port )
598 hb_net_t * n = calloc( sizeof( hb_net_t ), 1 );
600 struct sockaddr_in sock;
601 struct hostent * host;
603 /* TODO: find out why this doesn't work on Win32 */
604 if( !( host = gethostbyname( address ) ) )
606 hb_log( "gethostbyname failed (%s)", address );
611 memset( &sock, 0, sizeof( struct sockaddr_in ) );
612 sock.sin_family = host->h_addrtype;
613 sock.sin_port = htons( port );
614 memcpy( &sock.sin_addr, host->h_addr, host->h_length );
616 if( ( n->socket = socket( host->h_addrtype, SOCK_STREAM, 0 ) ) < 0 )
618 hb_log( "socket failed" );
623 if( connect( n->socket, (struct sockaddr *) &sock,
624 sizeof( struct sockaddr_in ) ) < 0 )
626 hb_log( "connect failed" );
634 int hb_net_send( hb_net_t * n, char * buffer )
636 return send( n->socket, buffer, strlen( buffer ), 0 );
639 int hb_net_recv( hb_net_t * n, char * buffer, int size )
641 return recv( n->socket, buffer, size - 1, 0 );
644 void hb_net_close( hb_net_t ** _n )
646 hb_net_t * n = (hb_net_t *) *_n;