myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [3073] The definition of a thread entry-point is done


From: Giuseppe Scrivano
Subject: [myserver-commit] [3073] The definition of a thread entry-point is done trought the `DEFINE_THREAD' macro.
Date: Sun, 03 May 2009 15:36:40 +0000

Revision: 3073
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=3073
Author:   gscrivano
Date:     2009-05-03 15:36:39 +0000 (Sun, 03 May 2009)
Log Message:
-----------
The definition of a thread entry-point is done trought the `DEFINE_THREAD' 
macro.

Modified Paths:
--------------
    trunk/myserver/include/base/thread/thread.h
    trunk/myserver/include/protocol/ftp/ftp.h
    trunk/myserver/include/server/clients_thread.h
    trunk/myserver/src/connections_scheduler/connections_scheduler.cpp
    trunk/myserver/src/protocol/ftp/ftp.cpp
    trunk/myserver/src/server/clients_thread.cpp
    trunk/myserver/tests/test_mutex.cpp
    trunk/myserver/tests/test_socket.cpp
    trunk/myserver/tests/test_ssl_socket.cpp
    trunk/myserver/tests/test_thread.cpp

Modified: trunk/myserver/include/base/thread/thread.h
===================================================================
--- trunk/myserver/include/base/thread/thread.h 2009-05-03 14:06:50 UTC (rev 
3072)
+++ trunk/myserver/include/base/thread/thread.h 2009-05-03 15:36:39 UTC (rev 
3073)
@@ -1,7 +1,7 @@
 /* -*- mode: c++ -*- */
 /*
 MyServer
-Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
+Copyright (C) 2002, 2003, 2004, 2006, 2007, 2009 Free Software Foundation, Inc.
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
@@ -25,9 +25,11 @@
 
 #ifdef WIN32
        typedef unsigned int ThreadID;
+#define DEFINE_THREAD(NAME,PARAM) unsigned int __stdcall NAME(void* PARAM)
 #endif
 #ifdef HAVE_PTHREAD
        typedef pthread_t ThreadID;
+#define DEFINE_THREAD(NAME,PARAM) void* NAME(void* PARAM)
 #endif
 
 class Thread

Modified: trunk/myserver/include/protocol/ftp/ftp.h
===================================================================
--- trunk/myserver/include/protocol/ftp/ftp.h   2009-05-03 14:06:50 UTC (rev 
3072)
+++ trunk/myserver/include/protocol/ftp/ftp.h   2009-05-03 15:36:39 UTC (rev 
3073)
@@ -1,7 +1,7 @@
 /* -*- mode: c++ -*- */
 /*
 MyServer
-Copyright (C) 2008 Free Software Foundation, Inc.
+Copyright (C) 2008, 2009 Free Software Foundation, Inc.
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
@@ -194,19 +194,6 @@
 
 int get_ftp_reply(int nReplyCode, std::string &sReply);
 void ftp_reply(ConnectionPtr pConnection, int nReplyCode, const std::string 
&sCustomText = "");
-
-#ifdef WIN32
-unsigned int __stdcall SendAsciiFile(void* pParam);
-unsigned int __stdcall SendImageFile(void* pParam);
-unsigned int __stdcall ReceiveAsciiFile(void* pParam);
-unsigned int __stdcall ReceiveImageFile(void* pParam);
-#elif HAVE_PTHREAD
-void* SendAsciiFile(void* pParam);
-void* SendImageFile(void* pParam);
-void* ReceiveAsciiFile(void* pParam);
-void* ReceiveImageFile(void* pParam);
-#endif //HAVE_PTHREAD
-
 void yyerror(YYLTYPE *pLoc, Ftp *pContext, const char *msg);
 
 

Modified: trunk/myserver/include/server/clients_thread.h
===================================================================
--- trunk/myserver/include/server/clients_thread.h      2009-05-03 14:06:50 UTC 
(rev 3072)
+++ trunk/myserver/include/server/clients_thread.h      2009-05-03 15:36:39 UTC 
(rev 3073)
@@ -1,7 +1,7 @@
 /* -*- mode: c++ -*- */
 /*
 MyServer
-Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software 
Foundation, Inc.
+Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software 
Foundation, Inc.
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
@@ -35,12 +35,8 @@
 {
   friend class Server;
 
-#ifdef WIN32
-  friend  unsigned int __stdcall clients_thread(void* pParam);
-#endif
-#ifdef HAVE_PTHREAD
-  friend  void* clients_thread(void* pParam);
-#endif
+  friend DEFINE_THREAD(clients_thread, pParam);
+
 public:
   enum RETURN_CODE
   {

Modified: trunk/myserver/src/connections_scheduler/connections_scheduler.cpp
===================================================================
--- trunk/myserver/src/connections_scheduler/connections_scheduler.cpp  
2009-05-03 14:06:50 UTC (rev 3072)
+++ trunk/myserver/src/connections_scheduler/connections_scheduler.cpp  
2009-05-03 15:36:39 UTC (rev 3073)
@@ -18,12 +18,7 @@
 #include <include/connections_scheduler/connections_scheduler.h>
 #include <include/server/server.h>
 
-
-#ifdef WIN32
-static unsigned int __stdcall dispatcher(void* p)
-#else
-static void* dispatcher(void* p)
-#endif
+static DEFINE_THREAD(dispatcher, p)
 {
   ConnectionsScheduler::DispatcherArg *da = 
(ConnectionsScheduler::DispatcherArg*)p;
 

Modified: trunk/myserver/src/protocol/ftp/ftp.cpp
===================================================================
--- trunk/myserver/src/protocol/ftp/ftp.cpp     2009-05-03 14:06:50 UTC (rev 
3072)
+++ trunk/myserver/src/protocol/ftp/ftp.cpp     2009-05-03 15:36:39 UTC (rev 
3073)
@@ -40,6 +40,12 @@
 #include <arpa/inet.h>
 #endif
 
+static DEFINE_THREAD(SendAsciiFile,pParam);
+static DEFINE_THREAD(SendImageFile,pParam);
+
+static DEFINE_THREAD(ReceiveAsciiFile,pParam);
+static DEFINE_THREAD(ReceiveImageFile,pParam);
+
 //////////////////////////////////////////////////////////////////////////////
 // FtpHost class
 void SetFtpHost(FtpHost &out, const FtpHost &in)
@@ -657,11 +663,7 @@
   }
 }
 
-#ifdef WIN32
-unsigned int __stdcall SendAsciiFile(void* pParam)
-#elif HAVE_PTHREAD
-void* SendAsciiFile(void* pParam)
-#endif //HAVE_PTHREAD
+static DEFINE_THREAD(SendAsciiFile, pParam)
 {
   DataConnectionWorkerThreadData *pWt = 
reinterpret_cast<DataConnectionWorkerThreadData *>(pParam);
   if ( pWt == NULL )
@@ -890,11 +892,7 @@
 
 }
 
-#ifdef WIN32
-unsigned int __stdcall SendImageFile(void* pParam)
-#elif HAVE_PTHREAD
-void* SendImageFile(void* pParam)
-#endif //HAVE_PTHREAD
+static DEFINE_THREAD(SendImageFile, pParam)
 {
   DataConnectionWorkerThreadData *pWt = 
reinterpret_cast<DataConnectionWorkerThreadData *>(pParam);
   if ( pWt == NULL )
@@ -1071,11 +1069,7 @@
 #endif
 }
 
-#ifdef WIN32
-unsigned int __stdcall ReceiveAsciiFile(void* pParam)
-#elif HAVE_PTHREAD
-void* ReceiveAsciiFile(void* pParam)
-#endif //HAVE_PTHREAD
+static DEFINE_THREAD(ReceiveAsciiFile, pParam)
 {
   DataConnectionWorkerThreadData *pWt = 
reinterpret_cast<DataConnectionWorkerThreadData *>(pParam);
   if ( pWt == NULL )
@@ -1267,11 +1261,7 @@
 #endif
 }
 
-#ifdef WIN32
-unsigned int __stdcall ReceiveImageFile(void* pParam)
-#elif HAVE_PTHREAD
-void* ReceiveImageFile(void* pParam)
-#endif //HAVE_PTHREAD
+static DEFINE_THREAD(ReceiveImageFile, pParam)
 {
   DataConnectionWorkerThreadData *pWt = 
reinterpret_cast<DataConnectionWorkerThreadData *>(pParam);
   if ( pWt == NULL )

Modified: trunk/myserver/src/server/clients_thread.cpp
===================================================================
--- trunk/myserver/src/server/clients_thread.cpp        2009-05-03 14:06:50 UTC 
(rev 3072)
+++ trunk/myserver/src/server/clients_thread.cpp        2009-05-03 15:36:39 UTC 
(rev 3073)
@@ -30,7 +30,8 @@
 #include <include/protocol/ftp/ftp.h>
 
 #ifndef WIN32
-extern "C" {
+extern "C" 
+{
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -117,16 +118,7 @@
  *class instance.
  *\param pParam Params to pass to the new thread.
  */
