You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by db...@apache.org on 2004/01/07 06:32:08 UTC

cvs commit: xml-xalan/c/samples/ThreadSafe ThreadSafe.cpp

dbertoni    2004/01/06 21:32:08

  Modified:    c/samples Makefile.in
               c/samples/ThreadSafe ThreadSafe.cpp
  Log:
  Patch for Bugzilla 24989.
  
  Revision  Changes    Path
  1.6       +12 -2     xml-xalan/c/samples/Makefile.in
  
  Index: Makefile.in
  ===================================================================
  RCS file: /home/cvs/xml-xalan/c/samples/Makefile.in,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Makefile.in	6 Jan 2004 02:41:26 -0000	1.5
  +++ Makefile.in	7 Jan 2004 05:32:08 -0000	1.6
  @@ -100,7 +100,8 @@
                     UseStylesheetParam \
                     XalanTransform \
                     XalanTransformerCallback \
  -                  XPathWrapper
  +                  XPathWrapper\
  +			ThreadSafe
   
   include $(wildcard $(XALANCROOT)/samples/version.incl \
                      $(XALANCROOT)/version.incl)
  @@ -119,7 +120,8 @@
   Samples: CompileStylesheet DocumentBuilder ExternalFunction \
            ParsedSourceWrappers SimpleTransform SerializeNodeSet SimpleXPathAPI \
            SimpleXPathCAPI StreamTransform TraceListen TransformToXercesDOM \
  -         UseStylesheetParam XalanTransform XalanTransformerCallback XPathWrapper
  +         UseStylesheetParam XalanTransform XalanTransformerCallback XPathWrapper\
  +         ThreadSafe	
   
   ApacheModuleXSLT: prepare $(XSL_LIB_DIR)/mod_xslt$(SHLIBSUFFIX) 
   
  @@ -247,6 +249,14 @@
   	$(LINK) $(XSL_BUILD_OPTIONS) $(PLATFORM_LIB_LINK_OPTIONS)  \
   	$(EXTRA_LINK_OPTIONS) $(XALAN_LIB) $(ALLLIBS) $(CXXFLAGS) $^ -o $@	
   $(XSL_OBJ_DIR)/%.o:$(SAMPLES_DIR)/XPathWrapper/%.cpp
  +	$(CC1) $(XSL_BUILD_OPTIONS) -c $(XSL_INCL) $(EXTRA_COMPILE_OPTIONS) -o $@ $<
  +
  +ThreadSafe: prepare $(XSL_BIN_DIR)/ThreadSafe
  +
  +$(XSL_BIN_DIR)/ThreadSafe: $(XSL_OBJ_DIR)/ThreadSafe.o
  +	$(LINK) $(XSL_BUILD_OPTIONS) $(PLATFORM_LIB_LINK_OPTIONS)  \
  +	$(EXTRA_LINK_OPTIONS) $(XALAN_LIB) $(ALLLIBS) $(CXXFLAGS) $^ -o $@	
  +$(XSL_OBJ_DIR)/%.o:$(SAMPLES_DIR)/ThreadSafe/%.cpp
   	$(CC1) $(XSL_BUILD_OPTIONS) -c $(XSL_INCL) $(EXTRA_COMPILE_OPTIONS) -o $@ $<
   
   prepare:
  
  
  
  1.26      +101 -14   xml-xalan/c/samples/ThreadSafe/ThreadSafe.cpp
  
  Index: ThreadSafe.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xalan/c/samples/ThreadSafe/ThreadSafe.cpp,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- ThreadSafe.cpp	6 Jan 2004 02:41:27 -0000	1.25
  +++ ThreadSafe.cpp	7 Jan 2004 05:32:08 -0000	1.26
  @@ -61,9 +61,16 @@
   
   #include <cassert>
   #include <ctime>
  +
  +#if defined(XALAN_CLASSIC_IOSTREAMS)
  +#include <fstream.h>
  +#include <iostream.h>
  +#include <strstream.h>
  +#else
   #include <fstream>
   #include <iostream>
   #include <strstream>
  +#endif
   
   
   
  @@ -76,10 +83,27 @@
   
   
   //This is here for the Windows threads.
  +#if defined(_MSC_VER)
   #define WIN32_LEAN_AND_MEAN
   #include <windows.h>
   #include <winbase.h>
   #define THREADFUNCTIONRETURN DWORD WINAPI
  +    typedef   DWORD      theThreadIDType;
  +    typedef   HANDLE     theThreadType;
  +
  +//This is here for Unix threads
  +#elif defined(XALAN_POSIX2_AVAILABLE)
  +#include <pthread.h>
  +#include <unistd.h>
  +    typedef   unsigned long     theThreadIDType;
  +    typedef   pthread_t         theThreadType;
  +
  +#else
  +//error Unsupported Platform!
  +#endif
  +
  +#define NUM_THREADS 10
  +
   
   
   
  @@ -110,7 +134,7 @@
   // beginning and end of the entire operation.
   void
   outputMessage(
  -			DWORD		id,
  +              theThreadIDType  id,
   			const char	msg[])
   {
   	ostrstream threadMsg;
  @@ -119,23 +143,36 @@
   
   	cout << threadMsg.str();
   
  +    #if defined(HPUX)
  +	threadMsg.rdbuf() -> freeze(false);
  +    #else
   	threadMsg.freeze(false);
  +    #endif
   }
   
   
  -
  +#if defined(_MSC_VER)
   THREADFUNCTIONRETURN
   theThread(LPVOID	param)
  +#elif defined(XALAN_POSIX2_AVAILABLE)
  +  void  *theThread(void   *param)
  +#endif
   {
   // This routine uses a compiled stylesheet (glbCompiledStylesheet), 
   // and a binary source tree (glbParsedSource) to perform the 
   // transformation.
   
   	int	theResult = 0;
  -
  +  
  +   #if defined(_MSC_VER)
   	const int	number = reinterpret_cast<int>(param);
  +        const theThreadIDType         theThreadID = GetCurrentThreadId();
   
  -	const DWORD		theThreadID = GetCurrentThreadId();
  +   #elif defined(XALAN_POSIX2_AVAILABLE)
  +        const int       number = *(int *)(param);
  +        const theThreadIDType         theThreadID = pthread_self();
  +
  +   #endif
   
   	outputMessage(theThreadID, "Starting ");
   
  @@ -150,7 +187,11 @@
   	const XSLTResultTarget	theResultTarget(XalanDOMString(theFormatterOut.str()));
   
   	// Unfreeze the ostrstream, so memory is returned...
  +     #if defined(HPUX)
  +        theFormatterOut.rdbuf() -> freeze(false);
  +     #else
   	theFormatterOut.freeze(false);
  +     #endif
   
   	outputMessage(theThreadID, "Transforming");
   
  @@ -167,8 +208,12 @@
   	}
   
   	outputMessage(theThreadID, "Finishing");
  -
  +  
  +  #if defined(_MSC_VER)
   	return (theResult);
  +  #elif defined(XALAN_POSIX2_AVAILABLE)
  +        return 0;
  +  #endif
   }
   
   
  @@ -179,21 +224,23 @@
   void
   doThreads(int	nThreads)
   {
  +      int   i=0;
  +      cout << endl << "Clock before starting threads: " << clock() << endl;
  +      
  +
   	XALAN_USING_STD(vector)
   
  -	vector<HANDLE>	hThreads;
  +        vector<theThreadType>   hThreads;
   
   	hThreads.reserve(nThreads);
   
  -	cout << endl << "Clock before starting threads: " << clock() << endl;
  -
  -	int		i = 0;	
  +#if defined(_MSC_VER)
   
   	for (; i < nThreads; ++i)
   	{
  -		DWORD  threadID;
  +                theThreadIDType  threadID;
   
  -		const HANDLE	hThread = CreateThread(
  +                const theThreadType  hThread = CreateThread(
   				0, 
   				4096,							// Stack size for thread.
   				theThread,						// pointer to thread function
  @@ -208,12 +255,51 @@
   
   	WaitForMultipleObjects(hThreads.size(), &hThreads[0], TRUE, INFINITE);
   
  -	cout << endl << "Clock after threads: " << clock() << endl;
  -
   	for (i = 0; i < nThreads; ++i)
   	{
   		CloseHandle(hThreads[i]);
   	}
  +
  +#elif defined(XALAN_POSIX2_AVAILABLE)
  +
  +  int result;
  +  void *thread_result;
  +  
  +  for(; i < nThreads; ++i)
  +  { 
  +   result = pthread_create(
  +                          &hThreads[i],    //thread pointer
  +                          NULL,            //thread's attribute default
  +                          theThread,       //thread function
  +                          (void *) &i      //thread function argument
  +                         );
  +  if (result != 0)
  +    {
  +      perror ("Thread creation failed");
  +      exit(EXIT_FAILURE);
  +    }
  +  }
  +
  +  cout << endl << "Waiting for threads to finish..." << endl << endl;
  +  for( i = nThreads - 1; i>=0; i-- )
  +  {
  +    result = pthread_join(
  +                           hThreads[i],
  +                           &thread_result
  +                         );
  +    if (result != 0)
  +     {
  +       perror ("Thread join failed");
  +       exit (EXIT_FAILURE);
  +     }
  +   //for UNIX debugging
  +   // cout << "Thread joined, return value: " << (char *)thread_result << endl;
  +  } 
  + 
  +#endif
  +
  +cout << endl << endl << "Clock after threads: " << clock() << endl;
  +
   }
   
   
  @@ -276,7 +362,8 @@
   						// Create and run the threads...
   						// Each thread uses the same document and 
   						// stylesheet to perform a transformation.
  -						doThreads(10);
  +                                    doThreads(NUM_THREADS);
  +
   					}
   				}
   			}
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org