thread.c 4.7 KB

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