-#ifdef WIN32
-#define ClientsThread_TYPE int
-unsigned int __stdcall clients_thread(void* pParam)
-#endif
-
-#ifdef HAVE_PTHREAD
-#define ClientsThread_TYPE void*
-void* clients_thread(void* pParam)
-#endif
-
+DEFINE_THREAD(clients_thread, pParam)
 {
 #ifndef WIN32
   /* Block SigTerm, SigInt, and SigPipe in threads.  */

Modified: trunk/myserver/tests/test_mutex.cpp
===================================================================
--- trunk/myserver/tests/test_mutex.cpp 2009-05-03 14:06:50 UTC (rev 3072)
+++ trunk/myserver/tests/test_mutex.cpp 2009-05-03 15:36:39 UTC (rev 3073)
@@ -1,6 +1,6 @@
 /*
  MyServer
- Copyright (C) 2008 Free Software Foundation, Inc.
+ Copyright (C) 2008, 2009 Free Software Foundation, Inc.
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
@@ -37,12 +37,7 @@
   int max;
 };
 
-#ifdef WIN32
-unsigned int __stdcall test_mutex_incrementer(void* pParam)
-#endif
-#ifdef HAVE_PTHREAD
-void* test_mutex_incrementer(void* pParam)
-#endif
+static DEFINE_THREAD(test_mutex_incrementer, pParam)
 {
   TestMutexThreadArg *arg = (TestMutexThreadArg*) pParam;
 

Modified: trunk/myserver/tests/test_socket.cpp
===================================================================
--- trunk/myserver/tests/test_socket.cpp        2009-05-03 14:06:50 UTC (rev 
3072)
+++ trunk/myserver/tests/test_socket.cpp        2009-05-03 15:36:39 UTC (rev 
3073)
@@ -30,7 +30,7 @@
 
 using namespace std;
 
-void* testRecvClient ( void* );
+static DEFINE_THREAD(testRecvClient, pParam);
 
 class TestSocket : public CppUnit::TestFixture
 {
@@ -86,8 +86,10 @@
     
     CPPUNIT_ASSERT( obj->socket ( AF_INET, SOCK_STREAM, 0 ) != -1 );
     
-    CPPUNIT_ASSERT( obj->setsockopt ( SOL_SOCKET, SO_REUSEADDR, (const char*) 
&optvalReuseAddr, sizeof(optvalReuseAddr) ) != -1 );
-    
+    CPPUNIT_ASSERT( obj->setsockopt ( SOL_SOCKET, SO_REUSEADDR,
+                                      (const char*) &optvalReuseAddr,
+                                      sizeof(optvalReuseAddr) ) != -1 );
+
     // If the port is used by another program, try a few others.
       if ( ( status = obj->bind ( &sockIn, sockInLen ) ) != 0 )
       while ( ++port < 28000 )
@@ -97,45 +99,35 @@
         if ( ( status = obj->bind ( &sockIn, sockInLen ) ) == 0 )
           break;
       }
-    
+
     CPPUNIT_ASSERT( status != -1 );
-    
+
     CPPUNIT_ASSERT( obj->listen ( 1 ) != -1 );
-    
+
     CPPUNIT_ASSERT_EQUAL( Thread::create ( &tid, testRecvClient, &port ), 0 );
-    
+
     Socket s = obj->accept ( &sockIn, &sockInLen );
-    
+
     status = int ( s.getHandle ( ) );
-    
+
     if ( status < 0 )
       CPPUNIT_ASSERT( status != -1 );
-    
+
     CPPUNIT_ASSERT( s.bytesToRead ( ) >= 0 );
-    
+
     int bufLen = 8;
     char buf[bufLen];
     memset ( buf, 0, bufLen );
-    
-    if ( ( status = s.recv ( buf, 1, 0 ) ) < 0 )
-      CPPUNIT_ASSERT( status >= 0 );
-      
-    CPPUNIT_ASSERT( s.bytesToRead ( ) > 0 );
-    
-    if ( ( status = s.recv ( buf, bufLen, 0, 2 ) ) < 0 && status != -2 )
-      CPPUNIT_ASSERT( status >= 0 || status == -2 );
-      
-    CPPUNIT_ASSERT( s.bytesToRead ( ) == 0 );
-    
-    if ( ( status = s.recv ( buf, bufLen, 0, 1 ) ) < 0 && status != -2 )
-      CPPUNIT_ASSERT( status >= 0 || status == -2 );
-    
-    // no other messages, must be -1
-    if ( ( status = s.recv ( buf, bufLen, 0 ) ) != -1)
-      CPPUNIT_ASSERT( status == -1 );
-    
+
+    status = s.recv ( buf, bufLen, 0 );
+
+    s.send ("a", 1, 0);
+
+    CPPUNIT_ASSERT( !strcmp (buf, "ehlo"));
+    CPPUNIT_ASSERT( status >= 0 || status == -2 );
+
     Thread::join ( tid );
-    
+
     CPPUNIT_ASSERT( obj->close ( ) != -1 );
   }
   
@@ -159,33 +151,33 @@
 
 CPPUNIT_TEST_SUITE_REGISTRATION( TestSocket );
 
-void*
-testRecvClient ( void *arg )
+static DEFINE_THREAD(testRecvClient, pParam)
 {
   Socket *obj2 = new Socket;
-  
+
   int optvalReuseAddr = 1;
   char host[] = "localhost";
-  int port = *((int*)arg);
+  int port = *((int*)pParam);
   int status;
-  
+
   CPPUNIT_ASSERT( obj2->socket ( AF_INET, SOCK_STREAM, 0 ) != -1 );
-  
+
   CPPUNIT_ASSERT( obj2->connect ( host, port ) != -1 );
-  
+
   int bufLen = 8;
   char buf[bufLen];
   memset ( buf, 0, bufLen );
   strcpy ( buf, "ehlo" );
-  
+
   CPPUNIT_ASSERT( obj2->send ( buf, strlen ( buf ), 0 ) != -1 );
-  
-  sleep ( 2 );
-  
+
+  /* To sync.  */
+  CPPUNIT_ASSERT( obj2->recv ( buf, bufLen, 0 ) != -1 );
+
   CPPUNIT_ASSERT( obj2->shutdown ( SD_BOTH ) != -1 );
- 
+
   CPPUNIT_ASSERT( obj2->close ( ) != -1 );
-  
+
   delete obj2;
   obj2 = NULL;
 }

