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>
57 #if defined( SYS_LINUX )
58 #include <linux/cdrom.h>
60 #include <sys/ioctl.h>
61 #elif defined( SYS_OPENBSD )
62 #include <sys/dvdio.h>
64 #include <sys/ioctl.h>
71 /************************************************************************
73 ************************************************************************
74 * Returns the current date in milliseconds.
75 * On Win32, we implement a gettimeofday emulation here because
76 * libdvdread and libmp4v2 use it without checking.
77 ************************************************************************/
84 int gettimeofday( struct timeval * tv, struct timezone * tz )
87 tick = GetTickCount();
88 tv->tv_sec = tick / 1000;
89 tv->tv_usec = ( tick % 1000 ) * 1000;
95 int hb_dvd_region(char *device, int *region_mask)
97 #if defined( DVD_LU_SEND_RPC_STATE ) && defined( DVD_AUTH )
102 fd = open( device, O_RDONLY );
105 if ( fstat( fd, &st ) < 0 )
107 if ( !( S_ISBLK( st.st_mode ) || S_ISCHR( st.st_mode ) ) )
110 ai.type = DVD_LU_SEND_RPC_STATE;
111 ret = ioctl(fd, DVD_AUTH, &ai);
115 *region_mask = ai.lrpcs.region_mask;
122 uint64_t hb_get_date()
125 gettimeofday( &tv, NULL );
126 return( (uint64_t) tv.tv_sec * 1000 + (uint64_t) tv.tv_usec / 1000 );
129 /************************************************************************
131 ************************************************************************
132 * Waits <delay> milliseconds.
133 ************************************************************************/
134 void hb_snooze( int delay )
140 #if defined( SYS_BEOS )
141 snooze( 1000 * delay );
142 #elif defined( SYS_DARWIN ) || defined( SYS_LINUX ) || defined( SYS_FREEBSD) || defined( SYS_SunOS )
143 usleep( 1000 * delay );
144 #elif defined( SYS_CYGWIN ) || defined( SYS_MINGW )
149 /************************************************************************
151 ************************************************************************
152 * Whenever possible, returns the number of CPUs on the current
153 * computer. Returns 1 otherwise.
154 * The detection is actually only performed on the first call.
155 ************************************************************************/
156 int hb_get_cpu_count()
158 static int cpu_count = 0;
166 #if defined(SYS_CYGWIN) || defined(SYS_MINGW)
168 GetSystemInfo( &cpuinfo );
169 cpu_count = cpuinfo.dwNumberOfProcessors;
171 #elif defined(SYS_LINUX)
174 memset( &p_aff, 0, sizeof(p_aff) );
175 sched_getaffinity( 0, sizeof(p_aff), &p_aff );
176 for( cpu_count = 0, bit = 0; bit < sizeof(p_aff); bit++ )
177 cpu_count += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
179 #elif defined(SYS_BEOS)
181 get_system_info( &info );
182 cpu_count = info.cpu_count;
184 #elif defined(SYS_DARWIN) || defined(SYS_FREEBSD) || defined(SYS_OPENBSD)
185 size_t length = sizeof( cpu_count );
187 int mib[2] = { CTL_HW, HW_NCPU };
188 if( sysctl(mib, 2, &cpu_count, &length, NULL, 0) )
190 if( sysctlbyname("hw.ncpu", &cpu_count, &length, NULL, 0) )
196 #elif defined( SYS_SunOS )
198 processorid_t cpumax;
201 cpumax = sysconf(_SC_CPUID_MAX);
203 for(i = 0; i <= cpumax; i++ )
205 if(p_online(i, P_STATUS) != -1)
214 cpu_count = MAX( 1, cpu_count );
215 cpu_count = MIN( cpu_count, 8 );
220 /************************************************************************
221 * Get a tempory directory for HB
222 ***********************************************************************/
223 void hb_get_tempory_directory( hb_handle_t * h, char path[512] )
227 /* Create the base */
228 #if defined( SYS_CYGWIN ) || defined( SYS_MINGW )
230 int i_size = GetTempPath( 512, base );
231 if( i_size <= 0 || i_size >= 512 )
233 if( getcwd( base, 512 ) == NULL )
234 strcpy( base, "c:" ); /* Bad fallback but ... */
237 /* c:/path/ works like a charm under cygwin(win32?) so use it */
238 while( ( p = strchr( base, '\\' ) ) )
241 strcpy( base, "/tmp" );
243 /* I prefer to remove evntual last '/' (for cygwin) */
244 if( base[strlen(base)-1] == '/' )
245 base[strlen(base)-1] = '\0';
247 snprintf( path, 512, "%s/hb.%d", base, hb_get_pid( h ) );
250 /************************************************************************
251 * Get a tempory filename for HB
252 ***********************************************************************/
253 void hb_get_tempory_filename( hb_handle_t * h, char name[1024],
258 hb_get_tempory_directory( h, name );
261 va_start( args, fmt );
262 vsnprintf( &name[strlen(name)], 1024 - strlen(name), fmt, args );
266 /************************************************************************
268 ************************************************************************
269 * Wrapper to the real mkdir, needed only because it doesn't take a
270 * second argument on Win32. Grrr.
271 ***********************************************************************/
272 void hb_mkdir( char * name )
281 /************************************************************************
282 * Portable thread implementation
283 ***********************************************************************/
288 void (* function) ( void * );
294 #if defined( SYS_BEOS )
298 //#elif defined( SYS_CYGWIN )
303 /* Get a unique identifier to thread and represent as 64-bit unsigned.
304 * If unsupported, the value 0 is be returned.
305 * Caller should use result only for display/log purposes.
307 static uint64_t hb_thread_to_integer( const hb_thread_t* t )
309 #if defined( USE_PTHREAD )
310 #if defined( SYS_CYGWIN )
311 return (uint64_t)t->thread;
312 #elif defined( _WIN32 ) || defined( __MINGW32__ )
313 return (uint64_t)(ptrdiff_t)t->thread.p;
314 #elif defined( SYS_DARWIN )
315 return (unsigned long)t->thread;
317 return (uint64_t)t->thread;
324 /************************************************************************
326 ************************************************************************
327 * We use it as the root routine for any thread, for two reasons:
328 * + To set the thread priority on OS X (pthread_setschedparam() could
329 * be called from hb_thread_init(), but it's nicer to do it as we
330 * are sure it is done before the real routine starts)
331 * + Get informed when the thread exits, so we know whether
332 * hb_thread_close() will block or not.
333 ***********************************************************************/
334 static void hb_thread_func( void * _t )
336 hb_thread_t * t = (hb_thread_t *) _t;
338 #if defined( SYS_DARWIN )
339 /* Set the thread priority */
340 struct sched_param param;
341 memset( ¶m, 0, sizeof( struct sched_param ) );
342 param.sched_priority = t->priority;
343 pthread_setschedparam( pthread_self(), SCHED_OTHER, ¶m );
346 #if defined( SYS_BEOS )
347 signal( SIGINT, SIG_IGN );
350 /* Start the actual routine */
351 t->function( t->arg );
353 /* Inform that the thread can be joined now */
354 hb_deep_log( 2, "thread %"PRIx64" exited (\"%s\")", hb_thread_to_integer( t ), t->name );
357 hb_unlock( t->lock );
360 /************************************************************************
362 ************************************************************************
363 * name: user-friendly name
364 * function: the thread routine
365 * arg: argument of the routine
366 * priority: HB_LOW_PRIORITY or HB_NORMAL_PRIORITY
367 ***********************************************************************/
368 hb_thread_t * hb_thread_init( char * name, void (* function)(void *),
369 void * arg, int priority )
371 hb_thread_t * t = calloc( sizeof( hb_thread_t ), 1 );
373 t->name = strdup( name );
374 t->function = function;
376 t->priority = priority;
378 t->lock = hb_lock_init();
380 /* Create and start the thread */
381 #if defined( SYS_BEOS )
382 t->thread = spawn_thread( (thread_func) hb_thread_func,
384 resume_thread( t->thread );
387 pthread_create( &t->thread, NULL,
388 (void * (*)( void * )) hb_thread_func, t );
390 //#elif defined( SYS_CYGWIN )
391 // t->thread = CreateThread( NULL, 0,
392 // (LPTHREAD_START_ROUTINE) hb_thread_func, t, 0, NULL );
394 // /* Maybe use THREAD_PRIORITY_LOWEST instead */
395 // if( priority == HB_LOW_PRIORITY )
396 // SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL );
399 hb_deep_log( 2, "thread %"PRIx64" started (\"%s\")", hb_thread_to_integer( t ), t->name );
403 /************************************************************************
405 ************************************************************************
406 * Joins the thread and frees memory.
407 ***********************************************************************/
408 void hb_thread_close( hb_thread_t ** _t )
410 hb_thread_t * t = *_t;
412 /* Join the thread */
413 #if defined( SYS_BEOS )
415 wait_for_thread( t->thread, &exit_value );
418 pthread_join( t->thread, NULL );
420 //#elif defined( SYS_CYGWIN )
421 // WaitForSingleObject( t->thread, INFINITE );
424 hb_deep_log( 2, "thread %"PRIx64" joined (\"%s\")", hb_thread_to_integer( t ), t->name );
426 hb_lock_close( &t->lock );
432 /************************************************************************
433 * hb_thread_has_exited()
434 ************************************************************************
435 * Returns 1 if the thread can be joined right away, 0 otherwise.
436 ***********************************************************************/
437 int hb_thread_has_exited( hb_thread_t * t )
443 hb_unlock( t->lock );
448 /************************************************************************
449 * Portable mutex implementation
450 ***********************************************************************/
453 #if defined( SYS_BEOS )
456 pthread_mutex_t mutex;
457 //#elif defined( SYS_CYGWIN )
462 /************************************************************************
467 ************************************************************************
468 * Basic wrappers to OS-specific semaphore or mutex functions.
469 ***********************************************************************/
470 hb_lock_t * hb_lock_init()
472 hb_lock_t * l = calloc( sizeof( hb_lock_t ), 1 );
474 #if defined( SYS_BEOS )
475 l->sem = create_sem( 1, "sem" );
477 pthread_mutexattr_t mta;
479 pthread_mutexattr_init(&mta);
481 #if defined( SYS_CYGWIN )
482 pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_NORMAL);
485 pthread_mutex_init( &l->mutex, &mta );
486 //#elif defined( SYS_CYGWIN )
487 // l->mutex = CreateMutex( 0, FALSE, 0 );
493 void hb_lock_close( hb_lock_t ** _l )
497 #if defined( SYS_BEOS )
498 delete_sem( l->sem );
500 pthread_mutex_destroy( &l->mutex );
501 //#elif defined( SYS_CYGWIN )
502 // CloseHandle( l->mutex );
509 void hb_lock( hb_lock_t * l )
511 #if defined( SYS_BEOS )
512 acquire_sem( l->sem );
514 pthread_mutex_lock( &l->mutex );
515 //#elif defined( SYS_CYGWIN )
516 // WaitForSingleObject( l->mutex, INFINITE );
520 void hb_unlock( hb_lock_t * l )
522 #if defined( SYS_BEOS )
523 release_sem( l->sem );
525 pthread_mutex_unlock( &l->mutex );
526 //#elif defined( SYS_CYGWIN )
527 // ReleaseMutex( l->mutex );
531 /************************************************************************
532 * Portable condition variable implementation
533 ***********************************************************************/
536 #if defined( SYS_BEOS )
540 //#elif defined( SYS_CYGWIN )
545 /************************************************************************
550 ************************************************************************
551 * Win9x is not supported by this implementation (SignalObjectAndWait()
552 * only available on Windows 2000/XP).
553 ***********************************************************************/
554 hb_cond_t * hb_cond_init()
556 hb_cond_t * c = calloc( sizeof( hb_cond_t ), 1 );
558 #if defined( SYS_BEOS )
561 pthread_cond_init( &c->cond, NULL );
562 //#elif defined( SYS_CYGWIN )
563 // c->event = CreateEvent( NULL, FALSE, FALSE, NULL );
569 void hb_cond_close( hb_cond_t ** _c )
573 #if defined( SYS_BEOS )
575 pthread_cond_destroy( &c->cond );
576 //#elif defined( SYS_CYGWIN )
577 // CloseHandle( c->event );
584 void hb_cond_wait( hb_cond_t * c, hb_lock_t * lock )
586 #if defined( SYS_BEOS )
587 c->thread = find_thread( NULL );
588 release_sem( lock->sem );
589 suspend_thread( c->thread );
590 acquire_sem( lock->sem );
593 pthread_cond_wait( &c->cond, &lock->mutex );
594 //#elif defined( SYS_CYGWIN )
595 // SignalObjectAndWait( lock->mutex, c->event, INFINITE, FALSE );
596 // WaitForSingleObject( lock->mutex, INFINITE );
600 void hb_cond_signal( hb_cond_t * c )
602 #if defined( SYS_BEOS )
603 while( c->thread != -1 )
606 get_thread_info( c->thread, &info );
607 if( info.state == B_THREAD_SUSPENDED )
609 resume_thread( c->thread );
612 /* Looks like we have been called between hb_cond_wait's
613 release_sem() and suspend_thread() lines. Wait until the
614 thread is actually suspended before we resume it */
618 pthread_cond_signal( &c->cond );
619 //#elif defined( SYS_CYGWIN )
620 // PulseEvent( c->event );
624 /************************************************************************
626 ***********************************************************************/
633 hb_net_t * hb_net_open( char * address, int port )
635 hb_net_t * n = calloc( sizeof( hb_net_t ), 1 );
637 struct sockaddr_in sock;
638 struct hostent * host;
640 /* TODO: find out why this doesn't work on Win32 */
641 if( !( host = gethostbyname( address ) ) )
643 hb_log( "gethostbyname failed (%s)", address );
648 memset( &sock, 0, sizeof( struct sockaddr_in ) );
649 sock.sin_family = host->h_addrtype;
650 sock.sin_port = htons( port );
651 memcpy( &sock.sin_addr, host->h_addr, host->h_length );
653 if( ( n->socket = socket( host->h_addrtype, SOCK_STREAM, 0 ) ) < 0 )
655 hb_log( "socket failed" );
660 if( connect( n->socket, (struct sockaddr *) &sock,
661 sizeof( struct sockaddr_in ) ) < 0 )
663 hb_log( "connect failed" );
671 int hb_net_send( hb_net_t * n, char * buffer )
673 return send( n->socket, buffer, strlen( buffer ), 0 );
676 int hb_net_recv( hb_net_t * n, char * buffer, int size )
678 return recv( n->socket, buffer, size - 1, 0 );
681 void hb_net_close( hb_net_t ** _n )
683 hb_net_t * n = (hb_net_t *) *_n;