00001 00002 /* 00003 * THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE 00004 * CHECKED IT OUT USING THE COMMAND CHECKOUT. 00005 * 00006 * $Id: sema__mag_8c-source.html 2161 2006-05-19 16:55:03Z paulf $ 00007 * 00008 * Revision history: 00009 * $Log$ 00009 * Revision 1.1 2006/05/19 16:55:02 paulf 00009 * first inclusion 00009 * 00010 * Revision 1.1 2000/02/14 18:53:30 lucky 00011 * Initial revision 00012 * 00013 * 00014 */ 00015 00016 /************************************************************** 00017 * sema_ew.c Windows NT version * 00018 * * 00019 * This file contains system-dependent functions for * 00020 * handling semaphores and mutexes. * 00021 **************************************************************/ 00022 00023 #include <windows.h> 00024 #include <stdlib.h> 00025 #include <mw_functions.h> 00026 00027 HANDLE mutSem; /* Mutex semaphore handle */ 00028 HANDLE semahandle; /* Handle of event semaphore */ 00029 00030 00031 /************************* CreateMutex_ew ************************** 00032 * Set up mutex semaphore to arbitrate the use of some variable * 00033 * by different threads. Since the mutex is unnamed, it can be * 00034 * used by only one process. * 00035 *******************************************************************/ 00036 00037 void CreateMutex_ew( void ) 00038 { 00039 mutSem = CreateMutex( 0, FALSE, 0 ); 00040 if ( mutSem == 0 ) 00041 logit( "et", "Error creating the mutex semaphore.\n" ); 00042 return; 00043 } 00044 00045 00046 /************************** RequestMutex *************************** 00047 * Wait until the mutex is available. Then, grab it. * 00048 *******************************************************************/ 00049 00050 void RequestMutex( void ) 00051 { 00052 WaitForSingleObject( mutSem, INFINITE ); /* Wait forever */ 00053 return; 00054 } 00055 00056 00057 /************************ ReleaseMutex_ew ************************** 00058 * Release the muxtex semaphore * 00059 *******************************************************************/ 00060 00061 void ReleaseMutex_ew( void ) 00062 { 00063 if ( ReleaseMutex( mutSem ) == 0 ) 00064 logit( "et", "Error releasing the mutex semaphore.\n" ); 00065 return; 00066 } 00067 00068 00069 /*************************** CloseMutex **************************** 00070 * We are done with the mutex semaphore. * 00071 *******************************************************************/ 00072 00073 void CloseMutex( void ) 00074 { 00075 CloseHandle( mutSem ); 00076 return; 00077 } 00078 00079 00080 /********************** CreateSpecificMutex ************************ 00081 * Set up mutex semaphore to arbitrate the use of resources. * 00082 *******************************************************************/ 00083 00084 void CreateSpecificMutex( HANDLE* mp ) 00085 { 00086 *mp = CreateMutex( 0, FALSE, 0 ); 00087 if ( *mp == 0 ) 00088 logit( "et", "Error creating specific mutex semaphore.\n" ); 00089 return; 00090 } 00091 00092 00093 /********************** RequestSpecificMutex *********************** 00094 * Request the mutex semaphore * 00095 *******************************************************************/ 00096 00097 void RequestSpecificMutex( HANDLE* mp ) 00098 { 00099 WaitForSingleObject( *mp, INFINITE ); 00100 return; 00101 } 00102 00103 00104 /********************** ReleaseSpecificMutex *********************** 00105 * Release the mutex semaphore * 00106 *******************************************************************/ 00107 00108 void ReleaseSpecificMutex( HANDLE* mp ) 00109 { 00110 if ( ReleaseMutex( *mp ) == 0 ) 00111 logit( "et", "Error releasing specific mutex semaphore.\n" ); 00112 return; 00113 } 00114 00115 00116 /*********************** CloseSpecificMutex ************************ 00117 * We are done with the mutex semaphore. * 00118 *******************************************************************/ 00119 00120 void CloseSpecificMutex( HANDLE* mp ) 00121 { 00122 CloseHandle( *mp ); 00123 return; 00124 } 00125 00126 00127 /************************* CreateSemaphore_ew ********************** 00128 * Create a semaphore * 00129 * The semaphore is not posted when it is created. * 00130 *******************************************************************/ 00131 00132 void CreateSemaphore_ew( void ) 00133 { 00134 const LONG InitialCount = 0; /* Initially unset */ 00135 const LONG MaxCount = 1000000000; /* One billion */ 00136 00137 semahandle = CreateSemaphore( NULL, InitialCount, MaxCount, NULL ); 00138 if ( semahandle == NULL ) 00139 { 00140 logit( "et", "CreateSemaphore() error: %d Exiting.\n", 00141 GetLastError() ); 00142 exit( -1 ); 00143 } 00144 return; 00145 } 00146 00147 00148 /*************************** PostSemaphore ************************* 00149 * Post the semaphore. * 00150 * The semaphore counter is incremented. * 00151 *******************************************************************/ 00152 00153 void PostSemaphore( void ) 00154 { 00155 if ( ReleaseSemaphore( semahandle, 1, NULL ) == FALSE ) 00156 logit( "et", "ReleaseSemaphore() error: %d\n", GetLastError() ); 00157 return; 00158 } 00159 00160 00161 /************************** WaitSemPost ************************* 00162 * Wait for the semaphore to be signaled. * 00163 ****************************************************************/ 00164 00165 void WaitSemPost( void ) 00166 { 00167 if ( WaitForSingleObject( semahandle, INFINITE ) == WAIT_FAILED ) 00168 logit( "et", "WaitForSingleObject() error: %d\n", GetLastError() ); 00169 return; 00170 } 00171 00172 00173 /************************* DestroySemaphore ********************* 00174 * Kill the semaphore. * 00175 ****************************************************************/ 00176 00177 void DestroySemaphore( void ) 00178 { 00179 CloseHandle( semahandle ); 00180 return; 00181 } 00182