Platform and OS stability stuff
[vg.git] / src / vg / vg_platform.h
index ab9c927b79a1582fb383235cf648ee552aa6754b..e5deae28a1a39626b7c1d0eae8ebe64103b9d826 100644 (file)
@@ -33,29 +33,40 @@ struct vg_achievement
 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
 #define VG_MUST_USE_RESULT __attribute__((warn_unused_result))
 
-#ifdef _WIN32
+#ifdef _WIN32_NO
        #include <windows.h>
 
-/* TODO */
-#define VG_DEPRECATED __declspec(deprecated)
+#ifdef I_THINK_THIS_IS_WHAT_MSCV_WANTS_BUT_HAVNT_TESTED_IT_YET
+
+   #define VG_DEPRECATED         __declspec(deprecated)
+   #define VG_THREAD_LOCAL       __declspec( thread )
+
+#else /* MINGW-64 */
+
+   #define VG_THREAD_LOCAL       __thread
+   #define VG_DEPRECATED         __attribute__((deprecated))
+
+#endif
+
+   typedef HANDLE                vg_semaphore;
+   typedef HANDLE                vg_mutex;
+   typedef u64                   vg_timespec;
 
 #else
        #include <pthread.h>
    #include <semaphore.h>
 
-#define VG_DEPRECATED __attribute__((deprecated))
-
+   #define VG_DEPRECATED         __attribute__((deprecated))
    #define VG_THREAD_LOCAL       __thread
 
    typedef sem_t                 vg_semaphore;
    typedef pthread_mutex_t       vg_mutex;
+   typedef struct timespec       vg_timespec;
 
 #endif
 
 #include <stdlib.h>
 
-/* TODO: If there is no graphics, we dont need to do an exit loop */
-
 static void vg_fatal_exit_loop( const char *error );
 static void *vg_alloc( size_t size )
 {
@@ -113,9 +124,89 @@ void free( void *ptr );
 #include <math.h>
 #include <assert.h>
 
-#ifdef _WIN32
+static int     vg_thread_run( void *pfunc, void *data );
+static void    vg_thread_exit(void);
+static void    vg_set_thread_name( const char *name );
+static int     vg_semaphore_init( vg_semaphore *sem, u32 value );
+static int     vg_semaphore_trywait( vg_semaphore *sem );
+static int     vg_semaphore_wait( vg_semaphore *sem );
+static int     vg_semaphore_post( vg_semaphore *sem );
+static void    vg_semaphore_free( vg_semaphore *sem );
+static int     vg_mutex_init( vg_mutex *mutex );
+static int     vg_mutex_lock( vg_mutex *mutex );
+static int     vg_mutex_unlock( vg_mutex *mutex );
+static void    vg_mutex_free( vg_mutex *mutex );
+static void    vg_sleep_ms( long msec );
+static double  vg_time_diff( vg_timespec start, vg_timespec end );
+
+#ifdef _WIN32_NO
+
+static int vg_thread_run( void *pfunc, void *data )
+{
+       HANDLE hThread = CreateThread
+       (
+               NULL,    /* Thread attributes */
+               0,       /* Stack size (0 = use default) */
+               pfunc,   /* Thread start address */
+               data,    /* Parameter to pass to the thread */
+               0,       /* Creation flags */
+               NULL            /* Thread id */
+       );
+       
+       if ( hThread == NULL )
+       {
+               /* 
+       * Thread creation failed.
+       * More details can be retrieved by calling GetLastError()
+       */
+               return 1;
+       }
+       else
+       {
+               CloseHandle( hThread );
+               return 0;
+       }
+}
+
+static void vg_thread_exit(void)
+{
+   ExitThread(0);
+}
+
+static void vg_set_thread_name( const char *name )
+{
+   /* I believe this is a meaningless concept in windows */
+}
+
+static int     vg_semaphore_init( vg_semaphore *sem, u32 value );
+static int     vg_semaphore_trywait( vg_semaphore *sem );
+static int     vg_semaphore_wait( vg_semaphore *sem );
+static int     vg_semaphore_post( vg_semaphore *sem );
+static void    vg_semaphore_free( vg_semaphore *sem );
+static int     vg_mutex_init( vg_mutex *mutex );
+static int     vg_mutex_lock( vg_mutex *mutex );
+static int     vg_mutex_unlock( vg_mutex *mutex );
+static void    vg_mutex_free( vg_mutex *mutex );
+static void    vg_sleep_ms( long msec );
+static double  vg_time_diff( vg_timespec start, vg_timespec end );
+
 #else
 
+static int vg_thread_run( void *pfunc, void *data )
+{
+       pthread_t hThread;
+       if( pthread_create( &hThread, NULL, pfunc, data ) )
+       {
+               return 1;
+       }
+       else
+       {
+               pthread_detach( hThread );
+               return 0;
+       }
+}
+
+
 static void vg_thread_exit(void)
 {
    pthread_exit(NULL);
@@ -181,49 +272,6 @@ static void vg_mutex_free( vg_mutex *mutex )
    
 }
 
-#endif
-
-
-int vg_thread_run( void *pfunc, void *data )
-{
-#ifdef _WIN32
-       HANDLE hThread = CreateThread
-       (
-               NULL,    /* Thread attributes */
-               0,       /* Stack size (0 = use default) */
-               pfunc,   /* Thread start address */
-               data,    /* Parameter to pass to the thread */
-               0,       /* Creation flags */
-               NULL            /* Thread id */
-       );
-       
-       if ( hThread == NULL )
-       {
-               /* 
-       * Thread creation failed.
-       * More details can be retrieved by calling GetLastError()
-       */
-               return 1;
-       }
-       else
-       {
-               CloseHandle( hThread );
-               return 0;
-       }
-#else
-       pthread_t hThread;
-       if( pthread_create( &hThread, NULL, pfunc, data ) )
-       {
-               return 1;
-       }
-       else
-       {
-               pthread_detach( hThread );
-               return 0;
-       }
-#endif
-}
-
 static void vg_sleep_ms( long msec )
 {
     struct timespec ts;
@@ -242,6 +290,8 @@ static double vg_time_diff( struct timespec start, struct timespec end )
    return elapsed;
 }
 
+#endif
+
 #define VG_MIN( A, B ) ((A)<(B)?(A):(B))
 #define VG_MAX( A, B ) ((A)>(B)?(A):(B))