RakThread.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "RakThread.h"
  11. #include "RakAssert.h"
  12. #include "RakNetDefines.h"
  13. #include "RakSleep.h"
  14. #include "RakMemoryOverride.h"
  15. using namespace RakNet;
  16. #if defined(_WIN32)
  17. #include "WindowsIncludes.h"
  18. #include <stdio.h>
  19. #if !defined(_WIN32_WCE)
  20. #include <process.h>
  21. #endif
  22. #else
  23. #include <pthread.h>
  24. #endif
  25. #if defined(_WIN32_WCE) || defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT)
  26. int RakThread::Create( LPTHREAD_START_ROUTINE start_address, void *arglist, int priority)
  27. #elif defined(_WIN32)
  28. int RakThread::Create( unsigned __stdcall start_address( void* ), void *arglist, int priority)
  29. #else
  30. int RakThread::Create( void* start_address( void* ), void *arglist, int priority)
  31. #endif
  32. {
  33. #ifdef _WIN32
  34. HANDLE threadHandle;
  35. unsigned threadID = 0;
  36. #if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT)
  37. threadHandle = CreateThread(NULL,0,start_address,arglist,CREATE_SUSPENDED, 0);
  38. #elif defined _WIN32_WCE
  39. threadHandle = CreateThread(NULL,MAX_ALLOCA_STACK_ALLOCATION*2,start_address,arglist,0,(DWORD*)&threadID);
  40. #else
  41. threadHandle = (HANDLE) _beginthreadex( NULL, MAX_ALLOCA_STACK_ALLOCATION*2, start_address, arglist, 0, &threadID );
  42. #endif
  43. SetThreadPriority(threadHandle, priority);
  44. #if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT)
  45. ResumeThread(threadHandle);
  46. #endif
  47. if (threadHandle==0)
  48. {
  49. return 1;
  50. }
  51. else
  52. {
  53. CloseHandle( threadHandle );
  54. return 0;
  55. }
  56. #else
  57. pthread_t threadHandle;
  58. // Create thread linux
  59. pthread_attr_t attr;
  60. sched_param param;
  61. param.sched_priority=priority;
  62. pthread_attr_init( &attr );
  63. pthread_attr_setschedparam(&attr, &param);
  64. pthread_attr_setstacksize(&attr, MAX_ALLOCA_STACK_ALLOCATION*2);
  65. pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
  66. int res = pthread_create( &threadHandle, &attr, start_address, arglist );
  67. RakAssert(res==0 && "pthread_create in RakThread.cpp failed.")
  68. return res;
  69. #endif
  70. }
粤ICP备19079148号