Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

communShMem.cpp

00001 /*
00002  *                         Sun Public License Notice
00003  *
00004  * The contents of this file are subject to the Sun Public License
00005  * Version 1.0 (the "License"); you may not use this file except
00006  * in compliance with the License. A copy of the License is available
00007  * at http://www.sun.com/
00008  *
00009  * The Original Code is the Java Profiler module.  The Initial Developers
00010  * of the Original Code are Jan Stola, Pavel Vacha, Michal Pise, Petr Luner,
00011  * Lukas Petru and Marek Przeczek.
00012  *
00013  * Portions created by Jan Stola are Copyright (C) 2000-2001.
00014  * All Rights Reserved.
00015  *
00016  * Portions created by Pavel Vacha are Copyright (C) 2000-2001.
00017  * All Rights Reserved.
00018  *
00019  * Portions created by Michal Pise are Copyright (C) 2000-2001.
00020  * All Rights Reserved.
00021  *
00022  * Portions created by Petr Luner are Copyright (C) 2000-2001.
00023  * All Rights Reserved.
00024  *
00025  * Portions created by Lukas Petru are Copyright (C) 2000-2001.
00026  * All Rights Reserved.
00027  *
00028  * Portions created by Marek Przeczek are Copyright (C) 2000-2001.
00029  * All Rights Reserved.
00030  *
00031  * Contributors: Jan Stola, Pavel Vacha, Michal Pise, Petr Luner,
00032  *               Lukas Petru and Marek Przeczek.
00033  */
00034 
00035 #include "../commun3/communShMem.h"
00036 
00037 CommunShMem::CommunShMem( const String& shmemId, int size) :
00038 
00039         _sem1(  String( "1") + shmemId + "XXXXXXXXXX"),
00040         _sem2(  String( "2") + shmemId + "XXXXXXXXXX"),
00041 
00042         _shmem( String( "3") + shmemId + "XXXXXXXXXX", size) {
00043 }
00044 
00045 CommunShMem::~CommunShMem() {
00046 
00047         *(jint*)(_shmem.getAddress()) = -1;
00048 
00049         _sem1.reset();
00050         _sem1.release();
00051 
00052         _sem2.reset();
00053         _sem2.release();
00054 }
00055 
00056 Commun& CommunShMem::operator>>( Buffer& b) {
00057 
00058         void* p = _shmem.getAddress();
00059         int shmemSize = _shmem.getSize();
00060 
00061         jint size  = *(jint*)p;
00062 
00063         if( size < 0 || !checkClient()) _failed = 1;
00064         else if( size) {
00065 
00066                 jint num = size/shmemSize;
00067 
00068                 char* buf = new char[size];
00069                 char* q = buf;
00070 
00071                 for( jint i = 0; i < num; i++, q += shmemSize) {
00072 
00073                         _sem2.release();
00074                         _sem1.wait();
00075 
00076                         _failed = !checkClient();
00077                         if( _failed) break;
00078 
00079                         memcpy( q, p, shmemSize);
00080                 }
00081 
00082                 _failed = !checkClient();
00083 
00084                 jint rest = size%shmemSize;
00085                 if( rest && !_failed) {
00086 
00087                         _sem2.release();
00088                         _sem1.wait();
00089 
00090                         _failed = !checkClient();
00091                         if( !_failed) memcpy( q, p, rest);
00092                 }
00093 
00094                 if( !_failed) b = Buffer( buf, size);
00095                 delete[] buf;
00096         }
00097         else b.clear();
00098 
00099         return *this;
00100 }
00101 
00102 Commun& CommunShMem::operator<<( const Buffer& b) {
00103 
00104         void* p = _shmem.getAddress();
00105         int shmemSize = _shmem.getSize();
00106 
00107         if( *(jint*)p < 0 || !checkClient()) _failed = 1;
00108         else {
00109 
00110                 jint size = b.getSize();
00111                 *(jint*)p = size;
00112 
00113                 if( size) {
00114 
00115                         jint num = size/shmemSize;
00116                         const char* q = b.getBuffer();
00117 
00118                         for( int i = 0; i < num; i++, q += shmemSize) {
00119 
00120                                 _sem2.release();
00121                                 _sem1.wait();
00122 
00123                                 _failed = !checkClient();
00124                                 if( _failed) break;
00125                 
00126                                 memcpy( p, q, shmemSize);
00127                         }
00128 
00129                         _failed = !checkClient();
00130 
00131                         jint rest = size%shmemSize;
00132                         if( rest && !_failed) {
00133 
00134                                 _sem2.release();
00135                                 _sem1.wait();
00136 
00137                                 _failed = !checkClient();
00138                                 if( !_failed) memcpy( p, q, rest);
00139                         }
00140                 }
00141         }
00142 
00143         _sem2.release();
00144 
00145         return *this;
00146 }
00147 
00148 int CommunShMem::isReady() {
00149 
00150         _sem1.wait();
00151 
00152         return 1;
00153 }
00154 
00155 int CommunShMem::initialize() {
00156 
00157         _sem1.reset();
00158         _sem1.wait();
00159 
00160         _failed = ( *(jint*)_shmem.getAddress() < 0);
00161 
00162 #ifdef WIN32
00163         DWORD* p = (DWORD*)((jint*)(_shmem.getAddress())+1);
00164 
00165         _pid = *p;
00166         *p = GetCurrentProcessId();
00167 #else
00168         pid_t* p = (pid_t*)((jint*)(_shmem.getAddress())+1);
00169 
00170         _pid = *p;
00171         *p = getpid();
00172 #endif
00173 
00174         _sem2.release();
00175 
00176         return !_failed;
00177 }
00178 
00179 int CommunShMem::checkClient() {
00180 
00181 #ifdef WIN32
00182         return GetProcessVersion( _pid);
00183 #else
00184         return !kill( _pid, 0);
00185 #endif
00186 }

Generated on Mon Jan 28 14:53:26 2002 for Java Profiler Dynamic Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001