thread.c 4.5 KB

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