Modified: trunk/myserver/tests/test_ssl_socket.cpp
===================================================================
--- trunk/myserver/tests/test_ssl_socket.cpp    2009-05-03 14:06:50 UTC (rev 
3072)
+++ trunk/myserver/tests/test_ssl_socket.cpp    2009-05-03 15:36:39 UTC (rev 
3073)
@@ -82,10 +82,8 @@
 3K2VTrZpJnbMs7+i3w/ziC/cqWRVK6Rcq3bLzTXrig==\n\
 -----END CERTIFICATE-----\n";
 
+static DEFINE_THREAD(testSslRecvClient, pParam);
 
-
-void* testSslRecvClient ( void* ); //
-
 class TestSslSocket : public CppUnit::TestFixture
 {
   CPPUNIT_TEST_SUITE( TestSslSocket );
@@ -186,8 +184,12 @@
     
     char buf[32] = {0};
     
-    CPPUNIT_ASSERT( sslObj->recv ( buf, sizeof(buf), 0 ) != -1 );
+    ret = sslObj->recv ( buf, sizeof(buf), 0 );
 
+    sslObj->send ( "a", 1, 0);
+
+    CPPUNIT_ASSERT(ret != -1 );
+
     CPPUNIT_ASSERT( sslObj->close ( ) != -1 );
 
     CPPUNIT_ASSERT( obj->close ( ) != -1 );
@@ -201,14 +203,13 @@
 
 CPPUNIT_TEST_SUITE_REGISTRATION( TestSslSocket );
 
-void*
-testSslRecvClient ( void *arg )
+static DEFINE_THREAD(testSslRecvClient, pParam)
 {
   Socket *obj2 = new Socket;
   SslSocket *sslObj2 = NULL;
   int optvalReuseAddr = 1;
   char host[] = "localhost";
-  int port = *((int*)arg);
+  int port = *((int*)pParam);
   int cstatus = 0;
   MYSERVER_SOCKADDRIN sockIn = { 0 };
 
@@ -226,8 +227,13 @@
 
   char buf[] = "Works?\n";
 
-  CPPUNIT_ASSERT(sslObj2->rawSend ( buf, strlen(buf), 0 ) != -1 );
+  int ret = sslObj2->send ( buf, strlen(buf), 0 );
 
+  sslObj2->recv ( buf, 1, 0 );
+
+  CPPUNIT_ASSERT(ret != -1 );
+
+
   CPPUNIT_ASSERT( sslObj2->close ( ) != -1 );
 
   delete obj2;

Modified: trunk/myserver/tests/test_thread.cpp
===================================================================
--- trunk/myserver/tests/test_thread.cpp        2009-05-03 14:06:50 UTC (rev 
3072)
+++ trunk/myserver/tests/test_thread.cpp        2009-05-03 15:36:39 UTC (rev 
3073)
@@ -1,6 +1,6 @@
 /*
  MyServer
- Copyright (C) 2008 Free Software Foundation, Inc.
+ Copyright (C) 2008, 2009 Free Software Foundation, Inc.
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
@@ -26,15 +26,7 @@
 #include <iostream>
 using namespace std;
 
-#ifdef WIN32
-#define ClientsThread_TYPE int
-unsigned int __stdcall test_thread(void* pParam)
-#endif
-
-#ifdef HAVE_PTHREAD
-#define ClientsThread_TYPE void*
-void* test_thread(void* pParam)
-#endif
+static DEFINE_THREAD(test_thread,pParam)
 {
   int *arg = (int*) pParam;
 
@@ -43,15 +35,7 @@
   return NULL;
 }
 
-#ifdef WIN32
-#define ClientsThread_TYPE int
-unsigned int __stdcall test_terminate_thread(void* pParam)
-#endif
-
-#ifdef HAVE_PTHREAD
-#define ClientsThread_TYPE void*
-void* test_terminate_thread(void* pParam)
-#endif
+static DEFINE_THREAD(test_terminate_thread, pParam)
 {
   int *arg = (int*) pParam;
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]