Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

VMDThreads.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2011 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: VMDThreads.h,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.54 $       $Date: 2010/12/16 04:08:47 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *   Remaining VMD-specific threading code used by CAVE/FreeVR builds.
00019  *   Derived from Tachyon threads code, but simplified for use in VMD.
00020  ***************************************************************************/
00021 
00022 #ifndef VMD_THREADS_INC
00023 #define VMD_THREADS_INC 1
00024 
00025 /* POSIX Threads */
00026 #if defined(__hpux) || defined(__irix) || defined(__linux) || defined(_CRAY) || defined(__osf__) || defined(_AIX) || defined(__APPLE__) || defined(__sun)
00027 #if !defined(USEPOSIXTHREADS)
00028 #define USEPOSIXTHREADS
00029 #endif
00030 #endif
00031 
00032 #ifdef VMDTHREADS
00033 #ifdef USEPOSIXTHREADS
00034 #include <pthread.h>
00035 
00036 typedef pthread_t        vmd_thread_t;
00037 typedef pthread_mutex_t   vmd_mutex_t;
00038 typedef pthread_cond_t     vmd_cond_t;
00039 
00040 typedef struct vmd_barrier_struct {
00041   int padding1[8]; /* Padding bytes to avoid false sharing and cache aliasing */
00042   pthread_mutex_t lock;   
00043   int n_clients;          
00044   int n_waiting;          
00045   int phase;              
00046   int sum;                
00047   int result;             
00048   pthread_cond_t wait_cv; 
00049   int padding2[8]; /* Padding bytes to avoid false sharing and cache aliasing */
00050 } vmd_barrier_t;
00051 
00052 #endif
00053 
00054 
00055 
00056 #ifdef _MSC_VER
00057 #include <windows.h>
00058 typedef HANDLE vmd_thread_t;
00059 typedef CRITICAL_SECTION vmd_mutex_t;
00060 
00061 #if 0 && (NTDDI_VERSION >= NTDDI_WS08 || _WIN32_WINNT > 0x0600) 
00062 /* Use native condition variables only with Windows Server 2008 and newer... */
00063 #define VMDUSEWIN2008CONDVARS 1
00064 typedef  CONDITION_VARIABLE vmd_cond_t;
00065 #else
00066 /* Every version of Windows prior to Vista/WS2008 must emulate */
00067 /* variables using manually resettable events or other schemes */ 
00068 
00069 /* For higher performance, use interlocked memory operations   */
00070 /* rather than locking/unlocking mutexes when manipulating     */
00071 /* internal state.                                             */
00072 #if 1
00073 #define VMDUSEINTERLOCKEDATOMICOPS 1
00074 #endif 
00075 #define VMD_COND_SIGNAL    0
00076 #define VMD_COND_BROADCAST 1
00077 typedef struct {
00078   LONG waiters;     
00080   CRITICAL_SECTION waiters_lock;
00081   HANDLE events[2]; 
00082 } vmd_cond_t;
00083 #endif
00084 
00085 typedef HANDLE vmd_barrier_t; 
00086 #endif
00087 #endif
00088 
00089 
00090 #ifndef VMDTHREADS
00091 typedef int vmd_thread_t;
00092 typedef int vmd_mutex_t;
00093 typedef int vmd_cond_t;
00094 typedef int vmd_barrier_t;
00095 #endif
00096 
00097 #ifdef __cplusplus
00098 extern "C" {
00099 #endif
00100 
00101 /*
00102  * Mutexes
00103  */
00104 
00105 /* initialize a mutex */
00106 int vmd_mutex_init(vmd_mutex_t *);
00107 
00108 /* lock a mutex */
00109 int vmd_mutex_lock(vmd_mutex_t *);
00110 
00111 /* unlock a mutex */
00112 int vmd_mutex_unlock(vmd_mutex_t *);
00113 
00114 /* destroy a mutex */
00115 int vmd_mutex_destroy(vmd_mutex_t *);
00116 
00117 
00118 /*
00119  * Condition variables
00120  */
00121 /* initialize a condition variable */
00122 int vmd_cond_init(vmd_cond_t *);
00123 
00124 /* destroy a condition variable */
00125 int vmd_cond_destroy(vmd_cond_t *);
00126 
00127 /* wait on a condition variable */
00128 int vmd_cond_wait(vmd_cond_t *, vmd_mutex_t *);
00129 
00130 /* signal a condition variable, waking at least one thread */
00131 int vmd_cond_signal(vmd_cond_t *);
00132 
00133 /* signal a condition variable, waking all threads */
00134 int vmd_cond_broadcast(vmd_cond_t *);
00135 
00136 #endif
00137 
00138 /* 
00139  * When rendering in the CAVE we use a special synchronization
00140  * mode so that shared memory mutexes and condition variables 
00141  * will work correctly when accessed from multiple processes.
00142  * Inter-process synchronization involves the kernel to a greater
00143  * degree, so these barriers are substantially more costly to use 
00144  * than the ones designed for use within a single-process.
00145  */
00146 int vmd_thread_barrier_init_proc_shared(vmd_barrier_t *, int n_clients);
00147 
00148 /* destroy a thread barrier */
00149 void vmd_thread_barrier_destroy(vmd_barrier_t *barrier);
00150 
00151 /* 
00152  * Synchronize on a thread barrier, returning the sum of all 
00153  * of the "increment" parameters from participating threads 
00154  */
00155 int vmd_thread_barrier(vmd_barrier_t *barrier, int increment);
00156 
00157 #ifdef __cplusplus
00158 }
00159 #endif

Generated on Sat May 26 01:48:36 2012 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002