NCBI C++ Toolkit Cross Reference

C++/src/util/mutex_pool.cpp


  1 /*  $Id: mutex_pool.cpp 103491 2007-05-04 17:18:18Z kazimird $
  2 * ===========================================================================
  3 *
  4 *                            PUBLIC DOMAIN NOTICE
  5 *               National Center for Biotechnology Information
  6 *
  7 *  This software/database is a "United States Government Work" under the
  8 *  terms of the United States Copyright Act.  It was written as part of
  9 *  the author's official duties as a United States Government employee and
 10 *  thus cannot be copyrighted.  This software/database is freely available
 11 *  to the public for use. The National Library of Medicine and the U.S.
 12 *  Government have not placed any restriction on its use or reproduction.
 13 *
 14 *  Although all reasonable efforts have been taken to ensure the accuracy
 15 *  and reliability of the software and data, the NLM and the U.S.
 16 *  Government do not and cannot warrant the performance or results that
 17 *  may be obtained by using this software or data. The NLM and the U.S.
 18 *  Government disclaim all warranties, express or implied, including
 19 *  warranties of performance, merchantability or fitness for any particular
 20 *  purpose.
 21 *
 22 *  Please cite the author in any work or product based on this material.
 23 *
 24 * ===========================================================================
 25 *
 26 * Authors:
 27 *           Eugene Vasilchenko
 28 *
 29 * File Description:
 30 *           Pool of mutexes for initialization of objects
 31 *
 32 */
 33 
 34 #include <ncbi_pch.hpp>
 35 #include <util/mutex_pool.hpp>
 36 
 37 BEGIN_NCBI_SCOPE
 38 
 39 /////////////////////////////////////////////////////////////////////////////
 40 // CInitMutexPool
 41 /////////////////////////////////////////////////////////////////////////////
 42 
 43 CInitMutexPool::CInitMutexPool(void)
 44 {
 45 }
 46 
 47 
 48 CInitMutexPool::~CInitMutexPool(void)
 49 {
 50 }
 51 
 52 
 53 bool CInitMutexPool::AcquireMutex(CInitMutex_Base& init, CRef<TMutex>& mutex)
 54 {
 55     _ASSERT(!mutex);
 56     CRef<TMutex> local(init.m_Mutex);
 57     if ( !local ) {
 58         CFastMutexGuard guard(m_Pool_Mtx);
 59         if ( init )
 60             return false;
 61         local = init.m_Mutex;
 62         if ( !local ) {
 63             if ( m_MutexList.empty() ) {
 64                 local.Reset(new TMutex(*this));
 65                 local->DoDeleteThisObject();
 66             }
 67             else {
 68                 local = m_MutexList.front();
 69                 m_MutexList.pop_front();
 70             }
 71             init.m_Mutex = local;
 72         }
 73     }
 74     local.Swap(mutex);
 75     _ASSERT(mutex);
 76     return true;
 77 }
 78 
 79 
 80 void CInitMutexPool::ReleaseMutex(CInitMutex_Base& init, CRef<TMutex>& mutex)
 81 {
 82     _ASSERT(mutex);
 83     if ( !init ) {
 84         return;
 85     }
 86     CFastMutexGuard guard(m_Pool_Mtx);
 87     CRef<TMutex> local;
 88     local.Swap(mutex);
 89     _ASSERT(local);
 90     init.m_Mutex.Reset();
 91     if ( local->ReferencedOnlyOnce() ) {
 92         m_MutexList.push_back(local);
 93     }
 94     _ASSERT(!mutex);
 95 }
 96 
 97 
 98 END_NCBI_SCOPE
 99 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.