thread.c 4.3 KB

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