blocxx
|
00001 /******************************************************************************* 00002 * Copyright (C) 2005 Novell, Inc. All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are met: 00006 * 00007 * - Redistributions of source code must retain the above copyright notice, 00008 * this list of conditions and the following disclaimer. 00009 * 00010 * - Redistributions in binary form must reproduce the above copyright notice, 00011 * this list of conditions and the following disclaimer in the documentation 00012 * and/or other materials provided with the distribution. 00013 * 00014 * - Neither the name of Novell, Inc., nor the names of its 00015 * contributors may be used to endorse or promote products derived from this 00016 * software without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 * ARE DISCLAIMED. IN NO EVENT SHALL Novell, Inc., OR THE 00022 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00024 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 00025 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00026 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00027 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 00028 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 *******************************************************************************/ 00030 00031 00036 #include "blocxx/BLOCXX_config.h" 00037 00038 #ifdef BLOCXX_GNU_LINUX 00039 #include "blocxx/IPCMutex.hpp" 00040 #include "blocxx/ExceptionIds.hpp" 00041 00042 00043 namespace BLOCXX_NAMESPACE 00044 { 00045 00046 const int ADD_KEY = 1; 00047 const int BLOCK_FOR_KEY = -1; 00048 00049 BLOCXX_DEFINE_EXCEPTION_WITH_ID(IPCMutex); 00050 00051 00053 IPCMutex::IPCMutex(int semKey) 00054 { 00055 m_sbuf.sem_num = 0; 00056 m_sbuf.sem_flg = 0; 00057 m_semid = semget((key_t)semKey, 1, 0666); 00058 if (m_semid == -1) 00059 { 00060 m_semid = semget((key_t)semKey, 1, IPC_CREAT | 0666); 00061 if (m_semid == -1) 00062 { 00063 BLOCXX_THROW(IPCMutexException, 00064 "Unable to create semaphore"); 00065 return; 00066 } 00067 m_arg.val = 1; 00068 if (semctl(m_semid, 0, SETVAL, m_arg) != 0) 00069 { 00070 BLOCXX_THROW_ERRNO_MSG(IPCMutexException, 00071 "semctl() failed"); 00072 } 00073 } 00074 } 00075 00077 void 00078 IPCMutex::wait() 00079 { 00080 m_sbuf.sem_op = BLOCK_FOR_KEY; 00081 if (semop(m_semid, &m_sbuf, 1) != 0) 00082 { 00083 BLOCXX_THROW_ERRNO_MSG(IPCMutexException, 00084 "Failed to wait on semaphore"); 00085 } 00086 } 00087 00089 void 00090 IPCMutex::signal() 00091 { 00092 m_sbuf.sem_op = ADD_KEY; 00093 if (semop(m_semid, &m_sbuf, 1) != 0) 00094 { 00095 BLOCXX_THROW_ERRNO_MSG(IPCMutexException, 00096 "Failed to signal semaphore"); 00097 } 00098 } 00099 00101 // static 00102 void 00103 IPCMutex::free(int semKey) 00104 { 00105 int semid = semget((key_t)semKey, 1, 0666); 00106 if (semid != -1) 00107 { 00108 semctl(semid, 1, IPC_RMID, 0); 00109 } 00110 } 00111 00113 IPCMutexLock::IPCMutexLock(IPCMutex& sem) 00114 : m_sem(sem) 00115 { 00116 m_sem.wait(); 00117 } 00118 00120 IPCMutexLock::~IPCMutexLock() 00121 { 00122 m_sem.signal(); 00123 } 00124 00125 } 00126 00127 #endif // BLOCXX_GNU_LINUX