pthread_thread.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // pthread_thread.cpp
  3. // threadpp
  4. //
  5. // Created by Melo Yao on 1/15/15.
  6. // Copyright (c) 2015 Melo Yao. All rights reserved.
  7. //
  8. #ifndef __threadpp__pthread_thread__hpp__
  9. #define __threadpp__pthread_thread__hpp__
  10. #include <errno.h>
  11. #include "../threadpp_assert.h"
  12. #include <cstring>
  13. #include <cmath>
  14. #include <unistd.h>
  15. namespace threadpp
  16. {
  17. inline pthread_thread::id_type pthread_thread::null_id()
  18. {
  19. return 0;
  20. }
  21. inline void* pthread_thread::pthread_fp_delegate(void* ctx)
  22. {
  23. pthread_thread::pthread_context* pctx =static_cast<pthread_thread::pthread_context*>(ctx);
  24. pctx->fp(pctx->context);
  25. return NULL;
  26. }
  27. inline pthread_thread::pthread_thread(runnable r,void* t)
  28. {
  29. _context.fp = r;
  30. _context.context = t;
  31. int code = pthread_create(&_thread, NULL, pthread_thread::pthread_fp_delegate, &_context);
  32. ASSERT(code==0,"create thread failed,error:%s",strerror(code));
  33. }
  34. // pthread_thread::pthread_thread(runnable r,void* t,float priority)
  35. // {
  36. // _context.fp = r;
  37. // _context.context = t;
  38. // pthread_attr_t tattr;
  39. // pthread_attr_init(&tattr);
  40. // struct sched_param schp;
  41. // int policy = SCHED_FIFO;
  42. // pthread_attr_getschedpolicy(&tattr, &policy);
  43. // pthread_attr_getschedparam(&tattr, &schp);
  44. // float pr =fminf(1.0f,fmaxf(0.0f, priority));
  45. // schp.sched_priority = sched_get_priority_min(policy) + pr*(sched_get_priority_max(policy) - sched_get_priority_min(policy));
  46. // pthread_attr_setschedparam(&tattr, &schp);
  47. // int code = pthread_create(&_thread, &tattr, pthread_thread::pthread_fp_delegate, &_context);
  48. // ASSERT(code==0,"create thread failed,error:%s",strerror(code));
  49. // pthread_attr_destroy(&tattr);
  50. // }
  51. inline pthread_thread::~pthread_thread()
  52. {
  53. ASSERT(_thread == 0,"%s","must join or detach a thread before destructing it");
  54. }
  55. inline void pthread_thread::join()
  56. {
  57. void* ret = NULL;
  58. int code = pthread_join(_thread, &ret);
  59. _thread = 0;
  60. ASSERT(code==0,"join thread failed,error:%s",strerror(code));
  61. }
  62. inline void pthread_thread::detach()
  63. {
  64. int code = pthread_detach(_thread);
  65. _thread = 0;
  66. ASSERT(code==0,"join thread failed,error:%s",strerror(code));
  67. }
  68. inline bool pthread_thread::is_equal(const pthread_thread& t) const
  69. {
  70. return pthread_equal(_thread, t._thread);
  71. }
  72. inline void pthread_thread::sleep(unsigned long millisecs)
  73. {
  74. usleep((useconds_t)millisecs*1000);
  75. }
  76. inline pthread_thread::id_type pthread_thread::get_id() const
  77. {
  78. #ifdef __APPLE__
  79. __uint64_t tid;
  80. return pthread_threadid_np(_thread, &tid);
  81. return tid;
  82. #else
  83. return (unsigned long long)(_thread);
  84. #endif
  85. }
  86. inline pthread_thread::id_type pthread_thread::current_thread_id()
  87. {
  88. #ifdef __APPLE__
  89. __uint64_t tid;
  90. pthread_threadid_np(pthread_self(), &tid);
  91. return tid;
  92. #else
  93. return (unsigned long long)(pthread_self());
  94. #endif
  95. }
  96. }
  97. #endif