From 17f898413e4164b7d71e578424dbbf7af6fabda6 Mon Sep 17 00:00:00 2001 From: David Rose Date: Sat, 16 Aug 2008 01:22:18 +0000 Subject: [PATCH] uniquify cache temp filename by thread --- panda/src/pipeline/thread.I | 12 ++++++++++++ panda/src/pipeline/thread.h | 1 + panda/src/pipeline/threadDummyImpl.cxx | 23 +++++++++++++++++++++++ panda/src/pipeline/threadDummyImpl.h | 2 ++ panda/src/pipeline/threadPosixImpl.cxx | 13 +++++++++++++ panda/src/pipeline/threadPosixImpl.h | 2 ++ panda/src/pipeline/threadSimpleImpl.cxx | 23 +++++++++++++++++++++++ panda/src/pipeline/threadSimpleImpl.h | 4 ++++ panda/src/pipeline/threadWin32Impl.cxx | 13 +++++++++++++ panda/src/pipeline/threadWin32Impl.h | 2 ++ panda/src/putil/bamCache.cxx | 4 +++- 11 files changed, 98 insertions(+), 1 deletion(-) diff --git a/panda/src/pipeline/thread.I b/panda/src/pipeline/thread.I index 121b692785..af4c10fd06 100644 --- a/panda/src/pipeline/thread.I +++ b/panda/src/pipeline/thread.I @@ -107,6 +107,18 @@ get_pstats_index() const { 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 // Access: Published diff --git a/panda/src/pipeline/thread.h b/panda/src/pipeline/thread.h index 2d2daf7716..f0e90593d0 100644 --- a/panda/src/pipeline/thread.h +++ b/panda/src/pipeline/thread.h @@ -60,6 +60,7 @@ PUBLISHED: INLINE const string &get_sync_name() const; INLINE int get_pstats_index() const; + INLINE string get_unique_id() const; INLINE int get_pipeline_stage() const; void set_pipeline_stage(int pipeline_stage); diff --git a/panda/src/pipeline/threadDummyImpl.cxx b/panda/src/pipeline/threadDummyImpl.cxx index 61f430ac50..3a402a1bf0 100644 --- a/panda/src/pipeline/threadDummyImpl.cxx +++ b/panda/src/pipeline/threadDummyImpl.cxx @@ -19,6 +19,29 @@ #include "threadDummyImpl.h" #include "thread.h" +#ifdef WIN32 +#define WIN32_LEAN_AND_MEAN 1 +#include +#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 // Access: Public diff --git a/panda/src/pipeline/threadDummyImpl.h b/panda/src/pipeline/threadDummyImpl.h index 4ddc960228..4b7b19c954 100644 --- a/panda/src/pipeline/threadDummyImpl.h +++ b/panda/src/pipeline/threadDummyImpl.h @@ -46,6 +46,8 @@ public: INLINE void join(); INLINE void preempt(); + string get_unique_id() const; + INLINE static void prepare_for_exit(); static Thread *get_current_thread(); diff --git a/panda/src/pipeline/threadPosixImpl.cxx b/panda/src/pipeline/threadPosixImpl.cxx index 303ca3ee1b..1d199ca956 100644 --- a/panda/src/pipeline/threadPosixImpl.cxx +++ b/panda/src/pipeline/threadPosixImpl.cxx @@ -179,6 +179,19 @@ join() { _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 // Access: Private, Static diff --git a/panda/src/pipeline/threadPosixImpl.h b/panda/src/pipeline/threadPosixImpl.h index d0797fd3a7..7fedf46406 100644 --- a/panda/src/pipeline/threadPosixImpl.h +++ b/panda/src/pipeline/threadPosixImpl.h @@ -42,6 +42,8 @@ public: void join(); INLINE void preempt(); + string get_unique_id() const; + INLINE static void prepare_for_exit(); INLINE static Thread *get_current_thread(); diff --git a/panda/src/pipeline/threadSimpleImpl.cxx b/panda/src/pipeline/threadSimpleImpl.cxx index ce50d5770c..ff651a5019 100644 --- a/panda/src/pipeline/threadSimpleImpl.cxx +++ b/panda/src/pipeline/threadSimpleImpl.cxx @@ -22,6 +22,8 @@ ThreadSimpleImpl *volatile ThreadSimpleImpl::_st_this; +int ThreadSimpleImpl::_next_unique_id = 1; + //////////////////////////////////////////////////////////////////// // Function: ThreadSimpleImpl::Constructor // Access: Public @@ -31,6 +33,9 @@ ThreadSimpleImpl:: ThreadSimpleImpl(Thread *parent_obj) : _parent_obj(parent_obj) { + _unique_id = _next_unique_id; + ++_next_unique_id; + _status = S_new; _joinable = false; _time_per_epoch = 0.0; @@ -157,6 +162,24 @@ preempt() { _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 // Access: Public, Static diff --git a/panda/src/pipeline/threadSimpleImpl.h b/panda/src/pipeline/threadSimpleImpl.h index 88d2645cbb..df6b0d5020 100644 --- a/panda/src/pipeline/threadSimpleImpl.h +++ b/panda/src/pipeline/threadSimpleImpl.h @@ -68,6 +68,8 @@ public: void join(); void preempt(); + string get_unique_id() const; + static void prepare_for_exit(); INLINE static Thread *get_current_thread(); @@ -98,6 +100,8 @@ private: S_killed, }; + static int _next_unique_id; + int _unique_id; Thread *_parent_obj; bool _joinable; Status _status; diff --git a/panda/src/pipeline/threadWin32Impl.cxx b/panda/src/pipeline/threadWin32Impl.cxx index 08a268ba49..9d8f1d3d6b 100644 --- a/panda/src/pipeline/threadWin32Impl.cxx +++ b/panda/src/pipeline/threadWin32Impl.cxx @@ -134,6 +134,19 @@ join() { _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 // Access: Private, Static diff --git a/panda/src/pipeline/threadWin32Impl.h b/panda/src/pipeline/threadWin32Impl.h index 17969d334e..73c71e364c 100644 --- a/panda/src/pipeline/threadWin32Impl.h +++ b/panda/src/pipeline/threadWin32Impl.h @@ -41,6 +41,8 @@ public: void join(); INLINE void preempt(); + string get_unique_id() const; + INLINE static void prepare_for_exit(); INLINE static Thread *get_current_thread(); diff --git a/panda/src/putil/bamCache.cxx b/panda/src/putil/bamCache.cxx index 775a9464e9..64ed5e60ee 100644 --- a/panda/src/putil/bamCache.cxx +++ b/panda/src/putil/bamCache.cxx @@ -210,8 +210,10 @@ store(BamCacheRecord *record) { // 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 // 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; - temp_pathname.set_extension("tmp"); + temp_pathname.set_extension(extension); temp_pathname.set_binary(); ofstream temp_file;