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 )
110 if ( !( S_ISBLK( st.st_mode ) || S_ISCHR( st.st_mode ) ) )
116 ai.type = DVD_LU_SEND_RPC_STATE;
117 ret = ioctl(fd, DVD_AUTH, &ai);
122 *region_mask = ai.lrpcs.region_mask;
129 uint64_t hb_get_date()
132 gettimeofday( &tv, NULL );
133 return( (uint64_t) tv.tv_sec * 1000 + (uint64_t) tv.tv_usec / 1000 );
136 /************************************************************************
138 ************************************************************************
139 * Waits <delay> milliseconds.
140 ************************************************************************/
141 void hb_snooze( int delay )
147 #if defined( SYS_BEOS )
148 snooze( 1000 * delay );
149 #elif defined( SYS_DARWIN ) || defined( SYS_LINUX ) || defined( SYS_FREEBSD) || defined( SYS_SunOS )
150 usleep( 1000 * delay );
151 #elif defined( SYS_CYGWIN ) || defined( SYS_MINGW )
156 /************************************************************************
158 ************************************************************************
159 * Whenever possible, returns the number of CPUs on the current
160 * computer. Returns 1 otherwise.
161 * The detection is actually only performed on the first call.
162 ************************************************************************/
163 int hb_get_cpu_count()
165 static int cpu_count = 0;
173 #if defined(SYS_CYGWIN) || defined(SYS_MINGW)
175 GetSystemInfo( &cpuinfo );
176 cpu_count = cpuinfo.dwNumberOfProcessors;
178 #elif defined(SYS_LINUX)
181 memset( &p_aff, 0, sizeof(p_aff) );
182 sched_getaffinity( 0, sizeof(p_aff), &p_aff );
183 for( cpu_count = 0, bit = 0; bit < sizeof(p_aff); bit++ )
184 cpu_count += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
186 #elif defined(SYS_BEOS)
188 get_system_info( &info );
189 cpu_count = info.cpu_count;
191 #elif defined(SYS_DARWIN) || defined(SYS_FREEBSD) || defined(SYS_OPENBSD)
192 size_t length = sizeof( cpu_count );
194 int mib[2] = { CTL_HW, HW_NCPU };
195 if( sysctl(mib, 2, &cpu_count, &length, NULL, 0) )
197 if( sysctlbyname("hw.ncpu", &cpu_count, &length, NULL, 0) )
203 #elif defined( SYS_SunOS )
205 processorid_t cpumax;
208 cpumax = sysconf(_SC_CPUID_MAX);
210 for(i = 0; i <= cpumax; i++ )
212 if(p_online(i, P_STATUS) != -1)
221 cpu_count = MAX( 1, cpu_count );
222 cpu_count = MIN( cpu_count, 8 );
227 /************************************************************************
228 * Get a tempory directory for HB
229 ***********************************************************************/
230 void hb_get_tempory_directory( hb_handle_t * h, char path[512] )
234 /* Create the base */
235 #if defined( SYS_CYGWIN ) || defined( SYS_MINGW )
237 int i_size = GetTempPath( 512, base );
238 if( i_size <= 0 || i_size >= 512 )
240 if( getcwd( base, 512 ) == NULL )
241 strcpy( base, "c:" ); /* Bad fallback but ... */
244 /* c:/path/ works like a charm under cygwin(win32?) so use it */
245 while( ( p = strchr( base, '\\' ) ) )
248 strcpy( base, "/tmp" );
250 /* I prefer to remove evntual last '/' (for cygwin) */
251 if( base[strlen(base)-1] == '/' )
252 base[strlen(base)-1] = '\0';
254 snprintf( path, 512, "%s/hb.%d", base, hb_get_pid( h ) );
257 /************************************************************************
258 * Get a tempory filename for HB
259 ***********************************************************************/
260 void hb_get_tempory_filename( hb_handle_t * h, char name[1024],
265 hb_get_tempory_directory( h, name );
268 va_start( args, fmt );
269 vsnprintf( &name[strlen(name)], 1024 - strlen(name), fmt, args );
273 /************************************************************************
275 ************************************************************************
276 * Wrapper to the real mkdir, needed only because it doesn't take a
277 * second argument on Win32. Grrr.
278 ***********************************************************************/
279 void hb_mkdir( char * name )
288 /************************************************************************
289 * Portable thread implementation
290 ***********************************************************************/
295 void (* function) ( void * );
301 #if defined( SYS_BEOS )
305 //#elif defined( SYS_CYGWIN )
310 /* Get a unique identifier to thread and represent as 64-bit unsigned.
311 * If unsupported, the value 0 is be returned.
312 * Caller should use result only for display/log purposes.
314 static uint64_t hb_thread_to_integer( const hb_thread_t* t )
316 #if defined( USE_PTHREAD )
317 #if defined( SYS_CYGWIN )
318 return (uint64_t)t->thread;
319 #elif defined( _WIN32 ) || defined( __MINGW32__ )
320 return (uint64_t)(ptrdiff_t)t->thread.p;
321 #elif defined( SYS_DARWIN )
322 return (unsigned long)t->thread;
324 return (uint64_t)t->thread;
331 /************************************************************************
333 ************************************************************************
334 * We use it as the root routine for any thread, for two reasons:
335 * + To set the thread priority on OS X (pthread_setschedparam() could
336 * be called from hb_thread_init(), but it's nicer to do it as we
337 * are sure it is done before the real routine starts)
338 * + Get informed when the thread exits, so we know whether
339 * hb_thread_close() will block or not.
340 ***********************************************************************/
341 static void hb_thread_func( void * _t )
343 hb_thread_t * t = (hb_thread_t *) _t;
345 #if defined( SYS_DARWIN )
346 /* Set the thread priority */
347 struct sched_param param;
348 memset( ¶m, 0, sizeof( struct sched_param ) );
349 param.sched_priority = t->priority;
350 pthread_setschedparam( pthread_self(), SCHED_OTHER, ¶m );
353 #if defined( SYS_BEOS )
354 signal( SIGINT, SIG_IGN );
357 /* Start the actual routine */
358 t->function( t->arg );
360 /* Inform that the thread can be joined now */
361 hb_deep_log( 2, "thread %"PRIx64" exited (\"%s\")", hb_thread_to_integer( t ), t->name );
364 hb_unlock( t->lock );
367 /************************************************************************
369 ************************************************************************
370 * name: user-friendly name
371 * function: the thread routine
372 * arg: argument of the routine
373 * priority: HB_LOW_PRIORITY or HB_NORMAL_PRIORITY
374 ***********************************************************************/
375 hb_thread_t * hb_thread_init( char * name, void (* function)(void *),
376 void * arg, int priority )
378 hb_thread_t * t = calloc( sizeof( hb_thread_t ), 1 );
380 t->name = strdup( name );
381 t->function = function;
383 t->priority = priority;
385 t->lock = hb_lock_init();
387 /* Create and start the thread */
388 #if defined( SYS_BEOS )
389 t->thread = spawn_thread( (thread_func) hb_thread_func,
391 resume_thread( t->thread );
394 pthread_create( &t->thread, NULL,
395 (void * (*)( void * )) hb_thread_func, t );
397 //#elif defined( SYS_CYGWIN )
398 // t->thread = CreateThread( NULL, 0,
399 // (LPTHREAD_START_ROUTINE) hb_thread_func, t, 0, NULL );
401 // /* Maybe use THREAD_PRIORITY_LOWEST instead */
402 // if( priority == HB_LOW_PRIORITY )
403 // SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL );
406 hb_deep_log( 2, "thread %"PRIx64" started (\"%s\")", hb_thread_to_integer( t ), t->name );
410 /************************************************************************
412 ************************************************************************
413 * Joins the thread and frees memory.
414 ***********************************************************************/
415 void hb_thread_close( hb_thread_t ** _t )
417 hb_thread_t * t = *_t;
419 /* Join the thread */
420 #if defined( SYS_BEOS )
422 wait_for_thread( t->thread, &exit_value );
425 pthread_join( t->thread, NULL );
427 //#elif defined( SYS_CYGWIN )
428 // WaitForSingleObject( t->thread, INFINITE );
431 hb_deep_log( 2, "thread %"PRIx64" joined (\"%s\")", hb_thread_to_integer( t ), t->name );
433 hb_lock_close( &t->lock );
439 /************************************************************************
440 * hb_thread_has_exited()
441 ************************************************************************
442 * Returns 1 if the thread can be joined right away, 0 otherwise.
443 ***********************************************************************/
444 int hb_thread_has_exited( hb_thread_t * t )
450 hb_unlock( t->lock );
455 /************************************************************************
456 * Portable mutex implementation
457 ***********************************************************************/
460 #if defined( SYS_BEOS )
463 pthread_mutex_t mutex;
464 //#elif defined( SYS_CYGWIN )
469 /************************************************************************
474 ************************************************************************
475 * Basic wrappers to OS-specific semaphore or mutex functions.
476 ***********************************************************************/
477 hb_lock_t * hb_lock_init()
479 hb_lock_t * l = calloc( sizeof( hb_lock_t ), 1 );
481 #if defined( SYS_BEOS )
482 l->sem = create_sem( 1, "sem" );
484 pthread_mutexattr_t mta;
486 pthread_mutexattr_init(&mta);
488 #if defined( SYS_CYGWIN )
489 pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_NORMAL);
492 pthread_mutex_init( &l->mutex, &mta );
493 //#elif defined( SYS_CYGWIN )
494 // l->mutex = CreateMutex( 0, FALSE, 0 );
500 void hb_lock_close( hb_lock_t ** _l )
504 #if defined( SYS_BEOS )
505 delete_sem( l->sem );
507 pthread_mutex_destroy( &l->mutex );
508 //#elif defined( SYS_CYGWIN )
509 // CloseHandle( l->mutex );
516 void hb_lock( hb_lock_t * l )
518 #if defined( SYS_BEOS )
519 acquire_sem( l->sem );
521 pthread_mutex_lock( &l->mutex );
522 //#elif defined( SYS_CYGWIN )
523 // WaitForSingleObject( l->mutex, INFINITE );
527 void hb_unlock( hb_lock_t * l )
529 #if defined( SYS_BEOS )
530 release_sem( l->sem );
532 pthread_mutex_unlock( &l->mutex );
533 //#elif defined( SYS_CYGWIN )
534 // ReleaseMutex( l->mutex );
538 /************************************************************************
539 * Portable condition variable implementation
540 ***********************************************************************/
543 #if defined( SYS_BEOS )
547 //#elif defined( SYS_CYGWIN )
552 /************************************************************************
557 ************************************************************************
558 * Win9x is not supported by this implementation (SignalObjectAndWait()
559 * only available on Windows 2000/XP).
560 ***********************************************************************/
561 hb_cond_t * hb_cond_init()
563 hb_cond_t * c = calloc( sizeof( hb_cond_t ), 1 );
565 #if defined( SYS_BEOS )
568 pthread_cond_init( &c->cond, NULL );
569 //#elif defined( SYS_CYGWIN )
570 // c->event = CreateEvent( NULL, FALSE, FALSE, NULL );
576 void hb_cond_close( hb_cond_t ** _c )
580 #if defined( SYS_BEOS )
582 pthread_cond_destroy( &c->cond );
583 //#elif defined( SYS_CYGWIN )
584 // CloseHandle( c->event );
591 void hb_cond_wait( hb_cond_t * c, hb_lock_t * lock )
593 #if defined( SYS_BEOS )
594 c->thread = find_thread( NULL );
595 release_sem( lock->sem );
596 suspend_thread( c->thread );
597 acquire_sem( lock->sem );
600 pthread_cond_wait( &c->cond, &lock->mutex );
601 //#elif defined( SYS_CYGWIN )
602 // SignalObjectAndWait( lock->mutex, c->event, INFINITE, FALSE );
603 // WaitForSingleObject( lock->mutex, INFINITE );
607 void hb_cond_signal( hb_cond_t * c )
609 #if defined( SYS_BEOS )
610 while( c->thread != -1 )
613 get_thread_info( c->thread, &info );
614 if( info.state == B_THREAD_SUSPENDED )
616 resume_thread( c->thread );
619 /* Looks like we have been called between hb_cond_wait's
620 release_sem() and suspend_thread() lines. Wait until the
621 thread is actually suspended before we resume it */
625 pthread_cond_signal( &c->cond );
626 //#elif defined( SYS_CYGWIN )
627 // PulseEvent( c->event );
631 /************************************************************************
633 ***********************************************************************/
640 hb_net_t * hb_net_open( char * address, int port )
642 hb_net_t * n = calloc( sizeof( hb_net_t ), 1 );
644 struct sockaddr_in sock;
645 struct hostent * host;
649 int iResult, winsock_init = 0;
651 // Initialize Winsock
654 iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
657 hb_log("WSAStartup failed: %d", iResult);
664 /* TODO: find out why this doesn't work on Win32 */
665 if( !( host = gethostbyname( address ) ) )
667 hb_log( "gethostbyname failed (%s)", address );
672 memset( &sock, 0, sizeof( struct sockaddr_in ) );
673 sock.sin_family = host->h_addrtype;
674 sock.sin_port = htons( port );
675 memcpy( &sock.sin_addr, host->h_addr, host->h_length );
677 if( ( n->socket = socket( host->h_addrtype, SOCK_STREAM, 0 ) ) < 0 )
679 hb_log( "socket failed" );
684 if( connect( n->socket, (struct sockaddr *) &sock,
685 sizeof( struct sockaddr_in ) ) < 0 )
687 hb_log( "connect failed" );
695 int hb_net_send( hb_net_t * n, char * buffer )
697 return send( n->socket, buffer, strlen( buffer ), 0 );
700 int hb_net_recv( hb_net_t * n, char * buffer, int size )
702 return recv( n->socket, buffer, size - 1, 0 );
705 void hb_net_close( hb_net_t ** _n )
707 hb_net_t * n = (hb_net_t *) *_n;