thread.c 3.8 KB

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