| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- #include "thread.h"
- static thread_attr_t thdAttrs[THREAD_ID_MAX]={
- //name fn arg stacksize prio
- {"web", NULL, NULL, 4*KB, 20},
- {"snmp", NULL, NULL, 2*KB, 20},
- {"power", NULL, NULL, 2*KB, 20},
- {"polling", NULL, NULL, 2*KB, 20},
- {"mqtt", NULL, NULL, 2*KB, 20},
- {"mail", NULL, NULL, 2*KB, 20},
- {"ui", NULL, NULL, 3*KB, 20},
- {"cascade_scan", NULL, NULL, 1*KB, 20},
- {"cascade_thread", NULL, NULL, 2*KB, 20},
- };
- static thread_handle_t thdHandles[THREAD_ID_MAX]={0};
- static void* thread_fn(void *arg)
- {
- thread_handle_t *h=(thread_handle_t*)arg;
-
- h->running = 1;
- if(h->attr->fn) {
- h->attr->fn(h);
- }
- h->running = 0;
- h->quit = 0;
- pthread_exit(NULL);
- }
- int thread_start(int id, thread_fn_t fn, void *arg)
- {
- if(id<0 || id>=THREAD_ID_MAX || !fn) {
- return -1;
- }
- thread_attr_t attr=thdAttrs[id];
- attr.fn = fn;
- attr.arg = arg;
- return thread_startEx(id, &attr);
- }
- int thread_start2(int id)
- {
- if(id<0 || id>=THREAD_ID_MAX) {
- return -1;
- }
- thread_attr_t *attr=&thdAttrs[id];
- if(!attr->fn) {
- return -1;
- }
- return thread_startEx(id, attr);
- }
- int thread_restart(int id)
- {
- if(id<0 || id>=THREAD_ID_MAX) {
- return -1;
- }
- thread_attr_t *attr=&thdAttrs[id];
- if(!attr->fn) {
- return -1;
- }
- thread_stop(id);
- return thread_startEx(id, attr);
- }
- int thread_startEx(int id, thread_attr_t *attr)
- {
- int r=-1;
- thread_handle_t *h=NULL;
- pthread_attr_t attr2;
- if(id<0 || id>=THREAD_ID_MAX || !attr || !attr->fn) {
- return -1;
- }
- h = &thdHandles[id];
- if(h->running) {
- return -1;
- }
- h->quit = 0;
- h->attr = NULL;
- r = pthread_attr_init(&attr2);
- if(r) {
- LOGE("____ thread %d attr init failed\n", id);
- return -1;
- }
- r = pthread_attr_setstacksize(&attr2, attr->stksz);
- if(r) {
- LOGE("____ thread %d attr setstacksize %d failed\n", id, attr->stksz);
- }
-
- r = pthread_create(&h->tid, &attr2, thread_fn, h);
- if(r==0) {
- h->running = 1;
- thdAttrs[id] = *attr;
- h->attr = &thdAttrs[id];
- #if 0
- r = pthread_setschedprio(h->tid, attr->prio);
- if(r) {
- LOGE("____ set thread prio %d failed\n", prio);
- }
- #endif
- }
- pthread_attr_destroy(&attr2);
- return r;
- }
- int thread_stop(int id)
- {
- if(id<0 || id>=THREAD_ID_MAX) {
- return -1;
- }
- thread_handle_t *h=&thdHandles[id];
- h->quit = 1;
- pthread_join(h->tid, NULL);
- h->tid = 0;
-
- return 0;
- }
- int thread_stop_all(void)
- {
- int i;
- for(i=0; i<THREAD_ID_MAX; i++) {
- thread_stop(i);
- }
- return 0;
- }
- int thread_is_running(int id)
- {
- if(id<0 || id>=THREAD_ID_MAX) {
- return 0;
- }
-
- thread_handle_t *h=&thdHandles[id];
- return h->running;
- }
- int thread_start_simp(thread_fn_t fn, void *arg, int stksz)
- {
- int r;
- pthread_t tid;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setstacksize(&attr, stksz);
-
- r = pthread_create(&tid, &attr, fn, arg);
- pthread_attr_destroy(&attr);
- return r;
- }
- int thread_print_stacksize(void)
- {
- int r;
- size_t size;
- pthread_attr_t attr;
- r = pthread_attr_init(&attr);
- if(r) {
- printf("___ print stacksize, attr init failed\n");
- return -1;
- }
- r = pthread_attr_getstacksize(&attr, &size);
- if(r) {
- printf("___ print stacksize, attr init failed\n");
- }
- else {
- printf("_____ stacksize: %dMB\n", size/(1024*1024));
- }
- pthread_attr_destroy(&attr);
- return r;
- }
|