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

semaphore.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/semaphore.h"
00036 
00037 Semaphore::Semaphore( const String& name) {
00038 
00039 #ifdef WIN32
00040         _semid = CreateSemaphore( NULL, 0, 1, name);
00041 #else
00042         _semid = semget( *(key_t*)(const char*)name, 1, 0666 | IPC_CREAT);
00043         reset();
00044 #endif
00045 }
00046 
00047 Semaphore::~Semaphore() {
00048         
00049 #ifdef WIN32
00050         CloseHandle( _semid);
00051 #endif
00052 
00053         /* don't destroy semaphore on Unix, because
00054          * another process can be still using it !
00055          * semaphore stays in system as long as we
00056          * start profiling again or reboot the machine
00057          * (or remove it manually using 'ipcrm') */
00058 }
00059 
00060 void Semaphore::wait() {
00061 
00062 #ifdef WIN32
00063         WaitForSingleObject( _semid, INFINITE);
00064 #else
00065         static sembuf sop[2] = { 0, 0, 0, 0, 1, SEM_UNDO};
00066         semop( _semid, sop, 2);
00067 #endif
00068 }
00069 
00070 void Semaphore::release() {
00071 
00072 #ifdef WIN32
00073         ReleaseSemaphore( _semid, 1, NULL);
00074 #else
00075         static sembuf sop = { 0, -1, SEM_UNDO | IPC_NOWAIT};
00076         semop( _semid, &sop, 1);
00077 #endif
00078 }
00079 
00080 void Semaphore::reset() {
00081 
00082 #ifndef WIN32
00083 #ifdef LINUX
00084         // we define semun union ourselves on linux
00085         union {
00086 
00087                 int val;
00088                 semid_ds* buf;
00089                 unsigned short int* array;
00090                 seminfo* __buf;
00091         } arg;
00092 #else
00093         // we define semun union ourselves on solaris
00094         union {
00095 
00096                 int val;
00097                 semid_ds* buf;
00098                 ushort_t* array;
00099         } arg;
00100 #endif
00101         arg.val = 1;
00102         semctl( _semid, 0, SETVAL, arg);
00103 #endif
00104 
00105         /* not necessary to reset semaphore on Win32,
00106          * JVM goes well together with the system
00107          * and it seems there are no problems
00108          * with semaphore counter */
00109 }

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