Mutex.cpp
Go to the documentation of this file.
00001 // ****************************************************************************
00002 // This file is part of the Integrating Vision Toolkit (IVT).
00003 //
00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
00006 //
00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are met:
00012 //
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 // 2. Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the distribution.
00019 //
00020 // 3. Neither the name of the KIT nor the names of its contributors may be
00021 //    used to endorse or promote products derived from this software
00022 //    without specific prior written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 // ****************************************************************************
00035 // ****************************************************************************
00036 // Filename:  Mutex.cpp
00037 // Author:    Kai Welke
00038 // Date:      13.01.2006
00039 // ****************************************************************************
00040 
00041 
00042 // ****************************************************************************
00043 // Includes
00044 // ****************************************************************************
00045 
00046 #include <new> // for explicitly using correct new/delete operators on VC DSPs
00047 
00048 #include "Mutex.h"
00049 
00050 
00051 
00052 // ****************************************************************************
00053 // Constructor / Destructor
00054 // ****************************************************************************
00055 
00056 CMutex::CMutex()
00057 {
00058 #ifdef WIN32
00059         InitializeCriticalSection(&m_CSWindowsMutex);
00060 #else
00061         pthread_mutexattr_init(&m_MutexAttr);
00062         pthread_mutex_init(&m_PosixMutex ,&m_MutexAttr);
00063 #endif
00064 }
00065 
00066 CMutex::~CMutex()
00067 {
00068 #ifdef WIN32
00069         DeleteCriticalSection(&m_CSWindowsMutex);
00070 #else
00071         pthread_mutex_destroy(&m_PosixMutex);
00072         pthread_mutexattr_destroy(&m_MutexAttr);
00073 #endif
00074 }
00075 
00076 
00077 // ****************************************************************************
00078 // Methods
00079 // ****************************************************************************
00080 
00081 // try to lock the mutex. If another thread is owner of mutex, wait till mutex is unlocked
00082 Threading::EMutexStatus CMutex::Lock()
00083 {
00084 #ifdef WIN32
00085         EnterCriticalSection(&m_CSWindowsMutex);
00086         return Threading::eMutexSuccess;
00087 #else
00088         if (pthread_mutex_lock(&m_PosixMutex) == 0)
00089                 return Threading::eMutexSuccess;
00090         else
00091                 return Threading::eMutexError;
00092 #endif
00093 }
00094 
00095 // try to lock the mutex. If another thread is owner of mutex, return EBusy, otherwise lock mutex
00096 Threading::EMutexStatus CMutex::TryLock()
00097 {
00098 #ifdef WIN32
00099         if (TryEnterCriticalSection(&m_CSWindowsMutex) == 0)
00100         {
00101                 return Threading::eMutexBusy;
00102         }
00103         else
00104         {
00105                 return Threading::eMutexSuccess;
00106         }
00107 #else
00108         const int nResult = pthread_mutex_trylock(&m_PosixMutex);
00109         
00110         if (nResult == 0)
00111         {
00112                 return Threading::eMutexSuccess;
00113         }
00114         else if (nResult == EBUSY)
00115         {
00116                 return Threading::eMutexBusy;
00117         }
00118         else
00119         {
00120                 return Threading::eMutexError;
00121         }
00122 #endif
00123 }
00124 
00125 // unlock mutex
00126 Threading::EMutexStatus CMutex::UnLock()
00127 {
00128 #ifdef WIN32
00129         LeaveCriticalSection(&m_CSWindowsMutex);
00130         return Threading::eMutexSuccess;
00131 #else
00132         if (pthread_mutex_unlock(&m_PosixMutex) == 0)
00133         {
00134                 return Threading::eMutexSuccess;
00135         }
00136         else
00137         {
00138                 return Threading::eMutexError;
00139         }
00140 #endif
00141 }


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:57