blocxx
IPCMutex.cpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005 Novell, Inc. All rights reserved.
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are met:
6*
7* - Redistributions of source code must retain the above copyright notice,
8* this list of conditions and the following disclaimer.
9*
10* - Redistributions in binary form must reproduce the above copyright notice,
11* this list of conditions and the following disclaimer in the documentation
12* and/or other materials provided with the distribution.
13*
14* - Neither the name of Novell, Inc., nor the names of its
15* contributors may be used to endorse or promote products derived from this
16* software without specific prior written permission.
17*
18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
19* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21* ARE DISCLAIMED. IN NO EVENT SHALL Novell, Inc., OR THE
22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*******************************************************************************/
30
31
36#include "blocxx/BLOCXX_config.h"
37
38#ifdef BLOCXX_GNU_LINUX
39#include "blocxx/IPCMutex.hpp"
41
42
43namespace BLOCXX_NAMESPACE
44{
45
46const int ADD_KEY = 1;
47const int BLOCK_FOR_KEY = -1;
48
50
51
53IPCMutex::IPCMutex(int semKey)
54{
55 m_sbuf.sem_num = 0;
56 m_sbuf.sem_flg = 0;
57 m_semid = semget((key_t)semKey, 1, 0666);
58 if (m_semid == -1)
59 {
60 m_semid = semget((key_t)semKey, 1, IPC_CREAT | 0666);
61 if (m_semid == -1)
62 {
63 BLOCXX_THROW(IPCMutexException,
64 "Unable to create semaphore");
65 return;
66 }
67 m_arg.val = 1;
68 if (semctl(m_semid, 0, SETVAL, m_arg) != 0)
69 {
70 BLOCXX_THROW_ERRNO_MSG(IPCMutexException,
71 "semctl() failed");
72 }
73 }
74}
75
77void
78IPCMutex::wait()
79{
80 m_sbuf.sem_op = BLOCK_FOR_KEY;
81 if (semop(m_semid, &m_sbuf, 1) != 0)
82 {
83 BLOCXX_THROW_ERRNO_MSG(IPCMutexException,
84 "Failed to wait on semaphore");
85 }
86}
87
89void
90IPCMutex::signal()
91{
92 m_sbuf.sem_op = ADD_KEY;
93 if (semop(m_semid, &m_sbuf, 1) != 0)
94 {
95 BLOCXX_THROW_ERRNO_MSG(IPCMutexException,
96 "Failed to signal semaphore");
97 }
98}
99
101// static
102void
103IPCMutex::free(int semKey)
104{
105 int semid = semget((key_t)semKey, 1, 0666);
106 if (semid != -1)
107 {
108 semctl(semid, 1, IPC_RMID, 0);
109 }
110}
111
113IPCMutexLock::IPCMutexLock(IPCMutex& sem)
114: m_sem(sem)
115{
116 m_sem.wait();
117}
118
120IPCMutexLock::~IPCMutexLock()
121{
122 m_sem.signal();
123}
124
125}
126
127#endif // BLOCXX_GNU_LINUX
#define BLOCXX_DEFINE_EXCEPTION_WITH_ID(NAME)
Define a new exception class named <NAME>Exception that derives from Exception.
#define BLOCXX_THROW(exType, msg)
Throw an exception using FILE and LINE.
#define BLOCXX_THROW_ERRNO_MSG(exType, msg)
Throw an exception using FILE, LINE, errno and strerror(errno)
Taken from RFC 1321.