diff --git a/panda/src/downloader/Sources.pp b/panda/src/downloader/Sources.pp index d6566d317a..ea90ce9ad1 100644 --- a/panda/src/downloader/Sources.pp +++ b/panda/src/downloader/Sources.pp @@ -14,6 +14,8 @@ downloadDb.cxx downloadDb.h \ downloader.I downloader.cxx downloader.h extractor.cxx extractor.h \ hashVal.cxx hashVal.I hashVal.h \ + multiplexStream.I multiplexStream.cxx multiplexStream.h \ + multiplexStreamBuf.I multiplexStreamBuf.cxx multiplexStreamBuf.h \ patcher.cxx \ patcher.h @@ -24,12 +26,18 @@ #define IF_CRYPTO_SOURCES \ crypto_utils.cxx crypto_utils.h - #define INSTALL_HEADERS \ - downloader.h downloader.I \ - config_downloader.h zcompressor.I zcompressor.h \ - asyncUtility.h asyncUtility.I decompressor.h \ - extractor.h download_utils.h downloadDb.h downloadDb.I \ - hashVal.I hashVal.h patcher.h + #define INSTALL_HEADERS \ + asyncUtility.h asyncUtility.I \ + config_downloader.h \ + decompressor.h \ + download_utils.h downloadDb.h downloadDb.I \ + downloader.h downloader.I \ + extractor.h \ + hashVal.I hashVal.h \ + multiplexStream.I multiplexStream.h \ + multiplexStreamBuf.I multiplexStreamBuf.I \ + patcher.h \ + zcompressor.I zcompressor.h #define IGATESCAN all diff --git a/panda/src/express/multiplexStream.I b/panda/src/downloader/multiplexStream.I similarity index 100% rename from panda/src/express/multiplexStream.I rename to panda/src/downloader/multiplexStream.I diff --git a/panda/src/express/multiplexStream.cxx b/panda/src/downloader/multiplexStream.cxx similarity index 100% rename from panda/src/express/multiplexStream.cxx rename to panda/src/downloader/multiplexStream.cxx diff --git a/panda/src/express/multiplexStream.h b/panda/src/downloader/multiplexStream.h similarity index 100% rename from panda/src/express/multiplexStream.h rename to panda/src/downloader/multiplexStream.h diff --git a/panda/src/downloader/multiplexStreamBuf.I b/panda/src/downloader/multiplexStreamBuf.I new file mode 100644 index 0000000000..839ae655dd --- /dev/null +++ b/panda/src/downloader/multiplexStreamBuf.I @@ -0,0 +1,5 @@ +// Filename: multiplexStreamBuf.I +// Created by: drose (27Nov00) +// +//////////////////////////////////////////////////////////////////// + diff --git a/panda/src/express/multiplexStreamBuf.cxx b/panda/src/downloader/multiplexStreamBuf.cxx similarity index 79% rename from panda/src/express/multiplexStreamBuf.cxx rename to panda/src/downloader/multiplexStreamBuf.cxx index b7f54b68d1..d1f46841f9 100644 --- a/panda/src/express/multiplexStreamBuf.cxx +++ b/panda/src/downloader/multiplexStreamBuf.cxx @@ -108,25 +108,44 @@ MultiplexStreamBuf:: } //////////////////////////////////////////////////////////////////// -// Function: MultiplexStreamBuf::sync -// Access: Public, Virtual -// Description: Called by the system ostream implementation when the -// buffer should be flushed to output (for instance, on -// destruction). +// Function: MultiplexStreamBuf::add_output +// Access: Public +// Description: Adds the indicated output destinition to the set of +// things that will be written to when characters are +// output to the MultiplexStream. //////////////////////////////////////////////////////////////////// -int MultiplexStreamBuf:: -sync() { - streamsize n = pptr() - pbase(); +void MultiplexStreamBuf:: +add_output(MultiplexStreamBuf::BufferType buffer_type, + MultiplexStreamBuf::OutputType output_type, + ostream *out, FILE *fout, bool owns_obj) { +#ifdef HAVE_IPC + // Ensure that we have the mutex while we fiddle with the list of + // outputs. + mutex_lock m(_lock); +#endif - // We pass in false for the flush value, even though our - // transmitting ostream said to sync. This allows us to get better - // line buffering, since our transmitting ostream is often set - // unitbuf, and might call sync multiple times in one line. We - // still have an explicit flush() call to force the issue. - write_chars(pbase(), n, false); - pbump(-n); + Output o; + o._buffer_type = buffer_type; + o._output_type = output_type; + o._out = out; + o._fout = fout; + o._owns_obj = owns_obj; + _outputs.push_back(o); +} - return 0; // Return 0 for success, EOF to indicate write full. + +//////////////////////////////////////////////////////////////////// +// Function: MultiplexStreamBuf::flush +// Access: Public +// Description: Forces out all output that hasn't yet been written. +//////////////////////////////////////////////////////////////////// +void MultiplexStreamBuf:: +flush() { +#ifdef HAVE_IPC + mutex_lock m(_lock); +#endif + + write_chars("", 0, true); } //////////////////////////////////////////////////////////////////// @@ -137,6 +156,10 @@ sync() { //////////////////////////////////////////////////////////////////// int MultiplexStreamBuf:: overflow(int ch) { +#ifdef HAVE_IPC + mutex_lock m(_lock); +#endif + streamsize n = pptr() - pbase(); if (n != 0) { @@ -153,12 +176,42 @@ overflow(int ch) { return 0; } +//////////////////////////////////////////////////////////////////// +// Function: MultiplexStreamBuf::sync +// Access: Public, Virtual +// Description: Called by the system ostream implementation when the +// buffer should be flushed to output (for instance, on +// destruction). +//////////////////////////////////////////////////////////////////// +int MultiplexStreamBuf:: +sync() { +#ifdef HAVE_IPC + mutex_lock m(_lock); +#endif + + streamsize n = pptr() - pbase(); + + // We pass in false for the flush value, even though our + // transmitting ostream said to sync. This allows us to get better + // line buffering, since our transmitting ostream is often set + // unitbuf, and might call sync multiple times in one line. We + // still have an explicit flush() call to force the issue. + write_chars(pbase(), n, false); + pbump(-n); + + return 0; // Return 0 for success, EOF to indicate write full. +} + //////////////////////////////////////////////////////////////////// // Function: MultiplexStreamBuf::write_chars // Access: Private // Description: An internal function called by sync() and overflow() // to store one or more characters written to the stream // into the memory buffer. +// +// It is assumed that there is only one thread at a time +// running this code; it is the responsibility of the +// caller to grab the _lock mutex before calling this. //////////////////////////////////////////////////////////////////// void MultiplexStreamBuf:: write_chars(const char *start, int length, bool flush) { diff --git a/panda/src/express/multiplexStreamBuf.h b/panda/src/downloader/multiplexStreamBuf.h similarity index 83% rename from panda/src/express/multiplexStreamBuf.h rename to panda/src/downloader/multiplexStreamBuf.h index d67e14119a..371a9ce6cc 100644 --- a/panda/src/express/multiplexStreamBuf.h +++ b/panda/src/downloader/multiplexStreamBuf.h @@ -8,6 +8,10 @@ #include +#ifdef HAVE_IPC +#include +#endif + #include #include @@ -33,12 +37,12 @@ public: OT_system_debug, }; - INLINE void add_output(BufferType buffer_type, OutputType output_type, - ostream *out = (ostream *)NULL, - FILE *fout = (FILE *)NULL, - bool owns_obj = false); + void add_output(BufferType buffer_type, OutputType output_type, + ostream *out = (ostream *)NULL, + FILE *fout = (FILE *)NULL, + bool owns_obj = false); - INLINE void flush(); + void flush(); protected: virtual int overflow(int c); @@ -64,6 +68,10 @@ private: Outputs _outputs; string _line_buffer; + +#ifdef HAVE_IPC + mutex _lock; +#endif }; #include "multiplexStreamBuf.I" diff --git a/panda/src/express/Sources.pp b/panda/src/express/Sources.pp index b76926eb03..02c208c0f7 100644 --- a/panda/src/express/Sources.pp +++ b/panda/src/express/Sources.pp @@ -19,8 +19,6 @@ littleEndian.cxx littleEndian.h memoryUsage.I memoryUsage.cxx \ memoryUsage.h memoryUsagePointers.I memoryUsagePointers.cxx \ memoryUsagePointers.h multifile.I multifile.cxx multifile.h \ - multiplexStream.I multiplexStream.cxx multiplexStream.h \ - multiplexStreamBuf.I multiplexStreamBuf.cxx multiplexStreamBuf.h \ namable.I namable.cxx namable.h numeric_types.h patchfile.I \ patchfile.cxx patchfile.h pointerTo.I pointerTo.h referenceCount.I \ referenceCount.cxx referenceCount.h tokenBoard.I tokenBoard.h \ @@ -38,8 +36,6 @@ indent.I indent.h littleEndian.I littleEndian.h \ memoryUsage.I memoryUsage.h memoryUsagePointers.I \ memoryUsagePointers.h multifile.I multifile.h \ - multiplexStream.I multiplexStream.h \ - multiplexStreamBuf.I multiplexStreamBuf.I \ numeric_types.h \ pointerTo.I pointerTo.h referenceCount.I referenceCount.h \ tokenBoard.h trueClock.I trueClock.h typeHandle.I typeHandle.h \ diff --git a/panda/src/express/multiplexStreamBuf.I b/panda/src/express/multiplexStreamBuf.I deleted file mode 100644 index 65f3466d55..0000000000 --- a/panda/src/express/multiplexStreamBuf.I +++ /dev/null @@ -1,36 +0,0 @@ -// Filename: multiplexStreamBuf.I -// Created by: drose (27Nov00) -// -//////////////////////////////////////////////////////////////////// - - -//////////////////////////////////////////////////////////////////// -// Function: MultiplexStreamBuf::add_output -// Access: Public -// Description: Adds the indicated output destinition to the set of -// things that will be written to when characters are -// output to the MultiplexStream. -//////////////////////////////////////////////////////////////////// -INLINE void MultiplexStreamBuf:: -add_output(MultiplexStreamBuf::BufferType buffer_type, - MultiplexStreamBuf::OutputType output_type, - ostream *out, FILE *fout, bool owns_obj) { - Output o; - o._buffer_type = buffer_type; - o._output_type = output_type; - o._out = out; - o._fout = fout; - o._owns_obj = owns_obj; - _outputs.push_back(o); -} - - -//////////////////////////////////////////////////////////////////// -// Function: MultiplexStreamBuf::flush -// Access: Public -// Description: Forces out all output that hasn't yet been written. -//////////////////////////////////////////////////////////////////// -INLINE void MultiplexStreamBuf:: -flush() { - write_chars("", 0, true); -}