|
From: Marek P. <ma...@us...> - 2001-08-23 23:57:35
|
Update of /cvsroot/javaprofiler/library/src/commun3
In directory usw-pr-cvs1:/tmp/cvs-serv1062/src/commun3
Modified Files:
communShMem.cpp communShMem.h semaphore.h
Log Message:
shared memory implementation; java client and dynamic library can
now communicate thru sockets or shmem;
main README completely rewritten;
new classes - for shared memory, semaphores etc.;
java native class CommunShMem implementation
Index: communShMem.cpp
===================================================================
RCS file: /cvsroot/javaprofiler/library/src/commun3/communShMem.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** communShMem.cpp 2001/08/12 07:35:31 1.2
--- communShMem.cpp 2001/08/23 23:57:31 1.3
***************
*** 3,12 ****
Commun& CommunShMem::operator>>( Buffer& b) {
- _sem1.wait();
-
void* p = _shmem.getAddress();
int shmemSize = _shmem.getSize();
jint size = ntohl( *(jint*)p);
if( size) {
--- 3,21 ----
Commun& CommunShMem::operator>>( Buffer& b) {
void* p = _shmem.getAddress();
int shmemSize = _shmem.getSize();
+ _isem.wait();
+
jint size = ntohl( *(jint*)p);
+ if( size < 0) {
+
+ _failed = 1;
+ _isem.release();
+
+ return *this;
+ }
+
+ _isem.release();
if( size) {
***************
*** 34,37 ****
--- 43,48 ----
}
+ _sem2.release();
+
b = Buffer( buf, size);
***************
*** 48,51 ****
--- 59,74 ----
int shmemSize = _shmem.getSize();
+ _isem.wait();
+
+ if( ntohl( *(jint*)p) < 0) {
+
+ _failed = 1;
+ _isem.release();
+
+ return *this;
+ }
+
+ _isem.release();
+
jint size = b.getSize();
*(jint*)p = htonl( size);
***************
*** 80,82 ****
--- 103,132 ----
return *this;
+ }
+
+ int CommunShMem::isReady() {
+
+ _isem.wait();
+ _failed = ( *(jint*)_shmem.getAddress() < 0);
+ _isem.release();
+
+ if( _failed) return 0;
+
+ int rc = _sem1.waitNoBlock();
+ if( _failed = (rc < 0)) return 0;
+
+ return rc;
+ }
+
+ int CommunShMem::initialize() {
+
+ _isem.wait();
+ *(jint*)_shmem.getAddress() = htonl( 0);
+ _failed = 0;
+ _isem.release();
+
+ _sem1.wait();
+ _sem1.release();
+
+ return 1;
}
Index: communShMem.h
===================================================================
RCS file: /cvsroot/javaprofiler/library/src/commun3/communShMem.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** communShMem.h 2001/08/12 07:35:31 1.2
--- communShMem.h 2001/08/23 23:57:31 1.3
***************
*** 14,17 ****
--- 14,19 ----
class CommunShMem: public Commun {
+ private:
+
/// read-write semaphore
Semaphore _sem1;
***************
*** 20,23 ****
--- 22,28 ----
Semaphore _sem2;
+ /// init semaphore
+ Semaphore _isem;
+
/// shared memory
SharedMemory _shmem;
***************
*** 36,43 ****
_sem1( String( "1") + shmemId + "XXXXXXXXXX"),
_sem2( String( "2") + shmemId + "XXXXXXXXXX"),
! _shmem( String( "3") + shmemId + "XXXXXXXXXX", size)
! {};
/** Read data. This operator is used to read data
** from communication channel (shmem) to buffer.
--- 41,80 ----
_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
+ ** for first unlocking of the semaphore used during communication.
+ ** In this way it recognizes that communication has started.
+ **
+ ** @return 0 (failed);
+ ** 1 (ok, initialized) */
+
+ virtual int initialize();
+
/** Read data. This operator is used to read data
** from communication channel (shmem) to buffer.
***************
*** 61,64 ****
--- 98,110 ----
virtual Commun& operator<<( const Buffer& b);
+
+ /** Communication channel ready for reading. This method
+ ** returns TRUE, if there are data in communication channel
+ ** and should be read from it.
+ **
+ ** @return 0 (nothing);
+ ** 1 (data in channel) */
+
+ virtual int isReady();
};
Index: semaphore.h
===================================================================
RCS file: /cvsroot/javaprofiler/library/src/commun3/semaphore.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** semaphore.h 2001/08/09 23:22:55 1.1
--- semaphore.h 2001/08/23 23:57:31 1.2
***************
*** 16,22 ****
#endif
- /// is semaphore locked or not ?
- int _locked;
-
public:
--- 16,19 ----
***************
*** 24,33 ****
** If semaphore doesn't exist, new one is created.
**
! ** @param name unique name (at least 8 bytes) */
! Semaphore( const String& name) : _locked( 0) {
#ifdef WIN32
! _semid = CreateSemaphore( NULL, 0, 1, name);
#else
#error FIXME - not implemented yet !!!
--- 21,31 ----
** 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
#error FIXME - not implemented yet !!!
***************
*** 38,43 ****
~Semaphore() {
- if( _locked) release();
-
#ifdef WIN32
CloseHandle( _semid);
--- 36,39 ----
***************
*** 60,64 ****
#error FIXME - not implemented yet !!!
#endif
- _locked = 1;
}
--- 56,59 ----
***************
*** 70,75 ****
void release() {
- _locked = 0;
-
#ifdef WIN32
ReleaseSemaphore( _semid, 1, NULL);
--- 65,68 ----
***************
*** 77,80 ****
--- 70,94 ----
#error FIXME - not implemented yet !!!
#endif
+ }
+
+ /** Lock semaphore but don't block. This method locks semaphore
+ ** if it is possible, if not, it returns immediatelly without locking.
+ **
+ ** @return 0 (failed, already locked);
+ ** 1 (okay, successfully locked);
+ ** -1 (failure) */
+
+ int waitNoBlock() {
+
+ #ifdef WIN32
+ unsigned long rc = WaitForSingleObject( _semid, 0);
+
+ if( rc == WAIT_FAILED) return -1;
+ if( rc == WAIT_TIMEOUT) return 0;
+ #else
+ #error FIXME - not implemented yet !!!
+ #endif
+
+ return 1;
}
};
|