Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

NonRecursiveMutexImpl.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 Novell, Inc. All rights reserved.
00004 *
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 *
00008 *  - Redistributions of source code must retain the above copyright notice,
00009 *    this list of conditions and the following disclaimer.
00010 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 *******************************************************************************/
00031 
00032 
00038 #include "blocxx/BLOCXX_config.h"
00039 #include "blocxx/NonRecursiveMutexImpl.hpp"
00040 #include <cerrno>
00041 #include <cassert>
00042 
00043 namespace BLOCXX_NAMESPACE
00044 {
00045 
00046 namespace NonRecursiveMutexImpl
00047 {
00048 
00054 int
00055 createMutex(NonRecursiveMutex_t& handle)
00056 {
00057 #if defined(BLOCXX_USE_PTHREAD)
00058    pthread_mutexattr_t attr;
00059    int res = pthread_mutexattr_init(&attr);
00060    assert(res == 0);
00061    if (res != 0)
00062    {
00063       return -1;
00064    }
00065  
00066    res = pthread_mutex_init(&handle.mutex, &attr);
00067    if (res != 0)
00068    {
00069       return -1;
00070    }
00071  
00072    return 0;
00073 #elif defined(BLOCXX_WIN32)
00074    int cc = -1;
00075    if ((handle = CreateMutex(NULL, FALSE, NULL)))
00076    {
00077       cc = 0;
00078    }
00079    return cc;
00080 #else
00081 #error "port me!"
00082 #endif
00083 }
00093 int
00094 destroyMutex(NonRecursiveMutex_t& handle)
00095 {
00096 #if defined(BLOCXX_USE_PTHREAD)
00097    switch (pthread_mutex_destroy(&handle.mutex))
00098    {
00099       case 0:
00100          break;
00101       case EBUSY:
00102          return -1;
00103          break;
00104       default:
00105          return -2;
00106    }
00107    return 0;
00108 #elif defined (BLOCXX_WIN32)
00109    ReleaseMutex(handle);
00110    return (CloseHandle(handle) == 0) ? -2 : 0;
00111 #else
00112 #error "port me!"
00113 #endif
00114 }
00123 int
00124 acquireMutex(NonRecursiveMutex_t& handle)
00125 {
00126 #if defined (BLOCXX_USE_PTHREAD)
00127    int res = pthread_mutex_lock(&handle.mutex);
00128    assert(res == 0);
00129    return res;
00130 #elif defined (BLOCXX_WIN32)
00131    int cc = -1;
00132    if (WaitForSingleObject(handle, INFINITE) != WAIT_FAILED)
00133    {
00134       cc = 0;
00135    }
00136    return cc;
00137 #else
00138 #error "port me!"
00139 #endif
00140 }
00147 int
00148 releaseMutex(NonRecursiveMutex_t& handle)
00149 {
00150 #if defined (BLOCXX_USE_PTHREAD)
00151    int res = pthread_mutex_unlock(&handle.mutex);
00152    assert(res == 0);
00153    return res;
00154  
00155 #elif defined (BLOCXX_WIN32)
00156    return (ReleaseMutex(handle)) ? 0 : -1;
00157 #else
00158 #error "port me!"
00159 #endif
00160 }
00161 
00162 int
00163 conditionPreWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
00164 {
00165 #if defined (BLOCXX_WIN32)
00166    state.pmutex = &handle;
00167 #else
00168    state.pmutex = &handle.mutex;
00169 #endif
00170    return 0;
00171 }
00172 int
00173 conditionPostWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
00174 {
00175    return 0;
00176 }
00177 
00178 } // end namespace NonRecursiveMutexImpl
00179 } // end namespace BLOCXX_NAMESPACE
00180 

Generated on Mon Sep 12 23:56:35 2005 for blocxx by  doxygen 1.4.4