thread.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. };
  31. static thread_handle_t thdHandles[THREAD_ID_MAX]={0};
  32. int thread_start(int id, thread_fn fn, void *arg)
  33. {
  34. if(id<0 || id>=THREAD_ID_MAX || !fn) {
  35. return -1;
  36. }
  37. thread_attr_t *attr=&thdAttrs[id];
  38. attr->fn = fn;
  39. attr->arg = arg;
  40. return thread_startEx(id, attr->name, attr->fn, attr->arg, attr->stksz, attr->prio);
  41. }
  42. int thread_start2(int id)
  43. {
  44. if(id<0 || id>=THREAD_ID_MAX) {
  45. return -1;
  46. }
  47. thread_attr_t *attr=&thdAttrs[id];
  48. if(!attr->fn) {
  49. return -1;
  50. }
  51. return thread_startEx(id, attr->name, attr->fn, attr->arg, attr->stksz, attr->prio);
  52. }
  53. int thread_restart(int id)
  54. {
  55. if(id<0 || id>=THREAD_ID_MAX) {
  56. return -1;
  57. }
  58. thread_attr_t *attr=&thdAttrs[id];
  59. if(!attr->fn) {
  60. return -1;
  61. }
  62. thread_stop(id);
  63. return thread_startEx(id, attr->name, attr->fn, attr->arg, attr->stksz, attr->prio);
  64. }
  65. int thread_startEx(int id, const char *name, thread_fn fn, void *arg, int stksz, int prio)
  66. {
  67. int r;
  68. pthread_attr_t attr;
  69. thread_handle_t *h=NULL;
  70. if(id<0 || id>=THREAD_ID_MAX || !fn) {
  71. return -1;
  72. }
  73. h = &thdHandles[id];
  74. if(h->running) {
  75. return -1;
  76. }
  77. h->name = name;
  78. h->arg = arg;
  79. h->quit = 0;
  80. r = pthread_attr_init(&attr);
  81. if(r) {
  82. printf("____ thread %d attr init failed\n", id);
  83. return -1;
  84. }
  85. #ifdef MEM_SAVE
  86. #if 0
  87. h->stk = malloc(stksz);
  88. if(h->stk) {
  89. r = pthread_attr_setstack(h->stk, stksz);
  90. if(r) {
  91. printf("____ thread %d attr setstack failed\n", id);
  92. }
  93. } else
  94. #endif
  95. {
  96. r = pthread_attr_setstacksize(&attr, stksz);
  97. if(r) {
  98. printf("____ thread %d attr setstacksize %d failed\n", id, stksz);
  99. }
  100. }
  101. #endif
  102. r = pthread_create(&h->tid, &attr, fn, h);
  103. if(r==0) {
  104. h->running = 1;
  105. #if 0
  106. r = pthread_setschedprio(h->tid, prio);
  107. if(r) {
  108. printf("____ set thread prio %d failed\n", prio);
  109. }
  110. #endif
  111. }
  112. pthread_attr_destroy(&attr);
  113. return r;
  114. }
  115. int thread_stop(int id)
  116. {
  117. if(id<0 || id>=THREAD_ID_MAX) {
  118. return -1;
  119. }
  120. thread_handle_t *h=&thdHandles[id];
  121. h->quit = 1;
  122. //pthread_detach();
  123. pthread_join(h->tid, NULL);
  124. h->tid = 0;
  125. h->running = 0;
  126. h->arg = NULL;
  127. h->fn = NULL;
  128. return 0;
  129. }
  130. int thread_stop_all(void)
  131. {
  132. int i;
  133. for(i=0; i<THREAD_ID_MAX; i++) {
  134. thread_stop(i);
  135. }
  136. return 0;
  137. }
  138. int thread_is_running(int id)
  139. {
  140. if(id<0 || id>=THREAD_ID_MAX) {
  141. return 0;
  142. }
  143. thread_handle_t *h=&thdHandles[id];
  144. return h->running;
  145. }
  146. int thread_start_simp(thread_fn fn, void *arg, int stksz)
  147. {
  148. int r;
  149. pthread_t tid;
  150. pthread_attr_t attr;
  151. pthread_attr_init(&attr);
  152. pthread_attr_setstacksize(&attr, stksz);
  153. r = pthread_create(&tid, &attr, fn, arg);
  154. pthread_attr_destroy(&attr);
  155. return r;
  156. }
  157. int thread_print_stacksize(void)
  158. {
  159. int r;
  160. size_t size;
  161. pthread_attr_t attr;
  162. r = pthread_attr_init(&attr);
  163. if(r) {
  164. fprintf(stdout, "___ print stacksize, attr init failed\n");
  165. return -1;
  166. }
  167. r = pthread_attr_getstacksize(&attr, &size);
  168. if(r) {
  169. fprintf(stdout, "___ print stacksize, attr init failed\n");
  170. }
  171. else {
  172. fprintf(stdout, "_____ stacksize: %dMB\n", size/(1024*1024));
  173. }
  174. pthread_attr_destroy(&attr);
  175. return r;
  176. }