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>
35 #include <sys/processor.h>
43 //#include <winsock2.h>
44 //#include <ws2tcpip.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
49 #include <netinet/in.h>
54 /************************************************************************
56 ************************************************************************
57 * Returns the current date in milliseconds.
58 * On Win32, we implement a gettimeofday emulation here because
59 * libdvdread and libmp4v2 use it without checking.
60 ************************************************************************/
67 int gettimeofday( struct timeval * tv, struct timezone * tz )
70 tick = GetTickCount();
71 tv->tv_sec = tick / 1000;
72 tv->tv_usec = ( tick % 1000 ) * 1000;
78 uint64_t hb_get_date()
81 gettimeofday( &tv, NULL );
82 return( (uint64_t) tv.tv_sec * 1000 + (uint64_t) tv.tv_usec / 1000 );
85 /************************************************************************
87 ************************************************************************
88 * Waits <delay> milliseconds.
89 ************************************************************************/
90 void hb_snooze( int delay )
96 #if defined( SYS_BEOS )
97 snooze( 1000 * delay );
98 #elif defined( SYS_DARWIN ) || defined( SYS_LINUX ) || defined( SYS_FREEBSD) || defined( SYS_SunOS )
99 usleep( 1000 * delay );
100 #elif defined( SYS_CYGWIN )
105 /************************************************************************
107 ************************************************************************
108 * Whenever possible, returns the number of CPUs on the current
109 * computer. Returns 1 otherwise.
110 * The detection is actually only performed on the first call.
111 ************************************************************************/
112 int hb_get_cpu_count()
114 static int cpu_count = 0;
122 #if defined(SYS_CYGWIN)
124 GetSystemInfo( &cpuinfo );
125 cpu_count = cpuinfo.dwNumberOfProcessors;
127 #elif defined(SYS_LINUX)
130 memset( &p_aff, 0, sizeof(p_aff) );
131 sched_getaffinity( 0, sizeof(p_aff), &p_aff );
132 for( cpu_count = 0, bit = 0; bit < sizeof(p_aff); bit++ )
133 cpu_count += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
135 #elif defined(SYS_BEOS)
137 get_system_info( &info );
138 cpu_count = info.cpu_count;
140 #elif defined(SYS_DARWIN) || defined(SYS_FREEBSD) || defined(SYS_OPENBSD)
141 size_t length = sizeof( cpu_count );
143 int mib[2] = { CTL_HW, HW_NCPU };
144 if( sysctl(mib, 2, &cpu_count, &length, NULL, 0) )
146 if( sysctlbyname("hw.ncpu", &cpu_count, &length, NULL, 0) )
152 #elif defined( SYS_SunOS )
154 processorid_t cpumax;
157 cpumax = sysconf(_SC_CPUID_MAX);
159 for(i = 0; i <= cpumax; i++ )
161 if(p_online(i, P_STATUS) != -1)
170 cpu_count = MAX( 1, cpu_count );
171 cpu_count = MIN( cpu_count, 8 );
176 /************************************************************************
177 * Get a tempory directory for HB
178 ***********************************************************************/
179 void hb_get_tempory_directory( hb_handle_t * h, char path[512] )
183 /* Create the base */
186 int i_size = GetTempPath( 512, base );
187 if( i_size <= 0 || i_size >= 512 )
189 if( getcwd( base, 512 ) == NULL )
190 strcpy( base, "c:" ); /* Bad fallback but ... */
193 /* c:/path/ works like a charm under cygwin(win32?) so use it */
194 while( ( p = strchr( base, '\\' ) ) )
197 strcpy( base, "/tmp" );
199 /* I prefer to remove evntual last '/' (for cygwin) */
200 if( base[strlen(base)-1] == '/' )
201 base[strlen(base)-1] = '\0';
203 snprintf( path, 512, "%s/hb.%d", base, hb_get_pid( h ) );
206 /************************************************************************
207 * Get a tempory filename for HB
208 ***********************************************************************/
209 void hb_get_tempory_filename( hb_handle_t * h, char name[1024],
214 hb_get_tempory_directory( h, name );
217 va_start( args, fmt );
218 vsnprintf( &name[strlen(name)], 1024 - strlen(name), fmt, args );
222 /************************************************************************
224 ************************************************************************
225 * Wrapper to the real mkdir, needed only because it doesn't take a
226 * second argument on Win32. Grrr.
227 ***********************************************************************/
228 void hb_mkdir( char * name )
237 /************************************************************************
238 * Portable thread implementation
239 ***********************************************************************/
244 void (* function) ( void * );
250 #if defined( SYS_BEOS )
254 //#elif defined( SYS_CYGWIN )
259 /************************************************************************
261 ************************************************************************
262 * We use it as the root routine for any thread, for two reasons:
263 * + To set the thread priority on OS X (pthread_setschedparam() could
264 * be called from hb_thread_init(), but it's nicer to do it as we
265 * are sure it is done before the real routine starts)
266 * + Get informed when the thread exits, so we know whether
267 * hb_thread_close() will block or not.
268 ***********************************************************************/
269 static void hb_thread_func( void * _t )
271 hb_thread_t * t = (hb_thread_t *) _t;
273 #if defined( SYS_DARWIN )
274 /* Set the thread priority */
275 struct sched_param param;
276 memset( ¶m, 0, sizeof( struct sched_param ) );
277 param.sched_priority = t->priority;
278 pthread_setschedparam( pthread_self(), SCHED_OTHER, ¶m );
281 #if defined( SYS_BEOS )
282 signal( SIGINT, SIG_IGN );
285 /* Start the actual routine */
286 t->function( t->arg );
288 /* Inform that the thread can be joined now */
289 hb_deep_log( 2, "thread %x exited (\"%s\")", t->thread, t->name );
292 hb_unlock( t->lock );
295 /************************************************************************
297 ************************************************************************
298 * name: user-friendly name
299 * function: the thread routine
300 * arg: argument of the routine
301 * priority: HB_LOW_PRIORITY or HB_NORMAL_PRIORITY
302 ***********************************************************************/
303 hb_thread_t * hb_thread_init( char * name, void (* function)(void *),
304 void * arg, int priority )
306 hb_thread_t * t = calloc( sizeof( hb_thread_t ), 1 );
308 t->name = strdup( name );
309 t->function = function;
311 t->priority = priority;
313 t->lock = hb_lock_init();
315 /* Create and start the thread */
316 #if defined( SYS_BEOS )
317 t->thread = spawn_thread( (thread_func) hb_thread_func,
319 resume_thread( t->thread );
322 pthread_create( &t->thread, NULL,
323 (void * (*)( void * )) hb_thread_func, t );
325 //#elif defined( SYS_CYGWIN )
326 // t->thread = CreateThread( NULL, 0,
327 // (LPTHREAD_START_ROUTINE) hb_thread_func, t, 0, NULL );
329 // /* Maybe use THREAD_PRIORITY_LOWEST instead */
330 // if( priority == HB_LOW_PRIORITY )
331 // SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL );
334 hb_deep_log( 2, "thread %x started (\"%s\")", t->thread, t->name );
338 /************************************************************************
340 ************************************************************************
341 * Joins the thread and frees memory.
342 ***********************************************************************/
343 void hb_thread_close( hb_thread_t ** _t )
345 hb_thread_t * t = *_t;
347 /* Join the thread */
348 #if defined( SYS_BEOS )
350 wait_for_thread( t->thread, &exit_value );
353 pthread_join( t->thread, NULL );
355 //#elif defined( SYS_CYGWIN )
356 // WaitForSingleObject( t->thread, INFINITE );
359 hb_deep_log( 2, "thread %x joined (\"%s\")",
360 t->thread, t->name );
362 hb_lock_close( &t->lock );
368 /************************************************************************
369 * hb_thread_has_exited()
370 ************************************************************************
371 * Returns 1 if the thread can be joined right away, 0 otherwise.
372 ***********************************************************************/
373 int hb_thread_has_exited( hb_thread_t * t )
379 hb_unlock( t->lock );
384 /************************************************************************
385 * Portable mutex implementation
386 ***********************************************************************/
389 #if defined( SYS_BEOS )
392 pthread_mutex_t mutex;
393 //#elif defined( SYS_CYGWIN )
398 /************************************************************************
403 ************************************************************************
404 * Basic wrappers to OS-specific semaphore or mutex functions.
405 ***********************************************************************/
406 hb_lock_t * hb_lock_init()
408 hb_lock_t * l = calloc( sizeof( hb_lock_t ), 1 );
410 #if defined( SYS_BEOS )
411 l->sem = create_sem( 1, "sem" );
413 pthread_mutexattr_t mta;
415 pthread_mutexattr_init(&mta);
417 #if defined( SYS_CYGWIN )
418 pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_NORMAL);
421 pthread_mutex_init( &l->mutex, &mta );
422 //#elif defined( SYS_CYGWIN )
423 // l->mutex = CreateMutex( 0, FALSE, 0 );
429 void hb_lock_close( hb_lock_t ** _l )
433 #if defined( SYS_BEOS )
434 delete_sem( l->sem );
436 pthread_mutex_destroy( &l->mutex );
437 //#elif defined( SYS_CYGWIN )
438 // CloseHandle( l->mutex );
445 void hb_lock( hb_lock_t * l )
447 #if defined( SYS_BEOS )
448 acquire_sem( l->sem );
450 pthread_mutex_lock( &l->mutex );
451 //#elif defined( SYS_CYGWIN )
452 // WaitForSingleObject( l->mutex, INFINITE );
456 void hb_unlock( hb_lock_t * l )
458 #if defined( SYS_BEOS )
459 release_sem( l->sem );
461 pthread_mutex_unlock( &l->mutex );
462 //#elif defined( SYS_CYGWIN )
463 // ReleaseMutex( l->mutex );
467 /************************************************************************
468 * Portable condition variable implementation
469 ***********************************************************************/
472 #if defined( SYS_BEOS )
476 //#elif defined( SYS_CYGWIN )
481 /************************************************************************
486 ************************************************************************
487 * Win9x is not supported by this implementation (SignalObjectAndWait()
488 * only available on Windows 2000/XP).
489 ***********************************************************************/
490 hb_cond_t * hb_cond_init()
492 hb_cond_t * c = calloc( sizeof( hb_cond_t ), 1 );
494 #if defined( SYS_BEOS )
497 pthread_cond_init( &c->cond, NULL );
498 //#elif defined( SYS_CYGWIN )
499 // c->event = CreateEvent( NULL, FALSE, FALSE, NULL );
505 void hb_cond_close( hb_cond_t ** _c )
509 #if defined( SYS_BEOS )
511 pthread_cond_destroy( &c->cond );
512 //#elif defined( SYS_CYGWIN )
513 // CloseHandle( c->event );
520 void hb_cond_wait( hb_cond_t * c, hb_lock_t * lock )
522 #if defined( SYS_BEOS )
523 c->thread = find_thread( NULL );
524 release_sem( lock->sem );
525 suspend_thread( c->thread );
526 acquire_sem( lock->sem );
529 pthread_cond_wait( &c->cond, &lock->mutex );
530 //#elif defined( SYS_CYGWIN )
531 // SignalObjectAndWait( lock->mutex, c->event, INFINITE, FALSE );
532 // WaitForSingleObject( lock->mutex, INFINITE );
536 void hb_cond_signal( hb_cond_t * c )
538 #if defined( SYS_BEOS )
539 while( c->thread != -1 )
542 get_thread_info( c->thread, &info );
543 if( info.state == B_THREAD_SUSPENDED )
545 resume_thread( c->thread );
548 /* Looks like we have been called between hb_cond_wait's
549 release_sem() and suspend_thread() lines. Wait until the
550 thread is actually suspended before we resume it */
554 pthread_cond_signal( &c->cond );
555 //#elif defined( SYS_CYGWIN )
556 // PulseEvent( c->event );
560 /************************************************************************
562 ***********************************************************************/
569 hb_net_t * hb_net_open( char * address, int port )
571 hb_net_t * n = calloc( sizeof( hb_net_t ), 1 );
573 struct sockaddr_in sock;
574 struct hostent * host;
576 /* TODO: find out why this doesn't work on Win32 */
577 if( !( host = gethostbyname( address ) ) )
579 hb_log( "gethostbyname failed (%s)", address );
584 memset( &sock, 0, sizeof( struct sockaddr_in ) );
585 sock.sin_family = host->h_addrtype;
586 sock.sin_port = htons( port );
587 memcpy( &sock.sin_addr, host->h_addr, host->h_length );
589 if( ( n->socket = socket( host->h_addrtype, SOCK_STREAM, 0 ) ) < 0 )
591 hb_log( "socket failed" );
596 if( connect( n->socket, (struct sockaddr *) &sock,
597 sizeof( struct sockaddr_in ) ) < 0 )
599 hb_log( "connect failed" );
607 int hb_net_send( hb_net_t * n, char * buffer )
609 return send( n->socket, buffer, strlen( buffer ), 0 );
612 int hb_net_recv( hb_net_t * n, char * buffer, int size )
614 return recv( n->socket, buffer, size - 1, 0 );
617 void hb_net_close( hb_net_t ** _n )
619 hb_net_t * n = (hb_net_t *) *_n;