blocxx

IPCMutex.hpp

Go to the documentation of this file.
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 
00035 #ifndef _BLOCXX_IPCMUTEX_HPP_INCLUDE_GUARD_
00036 #define _BLOCXX_IPCMUTEX_HPP_INCLUDE_GUARD_
00037 
00038 #include "blocxx/BLOCXX_config.h"
00039 // This class only works on Linux.
00040 #ifdef BLOCXX_GNU_LINUX
00041 #if !defined (BLOCXX_HAVE_SYS_IPC_H) && !defined (BLOCXX_HAVE_SYS_SEM_H)
00042  #error "Port me!"
00043 #else
00044 
00045 #include "blocxx/Exception.hpp"
00046 
00047 #include <sys/types.h>
00048 #include <sys/ipc.h>
00049 #include <sys/sem.h>
00050 
00051 
00052 namespace BLOCXX_NAMESPACE
00053 {
00054 
00055 BLOCXX_DECLARE_APIEXCEPTION(IPCMutex, BLOCXX_COMMON_API);
00056 
00061 class IPCMutex
00062 {
00063 public:
00074    IPCMutex(int semId); 
00075    ~IPCMutex() {}; 
00076 
00080    void wait(); 
00081 
00085    void signal(); 
00090    int getId() { return m_semid;}
00091 
00096    static void free(int semKey); 
00097 private:
00098    int m_semid; 
00099    struct sembuf m_sbuf; 
00100    union semun
00101    {
00102       int val; 
00103       struct sumid_ds* buf; 
00104       unsigned short* array;
00105    } m_arg; 
00106 
00107 }; 
00108 
00110 
00124 class IPCMutexLock
00125 {
00126 public:
00127    IPCMutexLock(IPCMutex& sem); 
00128    ~IPCMutexLock(); 
00129 private:
00130    IPCMutex& m_sem; 
00131 }; 
00132 
00133 } // end namespace
00134 
00135 #endif // #if !defined (BLOCXX_HAVE_SYS_IPC_H) && !defined (BLOCXX_HAVE_SYS_SEM_H)
00136 #endif // #ifdef BLOCXX_GNU_LINUX
00137 #endif // #ifndef _IPCMUTEXLOCK_HPP_INCLUDE_GUARD_
00138