uniquify cache temp filename by thread

This commit is contained in:
David Rose 2008-08-16 01:22:18 +00:00
parent 33b54effd2
commit 17f898413e
11 changed files with 98 additions and 1 deletions

View File

@ -107,6 +107,18 @@ get_pstats_index() const {
return _pstats_index; return _pstats_index;
} }
////////////////////////////////////////////////////////////////////
// Function: Thread::get_unique_id
// Access: Published
// Description: Returns a string that is guaranteed to be unique to
// this thread, across all processes on the machine,
// during at least the lifetime of this process.
////////////////////////////////////////////////////////////////////
INLINE string Thread::
get_unique_id() const {
return _impl.get_unique_id();
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: Thread::get_pipeline_stage // Function: Thread::get_pipeline_stage
// Access: Published // Access: Published

View File

@ -60,6 +60,7 @@ PUBLISHED:
INLINE const string &get_sync_name() const; INLINE const string &get_sync_name() const;
INLINE int get_pstats_index() const; INLINE int get_pstats_index() const;
INLINE string get_unique_id() const;
INLINE int get_pipeline_stage() const; INLINE int get_pipeline_stage() const;
void set_pipeline_stage(int pipeline_stage); void set_pipeline_stage(int pipeline_stage);

View File

@ -19,6 +19,29 @@
#include "threadDummyImpl.h" #include "threadDummyImpl.h"
#include "thread.h" #include "thread.h"
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#endif
////////////////////////////////////////////////////////////////////
// Function: ThreadDummyImpl::get_unique_id
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
string ThreadDummyImpl::
get_unique_id() const {
// In a single-threaded application, this is just the unique process
// ID.
ostringstream strm;
#ifdef WIN32
strm << GetCurrentProcessId();
#else
strm << getpid();
#endif
return strm.str();
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: ThreadDummyImpl::get_current_thread // Function: ThreadDummyImpl::get_current_thread
// Access: Public // Access: Public

View File

@ -46,6 +46,8 @@ public:
INLINE void join(); INLINE void join();
INLINE void preempt(); INLINE void preempt();
string get_unique_id() const;
INLINE static void prepare_for_exit(); INLINE static void prepare_for_exit();
static Thread *get_current_thread(); static Thread *get_current_thread();

View File

@ -179,6 +179,19 @@ join() {
_mutex.release(); _mutex.release();
} }
////////////////////////////////////////////////////////////////////
// Function: ThreadPosixImpl::get_unique_id
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
string ThreadPosixImpl::
get_unique_id() const {
ostringstream strm;
strm << getpid() << "." << _thread;
return strm.str();
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: ThreadPosixImpl::root_func // Function: ThreadPosixImpl::root_func
// Access: Private, Static // Access: Private, Static

View File

@ -42,6 +42,8 @@ public:
void join(); void join();
INLINE void preempt(); INLINE void preempt();
string get_unique_id() const;
INLINE static void prepare_for_exit(); INLINE static void prepare_for_exit();
INLINE static Thread *get_current_thread(); INLINE static Thread *get_current_thread();

View File

@ -22,6 +22,8 @@
ThreadSimpleImpl *volatile ThreadSimpleImpl::_st_this; ThreadSimpleImpl *volatile ThreadSimpleImpl::_st_this;
int ThreadSimpleImpl::_next_unique_id = 1;
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: ThreadSimpleImpl::Constructor // Function: ThreadSimpleImpl::Constructor
// Access: Public // Access: Public
@ -31,6 +33,9 @@ ThreadSimpleImpl::
ThreadSimpleImpl(Thread *parent_obj) : ThreadSimpleImpl(Thread *parent_obj) :
_parent_obj(parent_obj) _parent_obj(parent_obj)
{ {
_unique_id = _next_unique_id;
++_next_unique_id;
_status = S_new; _status = S_new;
_joinable = false; _joinable = false;
_time_per_epoch = 0.0; _time_per_epoch = 0.0;
@ -157,6 +162,24 @@ preempt() {
_manager->preempt(this); _manager->preempt(this);
} }
////////////////////////////////////////////////////////////////////
// Function: ThreadSimpleImpl::get_unique_id
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
string ThreadSimpleImpl::
get_unique_id() const {
ostringstream strm;
#ifdef WIN32
strm << GetCurrentProcessId();
#else
strm << getpid();
#endif
strm << "." << _unique_id;
return strm.str();
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: ThreadSimpleImpl::prepare_for_exit // Function: ThreadSimpleImpl::prepare_for_exit
// Access: Public, Static // Access: Public, Static

View File

@ -68,6 +68,8 @@ public:
void join(); void join();
void preempt(); void preempt();
string get_unique_id() const;
static void prepare_for_exit(); static void prepare_for_exit();
INLINE static Thread *get_current_thread(); INLINE static Thread *get_current_thread();
@ -98,6 +100,8 @@ private:
S_killed, S_killed,
}; };
static int _next_unique_id;
int _unique_id;
Thread *_parent_obj; Thread *_parent_obj;
bool _joinable; bool _joinable;
Status _status; Status _status;

View File

@ -134,6 +134,19 @@ join() {
_mutex.release(); _mutex.release();
} }
////////////////////////////////////////////////////////////////////
// Function: ThreadWin32Impl::get_unique_id
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
string ThreadWin32Impl::
get_unique_id() const {
ostringstream strm;
strm << GetCurrentProcessId() << "." << GetThreadId(_thread);
return strm.str();
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: ThreadWin32Impl::root_func // Function: ThreadWin32Impl::root_func
// Access: Private, Static // Access: Private, Static

View File

@ -41,6 +41,8 @@ public:
void join(); void join();
INLINE void preempt(); INLINE void preempt();
string get_unique_id() const;
INLINE static void prepare_for_exit(); INLINE static void prepare_for_exit();
INLINE static Thread *get_current_thread(); INLINE static Thread *get_current_thread();

View File

@ -210,8 +210,10 @@ store(BamCacheRecord *record) {
// We actually do the write to a temporary filename first, and then // We actually do the write to a temporary filename first, and then
// move it into place, so that no one attempts to read the file // move it into place, so that no one attempts to read the file
// while it is in the process of being written. // while it is in the process of being written.
Thread *current_thread = Thread::get_current_thread();
string extension = current_thread->get_unique_id() + string(".tmp");
Filename temp_pathname = cache_pathname; Filename temp_pathname = cache_pathname;
temp_pathname.set_extension("tmp"); temp_pathname.set_extension(extension);
temp_pathname.set_binary(); temp_pathname.set_binary();
ofstream temp_file; ofstream temp_file;