| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- #include "thread.h"
- static thread_handle_t myThdHandles[THREAD_ID_MAX]={0};
- int thread_start(int id, thread_fn fn, void *arg, int stksize, int prio)
- {
- int r;
- pthread_attr_t attr;
- if(id>=THREAD_ID_MAX) {
- return -1;
- }
- thread_handle_t *h=&myThdHandles[id];
- h->arg = arg;
- h->quit = 0;
- r = pthread_attr_init(&attr);
- if(r) {
- printf("____ thread %d attr init failed\n", id);
- return -1;
- }
- #if 0
- h->stk = malloc(stksize);
- if(h->stk) {
- r = pthread_attr_setstack(h->stk, stksize);
- if(r) {
- printf("____ thread %d attr setstack failed\n", id);
- }
- }
- else {
- r = pthread_attr_setstacksize(&attr, stksize);
- if(r) {
- printf("____ thread %d attr setstacksize %d failed\n", id, stksize);
- }
- }
- #endif
-
- r = pthread_create(&h->tid, &attr, fn, h);
- #if 0
- if(r==0) {
- r = pthread_setschedprio(h->tid, prio);
- if(r) {
- printf("____ set thread prio %d failed\n", prio);
- }
- }
- #endif
- pthread_attr_destroy(&attr);
- return r;
- }
- int thread_stop(int id)
- {
- if(id>=THREAD_ID_MAX) {
- return -1;
- }
- thread_handle_t *h=&myThdHandles[id];
- h->quit = 1;
- pthread_join(h->tid, NULL);
- h->tid = 0;
- h->arg = NULL;
- h->fn = NULL;
-
- return 0;
- }
- int thread_stop_all(void)
- {
- int i;
- for(i=0; i<THREAD_ID_MAX; i++) {
- thread_stop(i);
- }
- return 0;
- }
- int thread_start_simp(thread_fn fn, void *arg, int stksize)
- {
- int r;
- pthread_t tid;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setstacksize(&attr, stksize);
-
- 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/MB);
- }
- pthread_attr_destroy(&attr);
- return r;
- }
|