thread.c 4.0 KB

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