fix race condition

This commit is contained in:
David Rose 2009-01-28 22:19:00 +00:00
parent 38d262b7ba
commit 9087b030a9
3 changed files with 27 additions and 3 deletions

View File

@ -27,6 +27,7 @@
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE void BamCache:: INLINE void BamCache::
set_active(bool active) { set_active(bool active) {
ReMutexHolder holder(_lock);
_active = active; _active = active;
} }
@ -45,6 +46,7 @@ set_active(bool active) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE bool BamCache:: INLINE bool BamCache::
get_active() const { get_active() const {
ReMutexHolder holder(_lock);
return _active; return _active;
} }
@ -56,6 +58,7 @@ get_active() const {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE void BamCache:: INLINE void BamCache::
set_cache_models(bool flag) { set_cache_models(bool flag) {
ReMutexHolder holder(_lock);
_cache_models = flag; _cache_models = flag;
} }
@ -69,6 +72,7 @@ set_cache_models(bool flag) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE bool BamCache:: INLINE bool BamCache::
get_cache_models() const { get_cache_models() const {
ReMutexHolder holder(_lock);
return _cache_models && _active; return _cache_models && _active;
} }
@ -80,6 +84,7 @@ get_cache_models() const {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE void BamCache:: INLINE void BamCache::
set_cache_textures(bool flag) { set_cache_textures(bool flag) {
ReMutexHolder holder(_lock);
_cache_textures = flag; _cache_textures = flag;
} }
@ -93,6 +98,7 @@ set_cache_textures(bool flag) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE bool BamCache:: INLINE bool BamCache::
get_cache_textures() const { get_cache_textures() const {
ReMutexHolder holder(_lock);
return _cache_textures && _active; return _cache_textures && _active;
} }
@ -117,6 +123,7 @@ get_cache_textures() const {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE void BamCache:: INLINE void BamCache::
set_cache_compressed_textures(bool flag) { set_cache_compressed_textures(bool flag) {
ReMutexHolder holder(_lock);
_cache_compressed_textures = flag; _cache_compressed_textures = flag;
} }
@ -131,6 +138,7 @@ set_cache_compressed_textures(bool flag) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE bool BamCache:: INLINE bool BamCache::
get_cache_compressed_textures() const { get_cache_compressed_textures() const {
ReMutexHolder holder(_lock);
return _cache_compressed_textures && _active; return _cache_compressed_textures && _active;
} }
@ -140,8 +148,9 @@ get_cache_compressed_textures() const {
// Description: Returns the current root pathname of the cache. See // Description: Returns the current root pathname of the cache. See
// set_root(). // set_root().
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE const Filename &BamCache:: INLINE Filename BamCache::
get_root() const { get_root() const {
ReMutexHolder holder(_lock);
return _root; return _root;
} }
@ -153,6 +162,7 @@ get_root() const {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE void BamCache:: INLINE void BamCache::
set_flush_time(int flush_time) { set_flush_time(int flush_time) {
ReMutexHolder holder(_lock);
_flush_time = flush_time; _flush_time = flush_time;
} }
@ -164,6 +174,7 @@ set_flush_time(int flush_time) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE int BamCache:: INLINE int BamCache::
get_flush_time() const { get_flush_time() const {
ReMutexHolder holder(_lock);
return _flush_time; return _flush_time;
} }
@ -183,6 +194,7 @@ get_flush_time() const {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE void BamCache:: INLINE void BamCache::
set_cache_max_kbytes(int max_kbytes) { set_cache_max_kbytes(int max_kbytes) {
ReMutexHolder holder(_lock);
_max_kbytes = max_kbytes; _max_kbytes = max_kbytes;
check_cache_size(); check_cache_size();
} }
@ -196,6 +208,7 @@ set_cache_max_kbytes(int max_kbytes) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE int BamCache:: INLINE int BamCache::
get_cache_max_kbytes() const { get_cache_max_kbytes() const {
ReMutexHolder holder(_lock);
return _max_kbytes; return _max_kbytes;
} }
@ -210,6 +223,7 @@ get_cache_max_kbytes() const {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE void BamCache:: INLINE void BamCache::
set_read_only(bool ro) { set_read_only(bool ro) {
ReMutexHolder holder(_lock);
_read_only = ro; _read_only = ro;
} }
@ -224,6 +238,7 @@ set_read_only(bool ro) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE bool BamCache:: INLINE bool BamCache::
get_read_only() const { get_read_only() const {
ReMutexHolder holder(_lock);
return _read_only; return _read_only;
} }

View File

@ -114,6 +114,7 @@ BamCache::
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
void BamCache:: void BamCache::
set_root(const Filename &root) { set_root(const Filename &root) {
ReMutexHolder holder(_lock);
flush_index(); flush_index();
_root = root; _root = root;
@ -159,6 +160,7 @@ set_root(const Filename &root) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
PT(BamCacheRecord) BamCache:: PT(BamCacheRecord) BamCache::
lookup(const Filename &source_filename, const string &cache_extension) { lookup(const Filename &source_filename, const string &cache_extension) {
ReMutexHolder holder(_lock);
consider_flush_index(); consider_flush_index();
VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
@ -191,6 +193,7 @@ lookup(const Filename &source_filename, const string &cache_extension) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
bool BamCache:: bool BamCache::
store(BamCacheRecord *record) { store(BamCacheRecord *record) {
ReMutexHolder holder(_lock);
nassertr(!record->_cache_pathname.empty(), false); nassertr(!record->_cache_pathname.empty(), false);
nassertr(record->has_data(), false); nassertr(record->has_data(), false);
@ -320,6 +323,7 @@ emergency_read_only() {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
void BamCache:: void BamCache::
consider_flush_index() { consider_flush_index() {
ReMutexHolder holder(_lock);
if (_index_stale_since != 0) { if (_index_stale_since != 0) {
int elapsed = (int)time(NULL) - (int)_index_stale_since; int elapsed = (int)time(NULL) - (int)_index_stale_since;
if (elapsed > _flush_time) { if (elapsed > _flush_time) {
@ -335,6 +339,7 @@ consider_flush_index() {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
void BamCache:: void BamCache::
flush_index() { flush_index() {
ReMutexHolder holder(_lock);
if (_index_stale_since == 0) { if (_index_stale_since == 0) {
// Never mind. // Never mind.
return; return;
@ -991,7 +996,7 @@ void BamCache::
make_global() { make_global() {
_global_ptr = new BamCache; _global_ptr = new BamCache;
if (_global_ptr->get_root().empty()) { if (_global_ptr->_root.empty()) {
_global_ptr->set_active(false); _global_ptr->set_active(false);
} }
} }

View File

@ -21,6 +21,8 @@
#include "filename.h" #include "filename.h"
#include "pmap.h" #include "pmap.h"
#include "pvector.h" #include "pvector.h"
#include "reMutex.h"
#include "reMutexHolder.h"
class BamCacheIndex; class BamCacheIndex;
@ -58,7 +60,7 @@ PUBLISHED:
INLINE bool get_cache_compressed_textures() const; INLINE bool get_cache_compressed_textures() const;
void set_root(const Filename &root); void set_root(const Filename &root);
INLINE const Filename &get_root() const; INLINE Filename get_root() const;
INLINE void set_flush_time(int flush_time); INLINE void set_flush_time(int flush_time);
INLINE int get_flush_time() const; INLINE int get_flush_time() const;
@ -122,6 +124,8 @@ private:
Filename _index_pathname; Filename _index_pathname;
string _index_ref_contents; string _index_ref_contents;
ReMutex _lock;
}; };
#include "bamCache.I" #include "bamCache.I"