/*++ Copyright (c) 1989 Microsoft Corporation Module Name: os2sem.c Abstract: This is a test OS/2 application to test the Semaphore component of OS/2 Author: Steve Wood (stevewo) 22-Aug-1989 Environment: User Mode Only Revision History: --*/ #define OS2_API32 #define INCL_OS2V20_ERRORS #define INCL_OS2V20_MEMORY #define INCL_OS2V20_TASKING #define INCL_OS2V20_SEMAPHORES #include PPIB Pib; PNT_TIB NtTib; VOID TestMisc( VOID ); VOID TestProcess( VOID ); VOID TestHandles( VOID ); VOID TestEvent( VOID ); VOID TestMutex( VOID ); VOID TestMuxWait( VOID ); VOID ExitRoutine( ULONG ExitReason ); VOID TestThread( IN PCH ThreadName ); int main( int argc, char *argv[], char *envp[] ) { APIRET rc; DbgPrint( "*** Entering OS/2 Semaphore Test Application\n" ); rc = DosGetThreadInfo( &NtTib, &Pib ); rc = DosExitList( EXLST_ADD | 0x3000, ExitRoutine ); if (rc != NO_ERROR) { DbgPrint( "*** DosExitList(EXLST_ADD) failed - rc == %ld\n", rc ); } TestMisc(); TestProcess(); #if 1 DbgPrint( "*** Entering OS/2 Handle Semaphore Test\n" ); TestHandles(); DbgPrint( "*** Exiting OS/2 Handle Semaphore Test\n" ); #endif DbgPrint( "*** Entering OS/2 Event Semaphore Test\n" ); TestEvent(); DbgPrint( "*** Exiting OS/2 Event Semaphore Test\n" ); DbgPrint( "*** Entering OS/2 Mutex Semaphore Test\n" ); TestMutex(); DbgPrint( "*** Exiting OS/2 Mutex Semaphore Test\n" ); DbgPrint( "*** Entering OS/2 MuxWait Semaphore Test\n" ); TestMuxWait(); DbgPrint( "*** Exiting OS/2 MuxWait Semaphore Test\n" ); DbgPrint( "*** Exiting OS/2 Semaphore Test Application\n" ); return( 0 ); } VOID ExitRoutine( ULONG ExitReason ) { DbgPrint( "*** ExitRoutine( %lX ) called\n", ExitReason ); DosExitList( EXLST_EXIT, NULL ); } VOID TestThread( IN PCH ThreadName ) { APIRET rc; PPIB Pib; PNT_TIB NtTib; DbgPrint( "*** Entering OS/2 Thread %s\n", ThreadName ); rc = DosGetThreadInfo( &NtTib, &Pib ); DbgPrint( "*** Leaveing OS/2 Thread %s\n", ThreadName ); } VOID CloneTest( PPID ChildPid ); BOOLEAN IsClonedTest( VOID ); VOID CloneTest( PPID ChildPid ) { PPIB Pib; PNT_TIB NtTib; APIRET rc; PCH src, Variables, ImageFileName, CommandLine; CHAR ErrorBuffer[ 32 ]; RESULTCODES ResultCodes; rc = DosGetThreadInfo( &NtTib, &Pib ); if (rc != NO_ERROR) { DbgPrint( "*** DosGetThreadInfo failed - rc == %ld\n", rc ); return; } src = Pib->Environment; Variables = src; while (*src) { while (*src) { src++; } src++; } src++; ImageFileName = src; CommandLine = "CLONETEST\000"; rc = DosExecPgm( ErrorBuffer, sizeof( ErrorBuffer ), ChildPid == NULL ? EXEC_SYNC : EXEC_ASYNC, CommandLine, Variables, &ResultCodes, ImageFileName ); if (rc != NO_ERROR) { DbgPrint( "*** DosExecPgm( %s, %s failed - rc == %ld\n", ImageFileName, CommandLine, rc ); } else { if (ChildPid != NULL) { *ChildPid = (PID)ResultCodes.ExitReason; } } } BOOLEAN IsClonedTest( VOID ) { PPIB Pib; PNT_TIB NtTib; APIRET rc; rc = DosGetThreadInfo( &NtTib, &Pib ); if (rc != NO_ERROR) { DbgPrint( "*** DosGetThreadInfo failed - rc == %ld\n", rc ); return( FALSE ); } if (!strcmp( Pib->CommandLine, "CLONETEST" )) { return( TRUE ); } else { return( FALSE ); } } #define SEM_SHIFT 16 #define MAX_SEM_REC 64 VOID TestMisc( VOID ) { APIRET rc; SEMRECORD EventHandles[ 64 ]; SEMRECORD MutexHandles[ 64 ]; SEMRECORD srMtxPrivate[ 64 ]; SEMRECORD srMtxShared[ 64 ]; HMUX MuxWaitHandle; ULONG i, PostCount, UserValue; for (i=0; i<64; i++) { rc = DosCreateMutexSem( NULL, (PHMTX)&MutexHandles[ i ].hsemCur, NULL, TRUE ); MutexHandles[ i ].ulUser = 0; if (rc != NO_ERROR) { DbgPrint( "*** DosCreateMutexSem( %d ) failed - rc == %d\n", i, rc ); return; } else { DbgPrint( "MutexSem[ %d ] == %X\n", i, MutexHandles[ i ].hsemCur ); } } DbgBreakPoint(); rc = DosCreateMuxWaitSem( NULL, &MuxWaitHandle, 64, (PSEMRECORD)MutexHandles, DCMW_WAIT_ALL ); if (rc != NO_ERROR) { DbgPrint( "*** DosCreateMuxWaitSem failed - rc == %d\n", rc ); } else { rc = DosWaitMuxWaitSem( MuxWaitHandle, SEM_INDEFINITE_WAIT, &UserValue ); if (rc != NO_ERROR) { DbgPrint( "*** DosWaitMuxWaitSem failed - rc == %d\n", rc ); } rc = DosCloseMuxWaitSem( MuxWaitHandle ); if (rc != NO_ERROR) { DbgPrint( "*** DosCloseMuxWaitSem failed - rc == %d\n", rc ); } } for (i=0; i<64; i++) { rc = DosReleaseMutexSem( (HMTX)MutexHandles[ i ].hsemCur ); if (rc != NO_ERROR) { DbgPrint( "*** DosReleaseMutexSem( %d ) failed - rc == %d\n", i, rc ); } rc = DosCloseMutexSem( (HMTX)MutexHandles[ i ].hsemCur ); if (rc != NO_ERROR) { DbgPrint( "*** DosCloseMutexSem( %d ) failed - rc == %d\n", i, rc ); } } for (i = 0; i < MAX_SEM_REC + 1; i++) { rc=DosCreateMutexSem (NULL, &srMtxPrivate[i].hsemCur, 0, TRUE); DbgPrint("\n rc from DoCrMutx %u", rc); srMtxPrivate[i].ulUser = ((ULONG)srMtxPrivate[i].hsemCur << SEM_SHIFT) + i; } DbgPrint("\n TESTING MtxPrivate Wait all\n"); rc = DosCreateMuxWaitSem (NULL, &MuxWaitHandle, 32,(PSEMRECORD)srMtxPrivate, DCMW_WAIT_ALL); DbgPrint("\n rc from DoCrMuxWait %u", rc); rc = DosWaitMuxWaitSem (MuxWaitHandle, 10000, &UserValue); /* rc coming here is 6 */ DbgPrint("\n rc from DoWtMutx %u\n", rc); DbgPrint("\n TESTING MtxPrivate Wait any\n"); rc = DosCreateMuxWaitSem (NULL, &MuxWaitHandle, 32, (PSEMRECORD)srMtxPrivate, DCMW_WAIT_ANY); DbgPrint("\n rc from DoCrMuxWait %u", rc); rc = DosWaitMuxWaitSem (MuxWaitHandle, 10000, &UserValue); /* rc coming here is 6 */ DbgPrint("\n rc from DoWtMutx %u\n", rc); /* Testing for mutex shared sem */ for (i = 0; i < MAX_SEM_REC + 1; i++) { rc=DosCreateMutexSem (NULL, &srMtxShared[i].hsemCur, DC_SEM_SHARED, TRUE); DbgPrint("\n rc from DoCrMutx %u", rc); srMtxShared[i].ulUser = ((ULONG)srMtxShared[i].hsemCur << SEM_SHIFT) + i; } DbgPrint("\n TESTING MtxShared Wait all\n"); rc = DosCreateMuxWaitSem (NULL, &MuxWaitHandle, 32, (PSEMRECORD)srMtxShared, DCMW_WAIT_ALL); DbgPrint("\n rc from DoCrMuxWait %u", rc); rc = DosWaitMuxWaitSem (MuxWaitHandle, 10000, &UserValue); /* rc coming here is 6 */ DbgPrint("\n rc from DoWtMutx %u\n", rc); DbgPrint("\n TESTING MtxShared Wait any\n"); rc = DosCreateMuxWaitSem (NULL, &MuxWaitHandle, 32, (PSEMRECORD)srMtxShared, DCMW_WAIT_ANY); DbgPrint("\n rc from DoCrMuxWait %u", rc); rc = DosWaitMuxWaitSem (MuxWaitHandle,10000, &UserValue);/* rc coming here is 6 */ DbgPrint("\n rc from DoWtMutx %u\n", rc); return; } VOID TestProcess( VOID ) { APIRET rc; PID Pid; HEV EventHandle1, EventHandle2, EventHandle3, EventHandles[ 4 ]; ULONG i, PostCount; RESULTCODES ResultCodes; if (IsClonedTest()) { EventHandle1 = NULL; rc = DosOpenEventSem( "\\SEM32\\sem1", &EventHandle1 ); if (rc != NO_ERROR) { DbgPrint( "*** DosOpenEventSem( \\SEM32\\sem1 ) failed - rc == %ld\n", rc ); } else { DosPostEventSem( EventHandle1 ); DosCloseEventSem( EventHandle1 ); } EventHandle2 = NULL; rc = DosOpenEventSem( "\\SEM32\\sem2", &EventHandle2 ); if (rc != NO_ERROR) { DbgPrint( "*** DosOpenEventSem( \\SEM32\\sem2 ) failed - rc == %ld\n", rc ); } else { DosPostEventSem( EventHandle2 ); DosCloseEventSem( EventHandle2 ); } DosExit( EXIT_PROCESS, 0 ); } else { for (i=0; i<4; i++) { rc = DosCreateEventSem( NULL, &EventHandles[ i ], DC_SEM_SHARED, FALSE ); if (rc != NO_ERROR) { DbgPrint( "*** DosCreateEventSem( shared ) failed - rc == %ld\n", rc ); } } for (i=0; i<4; i++) { DosCloseEventSem( EventHandles[ i ] ); } rc = DosCreateEventSem( "\\SEM32\\sem1", &EventHandle1, 0, FALSE ); if (rc != NO_ERROR) { DbgPrint( "*** DosCreateEventSem( \\SEM32\\sem1 ) failed - rc == %ld\n", rc ); } rc = DosCreateEventSem( "\\SEM32\\sem2", &EventHandle2, 0, FALSE ); if (rc != NO_ERROR) { DbgPrint( "*** DosCreateEventSem( \\SEM32\\sem2 ) failed - rc == %ld\n", rc ); } DosResetEventSem( EventHandle1, &PostCount ); CloneTest( &Pid ); DosWaitEventSem( EventHandle1, SEM_INDEFINITE_WAIT ); DosCloseEventSem( EventHandle1 ); DosWaitEventSem( EventHandle2, SEM_INDEFINITE_WAIT ); DosCloseEventSem( EventHandle2 ); DosWaitChild( DCWA_PROCESS, DCWW_WAIT, &ResultCodes, &Pid, Pid ); rc = DosCreateEventSem( "\\SEM32\\sem3", &EventHandle3, 0, FALSE ); if (rc != NO_ERROR) { DbgPrint( "*** DosCreateEventSem( \\SEM32\\sem3 ) failed - rc == %ld\n", rc ); } rc = DosCreateEventSem( "\\SEM32\\sem4", (PHEV)0, 0, FALSE ); if (rc != ERROR_INVALID_ADDRESS) { DbgPrint( "*** DosCreateEventSem( \\SEM32\\sem4, (hEv == NULL) ) failed - rc == %ld\n", rc ); } DbgBreakPoint(); rc = DosCreateEventSem( "\\SEM32\\sem4", (PHEV)0xFFFF0000, 0, FALSE ); if (rc != ERROR_INVALID_ADDRESS) { DbgPrint( "*** DosCreateEventSem( \\SEM32\\sem4, (hEv == 0xFFFF0000) ) failed - rc == %ld\n", rc ); } } } VOID TestHandles( VOID ) { PVOID HandleBuffer; PHEV EventHandles; ULONG MaxCountHandles, i, j; APIRET rc; HandleBuffer = NULL; MaxCountHandles = (64*1024) / sizeof( HEV ); rc = DosAllocMem( &HandleBuffer, MaxCountHandles * sizeof( HEV ), PAG_COMMIT | PAG_READ | PAG_WRITE ); if (rc != NO_ERROR) { DbgPrint( "*** DosAllocMem failed - rc == %ld\n", rc ); return; } EventHandles = (PHEV)HandleBuffer; for (i=0; i