From 01c8b3d0c12dbd80c082bc370c624af4e249f74d Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 15 Nov 2004 13:08:46 +0000 Subject: [PATCH] Added ReentrantMutex --- src/osgDB/ReentrantMutex.cpp | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/osgDB/ReentrantMutex.cpp diff --git a/src/osgDB/ReentrantMutex.cpp b/src/osgDB/ReentrantMutex.cpp new file mode 100644 index 000000000..e4a89b39a --- /dev/null +++ b/src/osgDB/ReentrantMutex.cpp @@ -0,0 +1,75 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include + + +using namespace osgDB; + +ReentrantMutex::ReentrantMutex(): + _threadHoldingMutex(0), + _lockCount(0) +{ +} + +ReentrantMutex::~ReentrantMutex() +{ +} + +int ReentrantMutex::lock() +{ + if (_threadHoldingMutex==OpenThreads::Thread::CurrentThread()) + { + ++_lockCount; + return 0; + } + else + { + int result = Mutex::lock(); + if (result==0) + { + _threadHoldingMutex = OpenThreads::Thread::CurrentThread(); + _lockCount = 1; + } + return result; + } +} + +int ReentrantMutex::unlock() +{ + if (_threadHoldingMutex==OpenThreads::Thread::CurrentThread()) + { + --_lockCount; + if (_lockCount<=0) return Mutex::unlock(); + } + return 0; +} + +int ReentrantMutex::trylock() +{ + if (_threadHoldingMutex==OpenThreads::Thread::CurrentThread()) + { + ++_lockCount; + return 0; + } + else + { + int result = Mutex::trylock(); + if (result==0) + { + _threadHoldingMutex = OpenThreads::Thread::CurrentThread(); + _lockCount = 1; + } + return result; + } +}