thread.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "thread.h"
  2. static thread_attr_t thdAttrs[THREAD_ID_MAX]={
  3. //name fn arg stacksize prio
  4. {"io", NULL, NULL, 8*MB, 1},
  5. {"ws", NULL, NULL, 8*MB, 1},
  6. {"web", NULL, NULL, 8*MB, 1},
  7. {"ntp", NULL, NULL, 8*MB, 1},
  8. {"snmp", NULL, NULL, 8*MB, 1},
  9. {"power", NULL, NULL, 8*MB, 1},
  10. {"sensor", NULL, NULL, 8*MB, 1},
  11. {"polling", NULL, NULL, 8*MB, 1},
  12. {"cascade", NULL, NULL, 8*MB, 1},
  13. {"casscan", NULL, NULL, 8*MB, 1},
  14. {"shmw", NULL, NULL, 8*MB, 1},
  15. {"breaker", NULL, NULL, 8*MB, 1},
  16. {"switch", NULL, NULL, 8*MB, 1},
  17. {"brkscan", NULL, NULL, 8*MB, 1},
  18. {"mqtt", NULL, NULL, 8*MB, 1},
  19. {"mail", NULL, NULL, 8*MB, 1},
  20. {"modbus_tcp", NULL, NULL, 8*MB, 1},
  21. {"board", NULL, NULL, 8*MB, 1},
  22. };
  23. static thread_handle_t thdHandles[THREAD_ID_MAX]={0};
  24. int thread_start(int id, thread_fn fn, void *arg)
  25. {
  26. if(id<0 || id>=THREAD_ID_MAX || !fn) {
  27. return -1;
  28. }
  29. thread_attr_t *attr=&thdAttrs[id];
  30. attr->fn = fn;
  31. attr->arg = arg;
  32. return thread_startEx(id, attr->name, attr->fn, attr->arg, attr->stksz, attr->prio);
  33. }
  34. int thread_start2(int id)
  35. {
  36. if(id<0 || id>=THREAD_ID_MAX) {
  37. return -1;
  38. }
  39. thread_attr_t *attr=&thdAttrs[id];
  40. if(!attr->fn) {
  41. return -1;
  42. }
  43. return thread_startEx(id, attr->name, attr->fn, attr->arg, attr->stksz, attr->prio);
  44. }
  45. int thread_restart(int id)
  46. {
  47. if(id<0 || id>=THREAD_ID_MAX) {
  48. return -1;
  49. }
  50. thread_attr_t *attr=&thdAttrs[id];
  51. if(!attr->fn) {
  52. return -1;
  53. }
  54. thread_stop(id);
  55. return thread_startEx(id, attr->name, attr->fn, attr->arg, attr->stksz, attr->prio);
  56. }
  57. int thread_startEx(int id, const char *name, thread_fn fn, void *arg, int stksz, int prio)
  58. {
  59. int r;
  60. pthread_attr_t attr;
  61. thread_handle_t *h=NULL;
  62. if(id<0 || id>=THREAD_ID_MAX || !fn) {
  63. return -1;
  64. }
  65. h = &thdHandles[id];
  66. if(h->running) {
  67. return -1;
  68. }
  69. h->name = name;
  70. h->arg = arg;
  71. h->quit = 0;
  72. r = pthread_attr_init(&attr);
  73. if(r) {
  74. printf("____ thread %d attr init failed\n", id);
  75. return -1;
  76. }
  77. #if 0
  78. h->stk = malloc(stksz);
  79. if(h->stk) {
  80. r = pthread_attr_setstack(h->stk, stksz);
  81. if(r) {
  82. printf("____ thread %d attr setstack failed\n", id);
  83. }
  84. }
  85. else {
  86. r = pthread_attr_setstacksize(&attr, stksz);
  87. if(r) {
  88. printf("____ thread %d attr setstacksize %d failed\n", id, stksz);
  89. }
  90. }
  91. #endif
  92. r = pthread_create(&h->tid, &attr, fn, h);
  93. if(r==0) {
  94. h->running = 1;
  95. #if 0
  96. r = pthread_setschedprio(h->tid, prio);
  97. if(r) {
  98. printf("____ set thread prio %d failed\n", prio);
  99. }
  100. #endif
  101. }
  102. pthread_attr_destroy(&attr);
  103. return r;
  104. }
  105. int thread_stop(int id)
  106. {
  107. if(id<0 || id>=THREAD_ID_MAX) {
  108. return -1;
  109. }
  110. thread_handle_t *h=&thdHandles[id];
  111. h->quit = 1;
  112. //pthread_detach();
  113. pthread_join(h->tid, NULL);
  114. h->tid = 0;
  115. h->running = 0;
  116. h->arg = NULL;
  117. h->fn = NULL;
  118. return 0;
  119. }
  120. int thread_stop_all(void)
  121. {
  122. int i;
  123. for(i=0; i<THREAD_ID_MAX; i++) {
  124. thread_stop(i);
  125. }
  126. return 0;
  127. }
  128. int thread_is_running(int id)
  129. {
  130. if(id<0 || id>=THREAD_ID_MAX) {
  131. return 0;
  132. }
  133. thread_handle_t *h=&thdHandles[id];
  134. return h->running;
  135. }
  136. int thread_start_simp(thread_fn fn, void *arg, int stksz)
  137. {
  138. int r;
  139. pthread_t tid;
  140. pthread_attr_t attr;
  141. pthread_attr_init(&attr);
  142. pthread_attr_setstacksize(&attr, stksz);
  143. r = pthread_create(&tid, &attr, fn, arg);
  144. pthread_attr_destroy(&attr);
  145. return r;
  146. }
  147. int thread_print_stacksize(void)
  148. {
  149. int r;
  150. size_t size;
  151. pthread_attr_t attr;
  152. r = pthread_attr_init(&attr);
  153. if(r) {
  154. printf("___ print stacksize, attr init failed\n");
  155. return -1;
  156. }
  157. r = pthread_attr_getstacksize(&attr, &size);
  158. if(r) {
  159. printf("___ print stacksize, attr init failed\n");
  160. }
  161. else {
  162. printf("_____ stacksize: %dMB\n", size/(1024*1024));
  163. }
  164. pthread_attr_destroy(&attr);
  165. return r;
  166. }