Threads.cpp
上传用户:jxpjxmjjw
上传日期:2009-12-07
资源大小:5877k
文件大小:3k
源码类别:

模拟服务器

开发平台:

Visual C++

  1. // Copyright (C) 2004 Team Python
  2. //  
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. // 
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. // GNU General Public License for more details.
  12. // 
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software 
  15. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. #include "Threads.h"
  17. #if PLATFORM == PLATFORM_WIN32
  18. /// for _beginthread
  19. #  include <process.h>
  20. /// for SetThreadPriority
  21. #  define WIN32_LEAN_AND_MEAN
  22. #  include <windows.h>
  23. #else
  24. #  include <pthread.h>
  25. #endif
  26. /// Singleton
  27. createFileSingleton( Threads );
  28. void Threads::Fork( THREADRETURN (THREADCONVENTION *child)( void * ), void * param, ThreadPriority priority ) {
  29.   //THREADTYPE thread;
  30. #if PLATFORM == PLATFORM_WIN32
  31.   HANDLE threadhandle = (HANDLE)_beginthread( child, 0, param );
  32.   SetThreadPriority( threadhandle, getPlatformPriority( priority ) );
  33.   // TODO: for some reason getthreadid gives me a runtime error
  34.   //thread = GetThreadId( threadhandle );
  35. #else
  36.   pthread_t thread;
  37.   pthread_attr_t threadattributes;
  38.   pthread_attr_init( &threadattributes );
  39.   sched_param schedparams;
  40.   schedparams.sched_priority = getPlatformPriority( priority );
  41.   pthread_attr_setschedparam( &threadattributes, &schedparams );
  42.   pthread_create( &thread, &threadattributes, child, param );
  43.   //not needed
  44.   //pthread_attr_destroy( &threadattributes );
  45. #endif
  46.   // thread isn't set on windows =S
  47.   //mThreads.insert( thread );
  48. }
  49. void Threads::setCurrentPriority( ThreadPriority priority ) {
  50. #if PLATFORM == PLATFORM_WIN32
  51.   SetThreadPriority( GetCurrentThread( ), getPlatformPriority( priority ) );
  52. #else
  53.   sched_param schedparams;
  54.   schedparams.sched_priority = getPlatformPriority( priority );
  55.   pthread_setschedparam( pthread_self( ), SCHED_OTHER, &schedparams );
  56. #endif
  57. }
  58. void Threads::closeCurrentThread( ) {
  59.   THREADTYPE thread;
  60. #if PLATFORM != PLATFORM_WIN32
  61.   thread = pthread_self( );
  62.   mThreads.erase( thread );
  63.   int ret = 0;
  64.   pthread_exit( &ret );
  65. #else
  66.   thread = GetCurrentThreadId( );
  67.   //mThreads.erase( thread );
  68.   _endthread( );
  69. #endif
  70. }
  71. //void Threads::closeAllThreads( ) {
  72. //  while( mThreads.size( ) );
  73. //}
  74. int32 Threads::getPlatformPriority( ThreadPriority &priority ) {
  75.   switch( priority ) {
  76. #if PLATFORM == PLATFORM_WIN32
  77.     case TP_IDLE:
  78.       return THREAD_PRIORITY_IDLE;
  79.     case TP_LOWER:
  80.       return THREAD_PRIORITY_LOWEST;
  81.     case TP_LOW:
  82.       return THREAD_PRIORITY_BELOW_NORMAL;
  83.     case TP_NORMAL:
  84.       return THREAD_PRIORITY_NORMAL;
  85.     case TP_HIGH:
  86.       return THREAD_PRIORITY_ABOVE_NORMAL;
  87.     case TP_HIGHER:
  88.       return THREAD_PRIORITY_HIGHEST;
  89.     case TP_REALTIME:
  90.       return THREAD_PRIORITY_TIME_CRITICAL;
  91.     default:
  92.       return THREAD_PRIORITY_NORMAL;
  93. #else
  94.     case TP_IDLE:
  95.       return 45;
  96.     case TP_LOWER:
  97.       return 51;
  98.     case TP_LOW:
  99.       return 57;
  100.     case TP_NORMAL:
  101.       return 63;
  102.     case TP_HIGH:
  103.       return 69;
  104.     case TP_HIGHER:
  105.       return 75;
  106.     case TP_REALTIME:
  107.       return 81;
  108.     default:
  109.       return 63; 
  110. #endif
  111.   }
  112. }