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    pthread_mutexattr_destroy(&attr); 
00068    if (res != 0)
00069    {
00070       return -1;
00071    }
00072  
00073    return 0;
00074 #elif defined(BLOCXX_WIN32)
00075    int cc = -1;
00076    if ((handle = CreateMutex(NULL, FALSE, NULL)))
00077    {
00078       cc = 0;
00079    }
00080    return cc;
00081 #else
00082 #error "port me!"
00083 #endif
00084 }
00094 int
00095 destroyMutex(NonRecursiveMutex_t& handle)
00096 {
00097 #if defined(BLOCXX_USE_PTHREAD)
00098    switch (pthread_mutex_destroy(&handle.mutex))
00099    {
00100       case 0:
00101          break;
00102       case EBUSY:
00103          return -1;
00104          break;
00105       default:
00106          return -2;
00107    }
00108    return 0;
00109 #elif defined (BLOCXX_WIN32)
00110    ReleaseMutex(handle);
00111    return (CloseHandle(handle) == 0) ? -2 : 0;
00112 #else
00113 #error "port me!"
00114 #endif
00115 }
00124 int
00125 acquireMutex(NonRecursiveMutex_t& handle)
00126 {
00127 #if defined (BLOCXX_USE_PTHREAD)
00128    int res = pthread_mutex_lock(&handle.mutex);
00129    assert(res == 0);
00130    return res;
00131 #elif defined (BLOCXX_WIN32)
00132    int cc = -1;
00133    if (WaitForSingleObject(handle, INFINITE) != WAIT_FAILED)
00134    {
00135       cc = 0;
00136    }
00137    return cc;
00138 #else
00139 #error "port me!"
00140 #endif
00141 }
00148 int
00149 releaseMutex(NonRecursiveMutex_t& handle)
00150 {
00151 #if defined (BLOCXX_USE_PTHREAD)
00152    int res = pthread_mutex_unlock(&handle.mutex);
00153    assert(res == 0);
00154    return res;
00155  
00156 #elif defined (BLOCXX_WIN32)
00157    return (ReleaseMutex(handle)) ? 0 : -1;
00158 #else
00159 #error "port me!"
00160 #endif
00161 }
00162 
00163 int
00164 conditionPreWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
00165 {
00166 #if defined (BLOCXX_WIN32)
00167    state.pmutex = &handle;
00168 #else
00169    state.pmutex = &handle.mutex;
00170 #endif
00171    return 0;
00172 }
00173 int
00174 conditionPostWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
00175 {
00176    return 0;
00177 }
00178 
00179 } // end namespace NonRecursiveMutexImpl
00180 } // end namespace BLOCXX_NAMESPACE
00181 

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6