swthrd.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /************************************************************************
  2. * AUTHOR: NiuJiuRu
  3. * FILENAME: swthrd.c
  4. * DESCRIPTION: 线程操作
  5. * NOTE:
  6. * HISTORY:
  7. * 1, [2010-12-18] created by NiuJiuRu
  8. * 2, [2014-05-13] 修改"sw_thrd_delay()"函数, 当延时时间为负数时, 线程将
  9. * 无限等待; 修改"ThrdProc()"函数,去除不必要的信号检测函
  10. * 数, 提高用户回调函数的调用性能.
  11. ***********************************************************************/
  12. #include "swapi.h"
  13. #include "swstring.h"
  14. #include "swsignal.h"
  15. #include "swmem.h"
  16. #include "swthrd.h"
  17. // 线程的调度策略(1 - "SCHED_FIFO"实时调度策略,先到先服务; 2 - "SCHED_RR"实时调度策略,时间片轮转)
  18. #define MY_SCHEDPOLICY SCHED_RR
  19. typedef struct
  20. {
  21. // 线程的名称
  22. char name[32];
  23. // 线程的ID号
  24. pthread_t nThrdID;
  25. // 线程的控制事件
  26. void *hCtlEvent;
  27. // 线程的退出信号灯
  28. void *hEndSignal;
  29. // 线程的(用户)回调函数
  30. PThrdProc proc;
  31. unsigned long wParam;
  32. unsigned long lParam;
  33. // 线程是否暂停运行
  34. volatile bool bPause;
  35. // 线程是否正在执行(用户)回调函数还没有返回
  36. volatile bool bBusy;
  37. // 线程是否已退出
  38. volatile bool bQuit;
  39. } SThrdInfo;
  40. // 匿名线程计数器
  41. static int s_nameless_thrd_counter = 0;
  42. // 线程的执行函数
  43. static void ThrdProc(void *lpParameter);
  44. /* 创建线程, 成功后默认处于暂停状态 */
  45. void *sw_thrd_create(const char *name, int priority, int stack_size, PThrdProc proc,
  46. unsigned long wParam, unsigned long lParam)
  47. {
  48. SThrdInfo *pInfo = NULL;
  49. pthread_attr_t attr; // 线程的属性对象
  50. int policy = 0; // 调度策略
  51. pInfo = (SThrdInfo *)sw_heap_malloc(sizeof(SThrdInfo));
  52. if(!pInfo) goto ErrorP;
  53. memset(pInfo, 0, sizeof(SThrdInfo));
  54. // 设置线程的名称
  55. if(name) strncpy(pInfo->name, name, sizeof(pInfo->name)-1);
  56. else snprintf(pInfo->name, sizeof(pInfo->name)-1, "nameless%d thread", s_nameless_thrd_counter++);
  57. // 产生线程的控制事件
  58. pInfo->hCtlEvent = sw_signal_create();
  59. if(!pInfo->hCtlEvent) goto ErrorP;
  60. // 创建线程的退出信号灯
  61. pInfo->hEndSignal = sw_signal_create();
  62. if(!pInfo->hEndSignal) goto ErrorP;
  63. // 设置线程的(用户)回调函数
  64. pInfo->proc = proc;
  65. pInfo->wParam = wParam;
  66. pInfo->lParam = lParam;
  67. // 初始化线程属性对象
  68. pthread_attr_init(&attr);
  69. // 设置线程的调度策略
  70. if(priority > 0)
  71. {
  72. pthread_attr_getschedpolicy(&attr, &policy);
  73. if(policy != MY_SCHEDPOLICY)
  74. {
  75. policy = MY_SCHEDPOLICY;
  76. if(pthread_attr_setschedpolicy(&attr, policy) != 0) goto ErrorP;
  77. }
  78. }
  79. else pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); // 绑定的, 此线程将与系统中的所有线程进行竞争
  80. // 堆栈大小
  81. if(pthread_attr_setstacksize(&attr, stack_size) != 0) goto ErrorP;
  82. // 创建线程, 成功后线程默认处于暂停状态
  83. pInfo->bPause = true; pInfo->bBusy = false; pInfo->bQuit = false;
  84. if(pthread_create(&pInfo->nThrdID, &attr, (void *)ThrdProc, (void *)pInfo) != 0) goto ErrorP;
  85. // 销毁线程属性对象
  86. pthread_attr_destroy(&attr);
  87. // 设置线程的优先级
  88. sw_thrd_setPriority(pInfo, priority);
  89. // 等待线程状态稳定
  90. sw_thrd_delay(THRDWAIT_DELAY);
  91. return pInfo;
  92. ErrorP:
  93. pthread_attr_destroy(&attr); // 销毁线程属性对象
  94. sw_thrd_destroy(pInfo, 0);
  95. return NULL;
  96. }
  97. /* 销毁线程, 在"wait time <= timout(ms)"前尝试安全的关闭线程, "wait time > timout"后则强制关闭 */
  98. void sw_thrd_destroy(void *hThrd, int timeout)
  99. {
  100. SThrdInfo *pInfo = (SThrdInfo *)hThrd;
  101. if(!pInfo) return;
  102. if(pInfo->nThrdID != 0)
  103. {
  104. pInfo->bQuit = true;
  105. sw_thrd_resume(pInfo);
  106. if(sw_signal_wait(pInfo->hEndSignal, timeout) == 0) ; // 尝试安全的关闭线程
  107. else if(pthread_cancel(pInfo->nThrdID) != 0) // 强制关闭线程
  108. { sw_log_error("terminate %s error(errno=%d)!!", pInfo->name, errno); }
  109. pthread_join(pInfo->nThrdID, NULL);
  110. pInfo->nThrdID = 0;
  111. }
  112. if(pInfo->hCtlEvent) { sw_signal_destroy(pInfo->hCtlEvent); pInfo->hCtlEvent = NULL; }
  113. if(pInfo->hEndSignal) { sw_signal_destroy(pInfo->hEndSignal); pInfo->hEndSignal = NULL; }
  114. sw_heap_free(pInfo);
  115. }
  116. /* 暂停运行线程 */
  117. void sw_thrd_pause(void *hThrd)
  118. {
  119. SThrdInfo *pInfo = (SThrdInfo *)hThrd;
  120. if(!pInfo) return;
  121. while(sw_signal_wait(pInfo->hCtlEvent, 0) == 0) sw_thrd_delay(1);
  122. pInfo->bPause = true;
  123. }
  124. /* 继续运行线程 */
  125. void sw_thrd_resume(void *hThrd)
  126. {
  127. SThrdInfo *pInfo = (SThrdInfo *)hThrd;
  128. if(!pInfo) return;
  129. pInfo->bPause = false;
  130. sw_signal_give(pInfo->hCtlEvent);
  131. }
  132. /* 延时(毫秒) */
  133. void sw_thrd_delay(int ms)
  134. {
  135. if(ms < 0) select(0, NULL, NULL, NULL, NULL);
  136. else
  137. {
  138. struct timeval tv = { 0 };
  139. tv.tv_sec = ms/1000;
  140. tv.tv_usec = (ms%1000)*1000;
  141. select(0, NULL, NULL, NULL, &tv);
  142. }
  143. }
  144. /* 检查线程是否还存在 */
  145. bool sw_thrd_isAlive(void *hThrd)
  146. {
  147. SThrdInfo *pInfo = (SThrdInfo *)hThrd;
  148. if(!pInfo) return false;
  149. return (pInfo->nThrdID != 0 && pInfo->bQuit == false);
  150. }
  151. /* 检查线程是否正在运行: true = 是, false = 否 */
  152. bool sw_thrd_isRunning(void *hThrd)
  153. {
  154. SThrdInfo *pInfo = (SThrdInfo *)hThrd;
  155. if(!pInfo) return false;
  156. return (pInfo->nThrdID != 0 && pInfo->bQuit == false && pInfo->bPause == false);
  157. }
  158. /* 检查线程是否正在执行(用户)回调函数还没有返回: true = 是, false = 否 */
  159. bool sw_thrd_isBusy(void *hThrd)
  160. {
  161. SThrdInfo *pInfo = (SThrdInfo *)hThrd;
  162. if(!pInfo) return false;
  163. return pInfo->bBusy;
  164. }
  165. /* 设置线程名称 */
  166. bool sw_thrd_setName(void *hThrd, const char *name)
  167. {
  168. SThrdInfo *pInfo = (SThrdInfo *)hThrd;
  169. if(!pInfo || !name) return false;
  170. memset(pInfo->name, 0, sizeof(pInfo->name));
  171. strncpy(pInfo->name, name, sizeof(pInfo->name)-1);
  172. return true;
  173. }
  174. /* 取得线程名称 */
  175. char *sw_thrd_getName(void *hThrd, char *name)
  176. {
  177. SThrdInfo *pInfo = (SThrdInfo *)hThrd;
  178. if(!pInfo || !name) return NULL;
  179. strcpy(name, pInfo->name);
  180. return name;
  181. }
  182. /* 设置线程优先级 */
  183. bool sw_thrd_setPriority(void *hThrd, int priority)
  184. {
  185. SThrdInfo *pInfo = (SThrdInfo *)hThrd;
  186. struct sched_param sparam = { 0 };
  187. if(!pInfo) return false;
  188. sparam.__sched_priority = priority;
  189. if(pthread_setschedparam(pInfo->nThrdID, MY_SCHEDPOLICY, &sparam) == 0) return true;
  190. else return false;
  191. }
  192. /* 取得线程优先级 */
  193. int sw_thrd_getPriority(void *hThrd)
  194. {
  195. SThrdInfo *pInfo = (SThrdInfo *)hThrd;
  196. struct sched_param sparam = { 0 };
  197. int policy;
  198. if(!pInfo) return -1;
  199. if(pthread_getschedparam(pInfo->nThrdID, &policy, &sparam) == 0) return sparam.__sched_priority;
  200. else return -1;
  201. }
  202. /* 设置线程执行的(用户)回调函数 */
  203. bool sw_thrd_setProc(void *hThrd, PThrdProc proc, unsigned long wParam, unsigned long lParam)
  204. {
  205. SThrdInfo *pInfo = (SThrdInfo *)hThrd;
  206. if(!pInfo) return false;
  207. pInfo->proc = proc;
  208. pInfo->wParam = wParam;
  209. pInfo->lParam = lParam;
  210. return true;
  211. }
  212. /* 取得线程执行的(用户)回调函数 */
  213. void sw_thrd_getProc(void *hThrd, PThrdProc *proc, unsigned long *wParam, unsigned long *lParam)
  214. {
  215. SThrdInfo *pInfo = (SThrdInfo *)hThrd;
  216. if(!pInfo) return;
  217. if(proc) *proc = pInfo->proc;
  218. if(wParam) *wParam = pInfo->wParam;
  219. if(lParam) *lParam = pInfo->lParam;
  220. }
  221. // 线程的执行函数
  222. static void ThrdProc(void *lpParameter)
  223. {
  224. SThrdInfo *pInfo = (SThrdInfo *)lpParameter;
  225. int delay = 0;
  226. while(pInfo->bQuit == false)
  227. {
  228. if(pInfo->bPause) sw_signal_wait(pInfo->hCtlEvent, WAIT_FOREVER);
  229. if(pInfo->proc)
  230. {
  231. pInfo->bBusy = true;
  232. delay = pInfo->proc(pInfo->wParam, pInfo->lParam);
  233. pInfo->bBusy = false;
  234. }
  235. else delay = 1;
  236. if(delay < 0) pInfo->bQuit = true;
  237. else if(delay > 0) sw_signal_wait(pInfo->hCtlEvent, delay);
  238. }
  239. pInfo->proc = NULL;
  240. pInfo->wParam = pInfo->lParam = 0;
  241. sw_signal_give(pInfo->hEndSignal); // 点亮"退出信号灯", 表明线程已正常结束
  242. pthread_exit("thread normal exit.");
  243. }