commit 17e6e5f68c9b1982d10df8789edec98bd53e4412 parent da9c6ad7d5ad7fd99ab5227ebb588323071f31b3 Author: Michael Savage <mikejsavage@gmail.com> Date: Sun Jan 31 11:24:01 +0000 Convert platform specific stuff into headers Diffstat:
darwin_semaphore.cc | | | 17 | ----------------- |
darwin_semaphore.h | | | 22 | ++++++++++++++++++++++ |
linux_semaphore.cc | | | 21 | --------------------- |
linux_semaphore.h | | | 26 | ++++++++++++++++++++++++++ |
linuxdarwin_backtrace.cc | | | 11 | ----------- |
linuxdarwin_backtrace.h | | | 15 | +++++++++++++++ |
platform_atomic.h | | | 2 | +- |
platform_backtrace.h | | | 4 | +--- |
platform_semaphore.h | | | 4 | ++-- |
platform_thread.h | | | 2 | +- |
unix_atomic.cc | | | 40 | ---------------------------------------- |
unix_atomic.h | | | 45 | +++++++++++++++++++++++++++++++++++++++++++++ |
unix_thread.cc | | | 17 | ----------------- |
unix_thread.h | | | 22 | ++++++++++++++++++++++ |
diff --git a/darwin_semaphore.cc b/darwin_semaphore.cc @@ -1,17 +0,0 @@ -#include <dispatch/dispatch.h> - -struct Semaphore { - dispatch_semaphore_t sem; -}; - -inline void semaphore_init( Semaphore * const sem ) { - sem->sem = dispatch_semaphore_create( 0 ); -} - -inline void semaphore_signal( Semaphore * const sem ) { - dispatch_semaphore_signal( sem->sem ); -} - -inline void semaphore_wait( Semaphore * const sem ) { - dispatch_semaphore_wait( sem->sem, DISPATCH_TIME_FOREVER ); -} diff --git a/darwin_semaphore.h b/darwin_semaphore.h @@ -0,0 +1,22 @@ +#ifndef _DARWIN_SEMAPHORE_H_ +#define _DARWIN_SEMAPHORE_H_ + +#include <dispatch/dispatch.h> + +struct Semaphore { + dispatch_semaphore_t sem; +}; + +inline void semaphore_init( Semaphore * const sem ) { + sem->sem = dispatch_semaphore_create( 0 ); +} + +inline void semaphore_signal( Semaphore * const sem ) { + dispatch_semaphore_signal( sem->sem ); +} + +inline void semaphore_wait( Semaphore * const sem ) { + dispatch_semaphore_wait( sem->sem, DISPATCH_TIME_FOREVER ); +} + +#endif // _DARWIN_SEMAPHORE_H_ diff --git a/linux_semaphore.cc b/linux_semaphore.cc @@ -1,21 +0,0 @@ -#include <err.h> -#include <semaphore.h> - -struct Semaphore { - sem_t sem; -}; - -inline void semaphore_init( Semaphore * const sem ) { - const int ok = sem_init( &sem->sem, 0, 0 ); - if( ok == -1 ) { - err( 1, "sem_init failed" ); - } -} - -inline void semaphore_signal( Semaphore * const sem ) { - sem_post( &sem->sem ); -} - -inline void semaphore_wait( Semaphore * const sem ) { - sem_wait( &sem->sem ); -} diff --git a/linux_semaphore.h b/linux_semaphore.h @@ -0,0 +1,26 @@ +#ifndef _LINUX_SEMAPHORE_H_ +#define _LINUX_SEMAPHORE_H_ + +#include <err.h> +#include <semaphore.h> + +struct Semaphore { + sem_t sem; +}; + +inline void semaphore_init( Semaphore * const sem ) { + const int ok = sem_init( &sem->sem, 0, 0 ); + if( ok == -1 ) { + err( 1, "sem_init failed" ); + } +} + +inline void semaphore_signal( Semaphore * const sem ) { + sem_post( &sem->sem ); +} + +inline void semaphore_wait( Semaphore * const sem ) { + sem_wait( &sem->sem ); +} + +#endif // _LINUX_SEMAPHORE_H_ diff --git a/linuxdarwin_backtrace.cc b/linuxdarwin_backtrace.cc @@ -1,11 +0,0 @@ -#include <unistd.h> -#include <execinfo.h> - -#include "intrinsics.h" -#include "platform_backtrace.h" - -inline void print_backtrace() { - void * stack[ 128 ]; - const int stack_size = backtrace( stack, array_count( stack ) ); - backtrace_symbols_fd( stack, stack_size, STDERR_FILENO ); -} diff --git a/linuxdarwin_backtrace.h b/linuxdarwin_backtrace.h @@ -0,0 +1,15 @@ +#ifndef _LINUXDARWIN_BACKTRACE_H_ +#define _LINUXDARWIN_BACKTRACE_H_ + +#include <unistd.h> +#include <execinfo.h> + +#include "intrinsics.h" + +inline void print_backtrace() { + void * stack[ 128 ]; + const int stack_size = backtrace( stack, array_count( stack ) ); + backtrace_symbols_fd( stack, stack_size, STDERR_FILENO ); +} + +#endif // _LINUXDARWIN_BACKTRACE_H_ diff --git a/platform_atomic.h b/platform_atomic.h @@ -2,7 +2,7 @@ #define _PLATFORM_ATOMIC_H_ #if defined( __linux__ ) || defined( __APPLE__ ) -#include "unix_atomic.cc" +#include "unix_atomic.h" #endif #endif // _PLATFORM_ATOMIC_H_ diff --git a/platform_backtrace.h b/platform_backtrace.h @@ -1,10 +1,8 @@ #ifndef _PLATFORM_BACKTRACE_H_ #define _PLATFORM_BACKTRACE_H_ -void print_backtrace(); - #if defined( __linux__ ) || defined( __APPLE__ ) -#include "linuxdarwin_backtrace.cc" +#include "linuxdarwin_backtrace.h" #endif #endif // _PLATFORM_BACKTRACE_H_ diff --git a/platform_semaphore.h b/platform_semaphore.h @@ -3,11 +3,11 @@ #define _PLATFORM_SEMAPHORE_H_ #ifdef __linux__ -#include "linux_semaphore.cc" +#include "linux_semaphore.h" #endif #ifdef __APPLE__ -#include "darwin_semaphore.cc" +#include "darwin_semaphore.h" #endif #endif // _PLATFORM_SEMAPHORE_H_ diff --git a/platform_thread.h b/platform_thread.h @@ -2,7 +2,7 @@ #define _PLATFORM_THREAD_H_ #if defined( __linux__ ) || defined( __APPLE__ ) -#include "unix_thread.cc" +#include "unix_thread.h" #endif #endif // _PLATFORM_THREAD_H_ diff --git a/unix_atomic.cc b/unix_atomic.cc @@ -1,40 +0,0 @@ -#include "intrinsics.h" - -#define read_barrier() asm volatile ( "" ::: "memory" ) -#define write_barrier() asm volatile ( "" ::: "memory" ) - -inline u8 atomic_add_u8( volatile u8 * dest, u8 i ) { - return __sync_add_and_fetch( dest, i ); -} - -inline u16 atomic_add_u16( volatile u16 * dest, u16 i ) { - return __sync_add_and_fetch( dest, i ); -} - -inline u32 atomic_add_u32( volatile u32 * dest, u32 i ) { - return __sync_add_and_fetch( dest, i ); -} - -inline u64 atomic_add_u64( volatile u64 * dest, u64 i ) { - return __sync_add_and_fetch( dest, i ); -} - -inline bool atomic_cas_u8( volatile u8 * dest, u8 oldval, u8 newval ) { - return __sync_bool_compare_and_swap( dest, oldval, newval ); -} - -inline bool atomic_cas_u16( volatile u16 * dest, u16 oldval, u16 newval ) { - return __sync_bool_compare_and_swap( dest, oldval, newval ); -} - -inline bool atomic_cas_u32( volatile u32 * dest, u32 oldval, u32 newval ) { - return __sync_bool_compare_and_swap( dest, oldval, newval ); -} - -inline bool atomic_cas_u64( volatile u64 * dest, u64 oldval, u64 newval ) { - return __sync_bool_compare_and_swap( dest, oldval, newval ); -} - -inline bool atomic_cas_pointer( volatile void ** dest, void * oldval, void * newval ) { - return __sync_bool_compare_and_swap( dest, oldval, newval ); -} diff --git a/unix_atomic.h b/unix_atomic.h @@ -0,0 +1,45 @@ +#ifndef _UNIX_ATOMIC_H_ +#define _UNIX_ATOMIC_H_ + +#include "intrinsics.h" + +#define read_barrier() asm volatile ( "" ::: "memory" ) +#define write_barrier() asm volatile ( "" ::: "memory" ) + +inline u8 atomic_add_u8( volatile u8 * dest, u8 i ) { + return __sync_add_and_fetch( dest, i ); +} + +inline u16 atomic_add_u16( volatile u16 * dest, u16 i ) { + return __sync_add_and_fetch( dest, i ); +} + +inline u32 atomic_add_u32( volatile u32 * dest, u32 i ) { + return __sync_add_and_fetch( dest, i ); +} + +inline u64 atomic_add_u64( volatile u64 * dest, u64 i ) { + return __sync_add_and_fetch( dest, i ); +} + +inline bool atomic_cas_u8( volatile u8 * dest, u8 oldval, u8 newval ) { + return __sync_bool_compare_and_swap( dest, oldval, newval ); +} + +inline bool atomic_cas_u16( volatile u16 * dest, u16 oldval, u16 newval ) { + return __sync_bool_compare_and_swap( dest, oldval, newval ); +} + +inline bool atomic_cas_u32( volatile u32 * dest, u32 oldval, u32 newval ) { + return __sync_bool_compare_and_swap( dest, oldval, newval ); +} + +inline bool atomic_cas_u64( volatile u64 * dest, u64 oldval, u64 newval ) { + return __sync_bool_compare_and_swap( dest, oldval, newval ); +} + +inline bool atomic_cas_pointer( volatile void ** dest, void * oldval, void * newval ) { + return __sync_bool_compare_and_swap( dest, oldval, newval ); +} + +#endif // _UNIX_ATOMIC_H_ diff --git a/unix_thread.cc b/unix_thread.cc @@ -1,17 +0,0 @@ -#include <err.h> -#include <pthread.h> - -#define THREAD( f ) void * f( void * data ) -typedef THREAD( ThreadCallback ); -#define THREAD_END return NULL - -struct Thread { - pthread_t pthread; -}; - -inline void thread_init( Thread * thread, ThreadCallback callback, void * data ) { - const int ok = pthread_create( &thread->pthread, NULL, callback, data ); - if( ok == -1 ) { - err( 1, "pthread_create failed" ); - } -} diff --git a/unix_thread.h b/unix_thread.h @@ -0,0 +1,22 @@ +#ifndef _UNIX_THREAD_H_ +#define _UNIX_THREAD_H_ + +#include <err.h> +#include <pthread.h> + +#define THREAD( f ) void * f( void * data ) +typedef THREAD( ThreadCallback ); +#define THREAD_END return NULL + +struct Thread { + pthread_t pthread; +}; + +inline void thread_init( Thread * thread, ThreadCallback callback, void * data ) { + const int ok = pthread_create( &thread->pthread, NULL, callback, data ); + if( ok == -1 ) { + err( 1, "pthread_create failed" ); + } +} + +#endif // _UNIX_THREAD_H_