Update of /cvsroot/javaprofiler/library/src/commun3
In directory usw-pr-cvs1:/tmp/cvs-serv7456/src/commun3
Added Files:
Makefile Makefile.mak Makefile.rules communShMem.cpp
communShMem.h dir.info semaphore.h sharedMemory.h
Log Message:
no message
--- NEW FILE: Makefile ---
include ../../config.mk
include Makefile.rules
--- NEW FILE: Makefile.mak ---
!include ../../config.mk
!include Makefile.rules
--- NEW FILE: Makefile.rules ---
communShMem.o \
communShMem.obj: communShMem.cpp ../main/includes.h
$(CCC) $(CPPFLAGS) communShMem.cpp
--- NEW FILE: communShMem.cpp ---
#include "../main/includes.h"
Commun& CommunShMem::operator>>( Buffer& b) {
_sem1.wait();
void* p = _shmem.getAddress();
jint size = ntohl( *(jint*)p);
if( size) {
jint num = size/_shmemSize;
char* buf = new char[size];
char* q = buf;
for( jint i = 0; i < num; i++, q += _shmemSize) {
_sem2.release();
_sem1.wait();
memcpy( q, p, _shmemSize);
}
jint rest = size%_shmemSize;
if( rest) {
_sem2.release();
_sem1.wait();
memcpy( q, p, rest);
}
b = Buffer( buf, size);
delete[] buf;
}
else b.clear();
return *this;
}
Commun& CommunShMem::operator<<( const Buffer& b) {
void* p = _shmem.getAddress();
jint size = b.getSize();
*(jint*)p = htonl( size);
if( size) {
_sem2.release();
_sem1.wait();
jint num = size/_shmemSize;
const char* q = b.getBuffer();
for( int i = 0; i < num; i++, q += _shmemSize) {
memcpy( p, q, _shmemSize);
_sem2.release();
_sem1.wait();
}
jint rest = size%_shmemSize;
if( rest) {
memcpy( p, q, rest);
_sem2.release();
_sem1.wait();
}
}
else _sem2.release();
return *this;
}
--- NEW FILE: communShMem.h ---
#ifndef _COMMUN_SH_MEM_H_
#define _COMMUN_SH_MEM_H_
class CommunShMem: public Commun {
Semaphore _sem1;
Semaphore _sem2;
SharedMemory _shmem;
int _shmemSize;
public:
// maximum 3 znaky
CommunShMem( const String& shmemId, int size) :
_sem1( String( "1") + shmemId + "XXXXXXXXXX"),
_sem2( String( "2") + shmemId + "XXXXXXXXXX"),
_shmem( String( "3") + shmemId + "XXXXXXXXXX"),
_shmemSize( size)
{};
virtual Commun& operator>>( Buffer& b);
virtual Commun& operator<<( const Buffer& b);
};
#endif // _COMMUN_SH_MEM_H_
--- NEW FILE: dir.info ---
FILES = communShMem
CLEAN_FILES = *.pdb *.obj *.o
--- NEW FILE: semaphore.h ---
#ifndef _SEMAPHORE_H_
#define _SEMAPHORE_H_
/** Binary semaphore. This class implements IPC semaphore.
** Implementation of this class is platform/system dependent.
**
** @author Marek Przeczek */
class Semaphore {
#ifdef WIN32
/// win32 semaphore identifier
HANDLE _semid;
#else
#error FIXME - not implemented yet !!!
#endif
/// is semaphore locked or not ?
int _locked;
public:
/** Constructor. Opens a semaphore with given name.
** 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 !!!
#endif
}
/// Destructor.
~Semaphore() {
if( _locked) release();
#ifdef WIN32
CloseHandle( _semid);
#else
#error FIXME - not implemented yet !!!
#endif
}
/** Lock semaphore. If the semaphore is already locked,
** calling thread waits on semaphore until another process
** (or thread) will release it.
**
** @see release() */
void wait() {
#ifdef WIN32
WaitForSingleObject( _semid, INFINITE);
#else
#error FIXME - not implemented yet !!!
#endif
_locked = 1;
}
/** Unlock semaphore. This method releases semaphore
** locked by previous call to wait() method.
**
** @see wait() */
void release() {
_locked = 0;
#ifdef WIN32
ReleaseSemaphore( _semid, 1, NULL);
#else
#error FIXME - not implemented yet !!!
#endif
}
};
#endif // _SEMAPHORE_H_
--- NEW FILE: sharedMemory.h ---
#ifndef _SHARED_MEMORY_H_
#define _SHARED_MEMORY_H_
/** Shared memory. This class manages IPC shared memory.
** Implementation of this class is platform/system dependent.
**
** @author Marek Przeczek */
class SharedMemory {
#ifdef WIN32
/// win32 shared memory identifier
HANDLE _shmid;
/// win32 shared memory address
LPVOID _address;
#else
#error FIXME - not implemented yet !!!
#endif
public:
/** Constructor. It attaches shared memory associated
** with given name. If shared memory doesn't exist yet,
** new shared block of memory is created and associated
** with given name.
**
** @param name name of shared block of memory
** @param size size of shared memory (in bytes) */
SharedMemory( const String& name, int 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
#error FIXME - not implemented yet !!!
#endif
}
/** Destructor. It deattaches attached shared memory
** from the address space of the process. If possible,
** block of shared memory is destroyed. */
~SharedMemory() {
#ifdef WIN32
UnmapViewOfFile( _address);
CloseHandle( _shmid);
#else
#error FIXME - not implemented yet !!!
#endif
}
/** Address of shared memory block. This method returns
** a pointer where shared memory begins - is mapped
** to process' address space.
**
** @return pointer to shared memory block */
void* getAddress() { return _address;}
};
#endif // _SHARED_MEMORY_H_
|