liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | /************************************************************* |
| 2 | Description: |
| 3 | mbtk_task.c |
| 4 | Used to implement mobiletek standard task or thread interfaces |
| 5 | Author: |
| 6 | YangDagang |
| 7 | Date: |
| 8 | 2019-7-13 |
| 9 | *************************************************************/ |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <errno.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <pthread.h> |
| 18 | #include "mbtk_type.h" |
| 19 | #include "mbtk_queue.h" |
| 20 | #include "mbtk_task.h" |
| 21 | #include "mbtk_log.h" |
| 22 | |
| 23 | /************************************************************* |
| 24 | Constants and Macros |
| 25 | *************************************************************/ |
| 26 | |
| 27 | /************************************************************* |
| 28 | Definitions:enum,struct,union |
| 29 | *************************************************************/ |
| 30 | |
| 31 | |
| 32 | /************************************************************* |
| 33 | Variables:local,extern |
| 34 | *************************************************************/ |
| 35 | |
| 36 | /************************************************************* |
| 37 | Local Function Declaration |
| 38 | *************************************************************/ |
| 39 | |
| 40 | /************************************************************* |
| 41 | Extern Function Declaration |
| 42 | *************************************************************/ |
| 43 | |
| 44 | /************************************************************* |
| 45 | Function Definitions |
| 46 | *************************************************************/ |
| 47 | void mbtk_mutex_init(mbtk_mutex *mutex) |
| 48 | { |
| 49 | pthread_mutex_init(&mutex->crit_sect, NULL); |
| 50 | } |
| 51 | |
| 52 | void mbtk_mutex_deinit(mbtk_mutex *mutex) |
| 53 | { |
| 54 | pthread_mutex_destroy(&mutex->crit_sect); |
| 55 | } |
| 56 | |
| 57 | void mbtk_mutex_lock(mbtk_mutex *mutex) |
| 58 | { |
| 59 | pthread_mutex_lock(&mutex->crit_sect); |
| 60 | } |
| 61 | |
| 62 | void mbtk_mutex_unlock(mbtk_mutex *mutex) |
| 63 | { |
| 64 | pthread_mutex_unlock(&mutex->crit_sect); |
| 65 | } |
| 66 | |
| 67 | int mbtk_task_start(mbtk_task_info *task) |
| 68 | { |
| 69 | static pthread_attr_t thread_attr; |
| 70 | pthread_attr_init(&thread_attr); |
| 71 | if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) |
| 72 | { |
| 73 | LOGE("pthread_attr_setdetachstate() fail."); |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | if (pthread_create((pthread_t*)task->task_id, |
| 78 | &thread_attr, task->thread_run, task->args) != 0) |
| 79 | { |
| 80 | LOGE("%s errno: %d (%s)",__func__, errno, strerror(errno)); |
| 81 | return -1; |
| 82 | } |
| 83 | pthread_attr_destroy(&thread_attr); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int mbtk_task_queue_start(void *param,mbtk_task_cb_handle cb) |
| 89 | { |
| 90 | int res = 0; |
| 91 | mbtk_task_queue_info *ts = (mbtk_task_queue_info *)param; |
| 92 | |
| 93 | if(cb == NULL) |
| 94 | { |
| 95 | return MBTK_FAILE; |
| 96 | } |
| 97 | mbtk_queue_init(&ts->queue); |
| 98 | mbtk_mutex_init(&ts->mutex); |
| 99 | pthread_cond_init(&ts->cond, NULL); |
| 100 | res = pthread_create(&ts->thread_id,NULL,cb,NULL); |
| 101 | usleep(500000); |
| 102 | return res; |
| 103 | } |
| 104 | |
| 105 | void mbtk_task_queue_stop(void *param) |
| 106 | { |
| 107 | mbtk_task_queue_info *ts = (mbtk_task_queue_info *)param; |
| 108 | pthread_cancel(ts->thread_id); |
| 109 | } |
| 110 | |
| 111 | int mbtk_signal_send(void *param,mbtk_signal_info* info) |
| 112 | { |
| 113 | int res = 0; |
| 114 | mbtk_task_queue_info *ts = (mbtk_task_queue_info *)param; |
| 115 | |
| 116 | mbtk_mutex_lock(&ts->mutex); |
| 117 | res = mbtk_queue_put(&ts->queue,(void*)info); |
| 118 | pthread_cond_signal(&ts->cond); |
| 119 | mbtk_mutex_unlock(&ts->mutex); |
| 120 | return res; |
| 121 | } |
| 122 | |
| 123 | mbtk_signal_info *mbtk_signal_get(void *param) |
| 124 | { |
| 125 | void *res = NULL; |
| 126 | mbtk_task_queue_info *ts = (mbtk_task_queue_info *)param; |
| 127 | |
| 128 | mbtk_mutex_lock(&ts->mutex); |
| 129 | res = mbtk_queue_get(&ts->queue); |
| 130 | if(res == NULL){ |
| 131 | // extern int pthread_cond_wait (pthread_cond_t *__restrict __cond, |
| 132 | // pthread_mutex_t *__restrict __mutex) |
| 133 | pthread_cond_wait(&ts->cond, (pthread_mutex_t*)(&ts->mutex)); |
| 134 | res = mbtk_queue_get(&ts->queue); |
| 135 | } |
| 136 | mbtk_mutex_unlock(&ts->mutex); |
| 137 | return res; |
| 138 | } |
| 139 | |