Example #1
0
bool
ResourceCache::getOrCreateStateSet(SkinResource*                skin,
                                   osg::ref_ptr<osg::StateSet>& output)
{
    output = 0L;
    std::string key = skin->getConfig().toJSON(false);

    // exclusive lock (since it's an LRU)
    {
        Threading::ScopedMutexLock exclusive( _skinMutex );
            
        // double check to avoid race condition
        SkinCache::Record rec;
        if ( _skinCache.get(key, rec) && rec.value().valid() )
        {
            output = rec.value().get();
        }
        else
        {
            // still not there, make it.
            output = skin->createStateSet( _dbOptions.get() );
            if ( output.valid() )
            {
                _skinCache.insert( key, output.get() );
            }
        }
    }

    return output.valid();
}
Example #2
0
osg::StateSet*
ResourceCache::getStateSet( SkinResource* skin )
{
    osg::StateSet* result = 0L;

    if ( _threadSafe )
    {
        // first check if it exists
        {
            Threading::ScopedReadLock shared( _mutex );

            SkinCache::Record rec = _skinCache.get( skin );
            if ( rec.valid() )
            {
                result = rec.value();
            }
        }

        // no? exclusive lock and create it.
        if ( !result )
        {
            Threading::ScopedWriteLock exclusive( _mutex );
            
            // double check to avoid race condition
            SkinCache::Record rec = _skinCache.get( skin );
            if ( rec.valid() )
            {
                result = rec.value();
            }
            else
            {
                // still not there, make it.
                result = skin->createStateSet( _dbOptions.get() );
                if ( result )
                    _skinCache.insert( skin, result );
            }
        }
    }

    else
    {
        SkinCache::Record rec = _skinCache.get( skin );
        if ( rec.valid() )
        {
            result = rec.value();
        }
        else
        {
            result = skin->createStateSet( _dbOptions.get() );
            if ( result )
                _skinCache.insert( skin, result );
        }
    }

    return result;
}
Example #3
0
osg::StateSet*
ResourceCache::getStateSet( SkinResource* skin )
{
    // first check if it exists
    {
        Threading::ScopedReadLock shared( _skinCacheMutex );
        SkinCache::Record rec = _skinCache.get( skin );
        if ( rec.valid() )
            return rec.value();
    }

    // no? exclusive lock and create it.
    {
        Threading::ScopedWriteLock exclusive( _skinCacheMutex );

        // double check to avoid race condition
        SkinCache::Record rec = _skinCache.get( skin );
        if ( rec.valid() )
            return rec.value();

        // still not there, make it.
        osg::StateSet* stateSet = skin->createStateSet();
        if ( stateSet )
            _skinCache.insert( skin, stateSet );

        return stateSet;
    }
}
Example #4
0
bool
ResourceCache::getOrCreateStateSet(SkinResource*                skin,
                                   osg::ref_ptr<osg::StateSet>& output,
                                   const osgDB::Options*        readOptions)
{
    output = 0L;
    //std::string key = skin->getConfig().toJSON(false);

    // Note: we use the imageURI as the basis for the caching key since 
    // it's the only property used by Skin->createStateSet(). If that
    // changes, we need to address it here. It might be better it SkinResource
    // were to provide a unique key.
    std::string key = skin->getUniqueID();

    // exclusive lock (since it's an LRU)
    {
        Threading::ScopedMutexLock exclusive( _skinMutex );
            
        // double check to avoid race condition
        SkinCache::Record rec;       
        if ( _skinCache.get(key, rec) && rec.value().valid() )
        {
            output = rec.value().get();
        }
        else
        {
            // still not there, make it.
            output = skin->createStateSet(readOptions);
            if ( output.valid() )
            {
                _skinCache.insert( key, output.get() );
            }
        }
    }

    return output.valid();
}