Fix major overstep with last commit
[vg.git] / src / vg / vg_loader.h
index 2ba707e0d03c83739c65451ee9a4ddbb5e18b784..6e3882af4c2bd5696f7c09e4b451e172136e483e 100644 (file)
 
 static struct vg_loader
 {
-   /* Synchronization */
-   vg_semaphore sem_loading;
-   vg_mutex mux_status;
-
-   enum loader_status
-   {
-      k_loader_status_loading,
-      k_loader_status_complete,
-      k_loader_status_fail
-   }
-   status;
-
    /* Shutdown steps */
    struct loader_free_step
    {
@@ -39,37 +27,24 @@ vg_loader;
 
 static int vg_loader_init(void)
 {
-   vg_semaphore_init( &vg_loader.sem_loading, 0 );
-   vg_mutex_init( &vg_loader.mux_status );
    return 1;
 }
 
 static void vg_loader_free(void)
 {
-   vg_semaphore_wait( &vg_loader.sem_loading );
-   vg_semaphore_free( &vg_loader.sem_loading );
-   vg_mutex_free( &vg_loader.mux_status );
+   vg_info( "vg_loader_free\n" );
 
    for( int i=0; i<vg_loader.step_count; i++ )
    {
       struct loader_free_step *step = 
          &vg_loader.step_buffer[vg_loader.step_count -1 -i];
 
+      vg_info( " -> %p\n", step->fn_free );
       step->fn_free( step->data );
    }
 
-   free( vg_loader.step_buffer );
-}
-
-static enum loader_status vg_loader_status(void)
-{
-   enum loader_status answer;
-
-   vg_mutex_lock( &vg_loader.mux_status );
-   answer = vg_loader.status;
-   vg_mutex_unlock( &vg_loader.mux_status );
-
-   return answer;
+   vg_free( vg_loader.step_buffer );
+   vg_info( "done\n" );
 }
 
 static float hue_to_rgb( float p, float q, float t )
@@ -84,8 +59,8 @@ static float hue_to_rgb( float p, float q, float t )
 
 static void vg_loader_render(void)
 {
-   float h = vg_fractf(vg_time*0.1),
-         s = 0.2f,
+   float h = vg_randf(),
+         s = 0.7f,
          l = 0.1f, //* (0.5f+vg_fractf(vg_time*40.0)*0.5f),
          q = l < 0.5f ? l * (1.0f + s) : l + s - l * s,
          p = 2.0f * l - q,
@@ -131,65 +106,56 @@ static void vg_loader_render(void)
 }
 
 
-static int vg_load_full(void);
+static void vg_load_full(void);
 
 static void vg_loader_thread(void * nothing)
 {
-   /* Run client loader */
-   int res = vg_load_full();
-
-   /* Propogate status */
-   vg_mutex_lock( &vg_loader.mux_status );
-   if( res )
-   {
-      vg_loader.status = k_loader_status_complete;
-   }
-   else
-   {
-      vg_loader.status = k_loader_status_fail;
-   }
-   vg_mutex_unlock( &vg_loader.mux_status );
+   vg_thread_info.gl_context_level = 0;
+   vg_thread_info.purpose = k_thread_purpose_loader;
+   vg_set_thread_name( "[vg] Loader" );
 
-   vg_semaphore_post( &vg_loader.sem_loading );
+   /* Run client loader */
+   vg_load_full();
+   vg_semaphore_post( &vg.sem_loader );
 }
 
 static void vg_loader_start(void)
 {
+   vg_semaphore_wait( &vg.sem_loader );
    vg_thread_run( vg_loader_thread, NULL );
 }
 
 static void vg_free_libc_malloced( void *data )
 {
-   free( data );
+   vg_free( data );
 }
 
-static int vg_loader_push_free_step( struct loader_free_step step )
+static void vg_loader_push_free_step( struct loader_free_step step )
 {
-   void *buf = buffer_reserve( vg_loader.step_buffer, vg_loader.step_count, 
+   vg_loader.step_buffer = 
+      buffer_reserve( vg_loader.step_buffer, vg_loader.step_count, 
                               &vg_loader.step_cap, 1, 
                               sizeof( struct loader_free_step ) );
 
-   if( !buf )
-      return 0;
-
-   vg_loader.step_buffer = buf;
    vg_loader.step_buffer[ vg_loader.step_count ++ ] = step;
-   return 1;
 }
+
 /*
- * Schedule something to be freed
+ * Schedule something to be ran now, freed later
  */
-__attribute__((warn_unused_result))
-static int vg_loader_highwater( void( *fn_free )(void *), void *data )
+static void vg_loader_highwater( void( *fn_load )(void), 
+                                 void( *fn_free )(void *), void *data )
 {
+   if( fn_load )
+      fn_load();
+
    if( fn_free )
    {
       struct loader_free_step step;
       step.data = data;
       step.fn_free = fn_free;
 
-      if( !vg_loader_push_free_step( step ) )
-         return 0;
+      vg_loader_push_free_step( step );
    }
    else
    {
@@ -199,152 +165,11 @@ static int vg_loader_highwater( void( *fn_free )(void *), void *data )
          step.data = data;
          step.fn_free = vg_free_libc_malloced;
 
-         if( !vg_loader_push_free_step( step ) )
-            return 0;
+         vg_loader_push_free_step( step );
       }
    }
 
-   vg_mutex_lock( &vg.mux_engine_status );
-
-   if( !vg.engine_running )
-   {
-      vg_mutex_unlock( &vg.mux_engine_status );
-      return 0;
-   }
-
-   vg_mutex_unlock( &vg.mux_engine_status );
-   return 1;
+   vg_ensure_engine_running();
 }
 
 #endif /* VG_LOADER_H */
-
-#if 0
-#ifndef LOADER_H
-#define LOADER_H
-
-#include "common.h"
-
-static struct loader
-{
-   MUTEX_TYPE mux;
-
-   struct loader_step
-   {
-      int (*fn_load)(void);
-      void (*fn_free)(void);
-
-      int require_opengl;
-      const char *name;
-   }
-   *step_buffer;
-   u32 step_count, step_cap, step_action, 
-       low_water_mark; /* What is the minumum number of systems we can have? */
-
-   enum loader_status
-   {
-      k_loader_status_loading,
-      k_loader_status_complete,
-      k_loader_status_fail
-   }
-   status;
-
-   int cancel;
-}
-loader;
-
-static void loader_add_stage( struct loader_step step )
-{
-   loader.step_buffer = buffer_reserve( loader.step_buffer, loader.step_count, 
-                                        &loader.step_cap,
-                                        1, sizeof( struct loader_step ) );
-
-   loader.step_buffer[ loader.step_count ++ ] = step;
-}
-
-static void loader_insert_stage( struct loader_step step )
-{
-   
-}
-
-static void loader_cancel(void)
-{
-   MUTEX_LOCK( loader.mux );
-   loader.cancel = 1;
-   MUTEX_UNLOCK( loader.mux );
-}
-
-static void loader_worker_thread( void *nothing )
-{
-   while(1)
-   {
-      vg_sleep_ms( 1000.0 );
-      vg_info( "... loader ....\n" );
-
-      if( loader.cancel )
-      {
-         return;
-      }
-   }
-}
-
-static void loader_begin(void)
-{
-   if( loader.step_count == 0 )
-   {
-      loader.status = k_loader_status_complete;
-      return;
-   }
-
-   loader.status = k_loader_status_loading;
-   vg_thread_run( loader_worker_thread, NULL );
-}
-
-static void loader_free(void)
-{
-   /* TODO */
-   for( int i=0; i<loader.step_count; i++ )
-   {
-      struct loader_step *step = &loader.step_buffer[ loader.step_count -i -1 ];
-      if( step->fn_free )
-         step->fn_free();
-   }
-}
-
-/*
- * Returns 0 if loading is not happening
- * Returns 1 if we are loading something
- */
-static int loader_update(void)
-{
-   MUTEX_LOCK( loader.mux );
-
-   if( loader.status == k_loader_status_complete )
-   {
-      MUTEX_UNLOCK( loader.mux );
-      return 0;
-   }
-   else
-   {
-      struct loader_step *cstep = &loader.step_buffer[ loader.step_action ];
-
-      if( cstep->require_opengl )
-      {
-         if( !cstep->fn_load() )
-         {
-            loader.cancel = 1;
-            MUTEX_UNLOCK( loader.mux );
-            
-            loader_free();
-            vg_exit();
-         }
-
-         loader.step_action ++;
-      }
-
-      MUTEX_UNLOCK( loader.mux );
-      return 1;
-   }
-}
-
-#endif /* LOADER_H */
-#endif