Mutex.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00023 //----------------------------------------------------------------------
00024 #include "icl_core_thread/Mutex.h"
00025 
00026 
00027 #include <icl_core/os_lxrt.h>
00028 
00029 #undef ICL_CORE_LOCAL_LOGGING
00030 //#define ICL_CORE_LOCAL_LOGGING
00031 #include "icl_core_thread/Logging.h"
00032 
00033 #define LOCAL_PRINTF(args)
00034 //#define LOCAL_PRINTF PRINTF
00035 
00036 #if defined _SYSTEM_LXRT_
00037 # include "icl_core_thread/MutexImplLxrt.h"
00038 #endif
00039 
00040 #if defined _SYSTEM_POSIX_
00041 #  include "icl_core_thread/MutexImplPosix.h"
00042 #elif defined _SYSTEM_WIN32_
00043 # include "icl_core_thread/MutexImplWin32.h"
00044 #else
00045 # error "No mutex implementation defined for this platform."
00046 #endif
00047 
00048 using icl_core::logging::endl;
00049 
00050 namespace icl_core {
00051 namespace thread {
00052 
00053 Mutex::Mutex()
00054   : m_impl(NULL)
00055 {
00056 #if defined _SYSTEM_LXRT_
00057   // Only create an LXRT implementation if the LXRT runtime system is
00058   // really available. Otherwise create an ACE or POSIX
00059   // implementation, depending on the system configuration.
00060   // Remark: This allows us to compile programs with LXRT support but
00061   // run them on systems on which no LXRT is installed and to disable
00062   // LXRT support at program startup on systems with installed LXRT!
00063   if (icl_core::os::isLxrtAvailable())
00064   {
00065     LOCAL_PRINTF("Initializing LXRT mutex.\n");
00066     m_impl = new MutexImplLxrt;
00067   }
00068   else
00069   {
00070     LOCAL_PRINTF("Initializing POSIX mutex.\n");
00071     m_impl = new MutexImplPosix;
00072   }
00073 
00074 #elif defined _SYSTEM_POSIX_
00075   LOCAL_PRINTF("Initializing POSIX mutex.\n");
00076   m_impl = new MutexImplPosix;
00077 
00078 #elif defined _SYSTEM_WIN32_
00079   LOCAL_PRINTF("Initializing WIN32 mutex.\n");
00080   m_impl = new MutexImplWin32;
00081 
00082 #endif
00083 }
00084 
00085 Mutex::~Mutex()
00086 {
00087   LOCAL_PRINTF("Destroying mutex.\n");
00088   delete m_impl;
00089   m_impl = NULL;
00090 }
00091 
00092 bool Mutex::lock()
00093 {
00094   LLOGGING_TRACE_C(Thread, Mutex, "Locking mutex ..." << endl);
00095   bool result = m_impl->lock();
00096   LLOGGING_TRACE_C(Thread, Mutex, "Mutex lock " << (result ? "successful" : "failed") << "." << endl);
00097   return result;
00098 }
00099 
00100 bool Mutex::lock(const ::icl_core::TimeStamp& timeout)
00101 {
00102   LLOGGING_TRACE_C(Thread, Mutex, "Locking mutex with absolute timeout " << timeout << " ..." << endl);
00103   bool result = m_impl->lock(timeout);
00104   LLOGGING_TRACE_C(Thread, Mutex, "Mutex lock " << (result ? "successful" : "failed") << "." << endl);
00105   return result;
00106 }
00107 
00108 bool Mutex::lock(const ::icl_core::TimeSpan& timeout)
00109 {
00110   LLOGGING_TRACE_C(Thread, Mutex, "Locking mutex with relative timeout " << timeout << " ..." << endl);
00111   bool result = m_impl->lock(timeout);
00112   LLOGGING_TRACE_C(Thread, Mutex, "Mutex lock " << (result ? "successful" : "failed") << "." << endl);
00113   return result;
00114 }
00115 
00116 bool Mutex::tryLock()
00117 {
00118   LLOGGING_TRACE_C(Thread, Mutex, "Trying to lock mutex ..." << endl);
00119   bool result = m_impl->tryLock();
00120   LLOGGING_TRACE_C(Thread, Mutex, "Mutex try-lock " << (result ? "successful" : "failed") << "." << endl);
00121   return result;
00122 }
00123 
00124 void Mutex::unlock()
00125 {
00126   LLOGGING_TRACE_C(Thread, Mutex, "Unlocking mutex." << endl);
00127   m_impl->unlock();
00128 }
00129 
00131 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
00132 
00137 bool Mutex::Lock()
00138 {
00139   return lock();
00140 }
00141 
00146 bool Mutex::Lock(const icl_core::TimeStamp& timeout)
00147 {
00148   return lock(timeout);
00149 }
00150 
00155 bool Mutex::Lock(const icl_core::TimeSpan& timeout)
00156 {
00157   return lock(timeout);
00158 }
00159 
00166 bool Mutex::TryLock()
00167 {
00168   return tryLock();
00169 }
00170 
00173 void Mutex::Unlock()
00174 {
00175   unlock();
00176 }
00177 
00178 #endif
00179 
00180 
00181 }
00182 }


fzi_icl_core
Author(s):
autogenerated on Tue Aug 8 2017 02:28:03