MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSGenLib/MOOSLock.cpp
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite 
00004 //  
00005 //   A suit of Applications and Libraries for Mobile Robotics Research 
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and 
00007 //   Oxford University. 
00008 //    
00009 //   This software was written by Paul Newman at MIT 2001-2002 and Oxford 
00010 //   University 2003-2005. email: pnewman@robots.ox.ac.uk. 
00011 //      
00012 //   This file is part of a  MOOS Core Component. 
00013 //        
00014 //   This program is free software; you can redistribute it and/or 
00015 //   modify it under the terms of the GNU General Public License as 
00016 //   published by the Free Software Foundation; either version 2 of the 
00017 //   License, or (at your option) any later version. 
00018 //          
00019 //   This program is distributed in the hope that it will be useful, 
00020 //   but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
00022 //   General Public License for more details. 
00023 //            
00024 //   You should have received a copy of the GNU General Public License 
00025 //   along with this program; if not, write to the Free Software 
00026 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
00027 //   02111-1307, USA. 
00028 //
00030 /***************************************************************************
00031                           CMLLock.cpp  -  description
00032                              -------------------
00033     begin                : Mon Dec 18 2000
00034     copyright            : (C) 2000 by pnewman
00035     email                : pnewman@mit.edu
00036  ***************************************************************************/
00037 
00038 
00039 // MOOSLock.cpp: implementation of the CMOOSLock class.
00040 //
00042 
00043 #include "MOOSLock.h"
00044 
00045 
00046 
00047 #ifndef _WIN32
00048     #include <pthread.h>
00049 #endif
00050 
00051 
00053 // Construction/Destruction
00055 
00056 CMOOSLock::CMOOSLock(bool bInitial)
00057 {
00058 #ifdef _WIN32
00059     m_hLock = ::CreateEvent(NULL,false,bInitial,NULL);
00060 #else
00061     pthread_mutex_init(&m_hLock,NULL);
00062 #endif
00063 }
00064 
00065 CMOOSLock::~CMOOSLock()
00066 {
00067 #ifdef _WIN32
00068     if (m_hLock != INVALID_HANDLE_VALUE && m_hLock != 0)
00069     {
00070         ::CloseHandle(m_hLock);
00071     }
00072 #else
00073       pthread_mutex_destroy( &m_hLock);
00074 #endif
00075 
00076 
00077 }
00078 
00079 void CMOOSLock::Lock()
00080 {
00081 #ifdef _WIN32
00082     if (m_hLock != INVALID_HANDLE_VALUE && m_hLock != 0)
00083     {
00084         ::WaitForSingleObject(m_hLock,INFINITE);
00085     }
00086 #else
00087 
00088     pthread_mutex_lock( & m_hLock);
00089 
00090 #endif
00091 
00092 }
00093 
00094 void CMOOSLock::UnLock()
00095 {
00096 #ifdef _WIN32
00097     if (m_hLock != INVALID_HANDLE_VALUE && m_hLock != 0)
00098     {
00099         ::SetEvent(m_hLock);
00100     }
00101 #else
00102     pthread_mutex_unlock( & m_hLock);
00103 #endif
00104 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines