thread.c 3.9 KB

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