/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/sedflux/run_compaction.c

Go to the documentation of this file.
00001 //---
00002 //
00003 // This file is part of sedflux.
00004 //
00005 // sedflux is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // sedflux is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with sedflux; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //
00019 //---
00020 
00021 #define SED_COMPACTION_PROC_NAME "compaction"
00022 #define EH_LOG_DOMAIN SED_COMPACTION_PROC_NAME
00023 
00024 #include <stdio.h>
00025 #include <pthread.h>
00026 #include <sed/sed_sedflux.h>
00027 #include "my_processes.h"
00028 
00029 #undef WITH_THREADS
00030 
00031 #define N_THREADS 5
00032 
00033 void thread_compact( void *data , void *user_data );
00034 int compact(Sed_column,double);
00035 
00036 Sed_process_info
00037 run_compaction( Sed_process proc , Sed_cube p )
00038 {
00039    Sed_process_info info = SED_EMPTY_INFO;
00040 
00041 #if !defined(WITH_THREADS)
00042 
00043    {
00044       gint i;
00045       gint len = sed_cube_size(p);
00046       for ( i=0 ; i<len ; i++ )
00047          compact( sed_cube_col(p,i) , sed_cube_age_in_years(p) );
00048    }
00049 
00050 #else
00051 
00052    if (!g_thread_supported ()) g_thread_init(NULL);
00053 
00054    {
00055       GError *error=NULL;
00056       GThreadPool *compact_pool;
00057 
00058       compact_pool = g_thread_pool_new( (GFunc)&thread_compact ,
00059                                         (gpointer)p            ,
00060                                         N_THREADS              ,
00061                                         TRUE                   ,
00062                                         &error );
00063 
00064       eh_require( error==NULL );
00065       {
00066          gint   i;
00067          gint*  queue;
00068          gssize len = sed_cube_size(p);
00069 
00070          queue = eh_new( int , len );
00071          for ( i=0 ; i<len ; i++ )
00072          {
00073             queue[i] = i;
00074             g_thread_pool_push( compact_pool , &(queue[i]) , &error );
00075             eh_require( error==NULL );
00076          }
00077          g_thread_pool_free( compact_pool , FALSE , TRUE );
00078          eh_free( queue );
00079       }
00080    }
00081 
00082 #endif
00083 
00084    return info;
00085 }
00086 
00087 int pthread_mutex_spinlock(pthread_mutex_t *mutex);
00088 
00089 void thread_compact( void *data , void *user_data )
00090 {
00091    Sed_cube p = (Sed_cube)user_data;
00092    int i = *((int*)data);
00093 
00094    compact( sed_cube_col(p,i) , sed_cube_age_in_years(p) );
00095 
00096    return;
00097 }
00098 
00099 #define S_NTRIES 5
00100 
00101 int pthread_mutex_spinlock(pthread_mutex_t *mutex)
00102 {
00103    int i=0;
00104    while ( i++<S_NTRIES )
00105    {
00106       if ( pthread_mutex_trylock(mutex)==0 )
00107          return 0; // got the lock, return.
00108    }
00109    return pthread_mutex_lock(mutex); // give up.
00110 }
00111 

Generated on Fri Jan 4 18:04:15 2008 for sedflux by  doxygen 1.5.2