thread.c 5.3 KB

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