Update of /cvsroot/javaprofiler/library/src/commun3 In directory usw-pr-cvs1:/tmp/cvs-serv12170/src/commun3 Modified Files: Makefile.rules communShMem.cpp communShMem.h dir.info semaphore.h sharedMemory.h Added Files: semaphore.cpp sharedMemory.cpp Log Message: some parts completely rewritten; changes in communication interface to make it faster; ported to linux --- NEW FILE: semaphore.cpp --- /* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"); you may not use this file except * in compliance with the License. A copy of the License is available * at http://www.sun.com/ * * The Original Code is the Java Profiler module. The Initial Developers * of the Original Code are Jan Stola, Pavel Vacha, Michal Pise, Petr Luner, * Lukas Petru and Marek Przeczek. * * Portions created by Jan Stola are Copyright (C) 2000-2001. * All Rights Reserved. * * Portions created by Pavel Vacha are Copyright (C) 2000-2001. * All Rights Reserved. * * Portions created by Michal Pise are Copyright (C) 2000-2001. * All Rights Reserved. * * Portions created by Petr Luner are Copyright (C) 2000-2001. * All Rights Reserved. * * Portions created by Lukas Petru are Copyright (C) 2000-2001. * All Rights Reserved. * * Portions created by Marek Przeczek are Copyright (C) 2000-2001. * All Rights Reserved. * * Contributors: Jan Stola, Pavel Vacha, Michal Pise, Petr Luner, * Lukas Petru and Marek Przeczek. */ #include "../commun3/semaphore.h" Semaphore::Semaphore( const String& name, int locked) { #ifdef WIN32 _semid = CreateSemaphore( NULL, (( locked) ? 0 : 1), 1, name); #else _semid = semget( *(key_t*)(const char*)name, 1, 0666 | IPC_CREAT); _initLocked = locked; #endif } Semaphore::~Semaphore() { #ifdef WIN32 CloseHandle( _semid); #else #endif } void Semaphore::wait() { #ifdef WIN32 WaitForSingleObject( _semid, INFINITE); Sleep( 0); #else if( _initLocked) { static sembuf sop = { 0, -1, SEM_UNDO}; semop( _semid, &sop, 1); } else { static sembuf sop[2] = { 0, 0, 0, 0, 1, SEM_UNDO}; semop( _semid, sop, 2); } #endif } void Semaphore::release() { #ifdef WIN32 ReleaseSemaphore( _semid, 1, NULL); //Sleep( 0); #else if( _initLocked) { static sembuf sop = { 0, 1, SEM_UNDO}; semop( _semid, &sop, 1); } else { static sembuf sop = { 0, -1, SEM_UNDO | IPC_NOWAIT}; semop( _semid, &sop, 1); } #endif } int Semaphore::waitNoBlock() { #ifdef WIN32 unsigned long rc = WaitForSingleObject( _semid, 0); Sleep( 0); if( rc == WAIT_FAILED) return -1; if( rc == WAIT_TIMEOUT) return 0; #else if( _initLocked) { static sembuf sop = { 0, -1, SEM_UNDO | IPC_NOWAIT}; if( semop( _semid, &sop, 1)) return 0; } else { static sembuf sop[2] = { 0, 0, IPC_NOWAIT, 0, 1, SEM_UNDO}; if( semop( _semid, sop, 2)) return 0; } #endif return 1; } --- NEW FILE: sharedMemory.cpp --- /* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"); you may not use this file except * in compliance with the License. A copy of the License is available * at http://www.sun.com/ * * The Original Code is the Java Profiler module. The Initial Developers * of the Original Code are Jan Stola, Pavel Vacha, Michal Pise, Petr Luner, * Lukas Petru and Marek Przeczek. * * Portions created by Jan Stola are Copyright (C) 2000-2001. * All Rights Reserved. * * Portions created by Pavel Vacha are Copyright (C) 2000-2001. * All Rights Reserved. * * Portions created by Michal Pise are Copyright (C) 2000-2001. * All Rights Reserved. * * Portions created by Petr Luner are Copyright (C) 2000-2001. * All Rights Reserved. * * Portions created by Lukas Petru are Copyright (C) 2000-2001. * All Rights Reserved. * * Portions created by Marek Przeczek are Copyright (C) 2000-2001. * All Rights Reserved. * * Contributors: Jan Stola, Pavel Vacha, Michal Pise, Petr Luner, * Lukas Petru and Marek Przeczek. */ #include "../commun3/sharedMemory.h" #include "../setup/setup.h" SharedMemory::SharedMemory( const String& name, int size) : _size( size) { #ifdef WIN32 _shmid = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 0, _size, name); _address = MapViewOfFile( _shmid, FILE_MAP_ALL_ACCESS, 0, 0, 0); #else _size = Setup::COMMUN_SHMEM_SIZE; _shmid = shmget( *(key_t*)(const char*)name, _size, 0666 | IPC_CREAT); _address = shmat( _shmid, NULL, 0666); #endif } SharedMemory::~SharedMemory() { #ifdef WIN32 UnmapViewOfFile( _address); CloseHandle( _shmid); #else shmdt( _address); #endif } Index: Makefile.rules =================================================================== RCS file: /cvsroot/javaprofiler/library/src/commun3/Makefile.rules,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -r1.1 -r1.2 *** Makefile.rules 2001/08/09 23:22:55 1.1 --- Makefile.rules 2001/11/21 22:31:42 1.2 *************** *** 1,3 **** communShMem.o \ ! communShMem.obj: communShMem.cpp ../main/includes.h $(CCC) $(CPPFLAGS) communShMem.cpp --- 1,11 ---- communShMem.o \ ! communShMem.obj: communShMem.cpp $(CCC) $(CPPFLAGS) communShMem.cpp + + semaphore.o \ + semaphore.obj: semaphore.cpp + $(CCC) $(CPPFLAGS) semaphore.cpp + + sharedMemory.o \ + sharedMemory.obj: sharedMemory.cpp + $(CCC) $(CPPFLAGS) sharedMemory.cpp Index: communShMem.cpp =================================================================== RCS file: /cvsroot/javaprofiler/library/src/commun3/communShMem.cpp,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -r1.5 -r1.6 *** communShMem.cpp 2001/09/02 20:14:21 1.5 --- communShMem.cpp 2001/11/21 22:31:42 1.6 *************** *** 33,37 **** */ ! #include "../main/includes.h" Commun& CommunShMem::operator>>( Buffer& b) { --- 33,37 ---- */ ! #include "../commun3/communShMem.h" Commun& CommunShMem::operator>>( Buffer& b) { *************** *** 42,46 **** _isem.wait(); ! jint size = ntohl( *(jint*)p); if( size < 0) { --- 42,46 ---- _isem.wait(); ! jint size = (signed)ntohl( *(jint*)p); if( size < 0) { *************** *** 95,99 **** _isem.wait(); ! if( ntohl( *(jint*)p) < 0) { _failed = 1; --- 95,99 ---- _isem.wait(); ! if( (signed)ntohl( *(jint*)p) < 0) { _failed = 1; *************** *** 142,146 **** _isem.wait(); ! _failed = ( ntohl( *(jint*)_shmem.getAddress()) < 0); _isem.release(); --- 142,146 ---- _isem.wait(); ! _failed = ( (signed)ntohl( *(jint*)(_shmem.getAddress())) < 0); _isem.release(); *************** *** 156,160 **** _isem.wait(); ! *(jint*)_shmem.getAddress() = htonl( 0); _failed = 0; _isem.release(); --- 156,160 ---- _isem.wait(); ! *(jint*)(_shmem.getAddress()) = htonl( 0); _failed = 0; _isem.release(); *************** *** 164,166 **** --- 164,196 ---- return 1; + } + + CommunShMem::CommunShMem( const String& shmemId, int size) : + + _sem1( String( "1") + shmemId + "XXXXXXXXXX"), + _sem2( String( "2") + shmemId + "XXXXXXXXXX"), + _isem( String( "3") + shmemId + "XXXXXXXXXX", 1), + _shmem( String( "4") + shmemId + "XXXXXXXXXX", size) { + + _sem1.release(); + _sem1.wait(); + + _sem2.release(); + _sem2.wait(); + + *(jint*)(_shmem.getAddress()) = htonl( 0); + + _isem.release(); + }; + + CommunShMem::~CommunShMem() { + + _isem.wait(); + + _sem2.release(); + _sem1.release(); + + *(jint*)(_shmem.getAddress()) = htonl( (unsigned)(-1)); + + _isem.release(); } Index: communShMem.h =================================================================== RCS file: /cvsroot/javaprofiler/library/src/commun3/communShMem.h,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -r1.4 -r1.5 *** communShMem.h 2001/09/02 20:14:21 1.4 --- communShMem.h 2001/11/21 22:31:42 1.5 *************** *** 36,39 **** --- 36,46 ---- #define _COMMUN_SH_MEM_H_ + #include "../main/includes.h" + #include "../commun/commun.h" + #include "../commun3/semaphore.h" + #include "../commun3/sharedMemory.h" + #include "../string/string.h" + #include "../commun/buffer.h" + /** Shared memory communication. This class implements shared memory ** communication and offers the user of this class the standard *************** *** 48,53 **** class CommunShMem: public Commun { - private: - /// read-write semaphore Semaphore _sem1; --- 55,58 ---- *************** *** 69,104 **** ** ** @param shmemId shared memory identifier (3 characters long only) ! ** @param size shared memory size */ ! ! CommunShMem( const String& shmemId, int size) : ! ! _sem1( String( "1") + shmemId + "XXXXXXXXXX"), ! _sem2( String( "2") + shmemId + "XXXXXXXXXX"), ! _isem( String( "3") + shmemId + "XXXXXXXXXX", 1), ! _shmem( String( "4") + shmemId + "XXXXXXXXXX", size) { ! ! _sem1.release(); ! _sem1.wait(); ! _sem2.release(); ! _sem2.wait(); - *(jint*)_shmem.getAddress() = htonl( 0); - - _isem.release(); - }; - /// Destructor. ! virtual ~CommunShMem() { ! ! _isem.wait(); ! ! _sem2.release(); ! _sem1.release(); ! ! *(jint*)_shmem.getAddress() = htonl( -1); ! ! _isem.release(); ! } /** Initialization of communication. This method only waits --- 74,83 ---- ** ** @param shmemId shared memory identifier (3 characters long only) ! ** @param size shared memory size */ ! CommunShMem( const String& shmemId, int size); /// Destructor. ! virtual ~CommunShMem(); /** Initialization of communication. This method only waits Index: dir.info =================================================================== RCS file: /cvsroot/javaprofiler/library/src/commun3/dir.info,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -r1.1 -r1.2 *** dir.info 2001/08/09 23:22:55 1.1 --- dir.info 2001/11/21 22:31:42 1.2 *************** *** 1,3 **** ! FILES = communShMem ! CLEAN_FILES = *.pdb *.obj *.o --- 1,2 ---- ! FILES = communShMem semaphore sharedMemory CLEAN_FILES = *.pdb *.obj *.o Index: semaphore.h =================================================================== RCS file: /cvsroot/javaprofiler/library/src/commun3/semaphore.h,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -r1.5 -r1.6 *** semaphore.h 2001/09/02 20:14:21 1.5 --- semaphore.h 2001/11/21 22:31:42 1.6 *************** *** 36,39 **** --- 36,42 ---- #define _SEMAPHORE_H_ + #include "../main/includes.h" + #include "../string/string.h" + /** Binary semaphore. This class implements IPC semaphore. ** Implementation of this class is platform/system dependent. *************** *** 59,83 **** ** If semaphore doesn't exist, new one is created. ** ! ** @param name unique name (4 characters long only) ** @param locked semaphore should be locked after it has been initialized */ ! Semaphore( const String& name, int locked = 0) { - #ifdef WIN32 - _semid = CreateSemaphore( NULL, (( locked) ? 0 : 1), 1, name); - #else - _semid = semget( *(key_t*)(const char*)name, 1, 0666 | IPC_CREAT); - _initLocked = locked; - #endif - } - /// Destructor. ! ~Semaphore() { ! ! #ifdef WIN32 ! CloseHandle( _semid); ! #else ! #endif ! } /** Lock semaphore. If the semaphore is already locked, --- 62,72 ---- ** If semaphore doesn't exist, new one is created. ** ! ** @param name unique name (4 characters long only) ** @param locked semaphore should be locked after it has been initialized */ ! Semaphore( const String& name, int locked = 0); /// Destructor. ! ~Semaphore(); /** Lock semaphore. If the semaphore is already locked, *************** *** 86,108 **** ** ** @see release() */ - - void wait() { - - #ifdef WIN32 - WaitForSingleObject( _semid, INFINITE); - Sleep( 0); - #else - if( _initLocked) { ! static sembuf sop = { 0, -1, SEM_UNDO}; ! semop( _semid, &sop, 1); ! } ! else { ! ! static sembuf sop[2] = { 0, 0, 0, 0, 1, SEM_UNDO}; ! semop( _semid, sop, 2); ! } ! #endif ! } /** Unlock semaphore. This method releases semaphore --- 75,80 ---- ** ** @see release() */ ! void wait(); /** Unlock semaphore. This method releases semaphore *************** *** 111,133 **** ** @see wait() */ ! void release() { - #ifdef WIN32 - ReleaseSemaphore( _semid, 1, NULL); - // Sleep( 0); - #else - if( _initLocked) { - - static sembuf sop = { 0, 1, SEM_UNDO}; - semop( _semid, &sop, 1); - } - else { - - static sembuf sop = { 0, -1, SEM_UNDO | IPC_NOWAIT}; - semop( _semid, &sop, 1); - } - #endif - } - /** Lock semaphore but don't block. This method locks semaphore ** if it is possible, if not, it returns immediatelly without locking. --- 83,88 ---- ** @see wait() */ ! void release(); /** Lock semaphore but don't block. This method locks semaphore ** if it is possible, if not, it returns immediatelly without locking. *************** *** 136,164 **** ** 1 (okay, successfully locked); ** -1 (failure) */ - - int waitNoBlock() { ! #ifdef WIN32 ! unsigned long rc = WaitForSingleObject( _semid, 0); ! Sleep( 0); ! ! if( rc == WAIT_FAILED) return -1; ! if( rc == WAIT_TIMEOUT) return 0; ! #else ! if( _initLocked) { ! ! static sembuf sop = { 0, -1, SEM_UNDO | IPC_NOWAIT}; ! if( semop( _semid, &sop, 1)) return 0; ! } ! else { ! ! static sembuf sop[2] = { 0, 0, IPC_NOWAIT, 0, 1, SEM_UNDO}; ! if( semop( _semid, sop, 2)) return 0; ! } ! #endif ! return 1; ! } }; #endif // _SEMAPHORE_H_ - --- 91,97 ---- ** 1 (okay, successfully locked); ** -1 (failure) */ ! int waitNoBlock(); }; #endif // _SEMAPHORE_H_ Index: sharedMemory.h =================================================================== RCS file: /cvsroot/javaprofiler/library/src/commun3/sharedMemory.h,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -r1.4 -r1.5 *** sharedMemory.h 2001/09/02 20:14:21 1.4 --- sharedMemory.h 2001/11/21 22:31:42 1.5 *************** *** 36,39 **** --- 36,42 ---- #define _SHARED_MEMORY_H_ + #include "../main/includes.h" + #include "../string/string.h" + /** Shared memory. This class manages IPC shared memory. ** Implementation of this class is platform/system dependent. *************** *** 69,85 **** ** @param name name of shared block of memory ** @param size size of shared memory (in bytes) */ - - SharedMemory( const String& name, int size) : _size( size) { ! #ifdef WIN32 ! _shmid = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, ! PAGE_READWRITE | SEC_COMMIT, 0, _size, name); ! _address = MapViewOfFile( _shmid, FILE_MAP_ALL_ACCESS, 0, 0, 0); ! #else ! _size = IProf::COMMUN_SHMEM_SIZE; ! _shmid = shmget( *(key_t*)(const char*)name, _size, 0666 | IPC_CREAT); ! _address = shmat( _shmid, NULL, 0666); ! #endif ! } /** Destructor. It deattaches attached shared memory --- 72,77 ---- ** @param name name of shared block of memory ** @param size size of shared memory (in bytes) */ ! SharedMemory( const String& name, int size); /** Destructor. It deattaches attached shared memory *************** *** 87,99 **** ** block of shared memory is destroyed. */ ! ~SharedMemory() { ! ! #ifdef WIN32 ! UnmapViewOfFile( _address); ! CloseHandle( _shmid); ! #else ! shmdt( _address); ! #endif ! } /** Address of shared memory block. This method returns --- 79,83 ---- ** block of shared memory is destroyed. */ ! ~SharedMemory(); /** Address of shared memory block. This method returns *************** *** 113,115 **** #endif // _SHARED_MEMORY_H_ - --- 97,98 ---- |