thread.c 5.4 KB

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