swthrd.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /************************************************************************
  2. * AUTHOR: NiuJiuRu
  3. * FILENAME: swthrd.h
  4. * DESCRIPTION: 线程操作
  5. * NOTE:
  6. * HISTORY:
  7. * 1, [2010-09-07] created by NiuJiuRu
  8. ***********************************************************************/
  9. #ifndef __SWTHRD_H__
  10. #define __SWTHRD_H__
  11. // thread's default satck size
  12. #define THREAD_DEFAULT_STACK_SIZE 65536
  13. // thread's default priority
  14. #define THREAD_DEFAULT_PRIORITY 50
  15. #ifdef __cplusplus
  16. extern "C"
  17. {
  18. #endif
  19. /* 线程的(用户)回调函数, 会被反复调用("返回值 < 0"时表示退出线程; "返回值 >= 0"表示延时一段时间(ms)后继续运行) */
  20. typedef int (*PThrdProc)(unsigned long wParam, unsigned long lParam);
  21. /* 创建线程, 成功后默认处于暂停状态 */
  22. void *sw_thrd_create(const char *name, int priority, int stack_size, PThrdProc proc,
  23. unsigned long wParam, unsigned long lParam);
  24. /* 销毁线程, 在"wait time <= timout(ms)"前尝试安全的关闭线程, "wait time > timout"后则强制关闭 */
  25. void sw_thrd_destroy(void *hThrd, int timeout);
  26. /* 暂停运行线程 */
  27. void sw_thrd_pause(void *hThrd);
  28. /* 继续运行线程 */
  29. void sw_thrd_resume(void *hThrd);
  30. /* 延时(毫秒) */
  31. void sw_thrd_delay(int ms);
  32. /* 检查线程是否还存在 */
  33. bool sw_thrd_isAlive(void *hThrd);
  34. /* 检查线程是否正在运行: true = 是, false = 否 */
  35. bool sw_thrd_isRunning(void *hThrd);
  36. /* 检查线程是否正在执行(用户)回调函数还没有返回: true = 是, false = 否 */
  37. bool sw_thrd_isBusy(void *hThrd);
  38. /* 设置线程名称 */
  39. bool sw_thrd_setName(void *hThrd, const char *name);
  40. /* 取得线程名称 */
  41. char *sw_thrd_getName(void *hThrd, char *name);
  42. /* 设置线程优先级 */
  43. bool sw_thrd_setPriority(void *hThrd, int priority);
  44. /* 取得线程优先级 */
  45. int sw_thrd_getPriority(void *hThrd);
  46. /* 设置线程执行的(用户)回调函数 */
  47. bool sw_thrd_setProc(void *hThrd, PThrdProc proc, unsigned long wParam, unsigned long lParam);
  48. /* 取得线程执行的(用户)回调函数 */
  49. void sw_thrd_getProc(void *hThrd, PThrdProc *proc, unsigned long *wParam, unsigned long *lParam);
  50. #ifdef __cplusplus
  51. }
  52. #endif
  53. #endif /* __SWTHRD_H__ */