thread封装了pthread, 完成的功能是, 使用fixed_queue作为workquque, 将需要被执行的thread function放入其中(enqueue), 使用thread_post来enqueue,enqueue完成后semophore发送信号给dequeue, 然后使用reactor中的epoll_wait监控到dequeue semaphore变更, 就读出queue中的数据, 而queue中的item是thread function与args, 于是运行这个function, 完成任务执行功能.
struct thread_t { bool is_joined; pthread_t pthread; pid_t tid; char name[THREAD_NAME_MAX + 1]; reactor_t *reactor; // 对dequeue semophore fd进行监听 fixed_queue_t *work_queue; //存放work_item }; struct start_arg { thread_t *thread; semaphore_t *start_sem; int error; }; typedef struct { //用于thread_post传递需要的执行的function以及function需要的args thread_fn func; void *context; } work_item_t;
thread_t *thread_new_sized(const char *name, size_t work_queue_capacity) { assert(name != NULL); assert(work_queue_capacity != 0); thread_t *ret = osi_calloc(sizeof(thread_t)); if (!ret) goto error; ret->reactor = reactor_new(); if (!ret->reactor) goto error; ret->work_queue = fixed_queue_new(work_queue_capacity); if (!ret->work_queue) goto error; // Start is on the stack, but we use a semaphore, so it's safe struct start_arg start; start.start_sem = semaphore_new(0); if (!start.start_sem) goto error; strncpy(ret->name, name, THREAD_NAME_MAX); start.thread = ret; start.error = 0; pthread_create(&ret->pthread, NULL, run_thread, &start); semaphore_wait(start.start_sem); // A1:等待run_thread执行后,且使用prctl更改名字成功才返回 等待thread_post中进行唤醒才会退出,在那之前包装pthread_create创建出来的thread function可以一直执行, 这里返回即为主线程的退出 semaphore_free(start.start_sem); if (start.error) //error值在run_pthread中设置的,见下面的B1 goto error; return ret; error:; if (ret) { fixed_queue_free(ret->work_queue, osi_free); reactor_free(ret->reactor); } osi_free(ret); return NULL; }
完成thread的main_loop函数设置
bool thread_post(thread_t *thread, thread_fn func, void *context) { assert(thread != NULL); assert(func != NULL); // TODO(sharvil): if the current thread == |thread| and we've run out // of queue space, we should abort this operation, otherwise we'll // deadlock. // Queue item is freed either when the queue itself is destroyed // or when the item is removed from the queue for dispatch. work_item_t *item = (work_item_t *)osi_malloc(sizeof(work_item_t)); if (!item) { LOG_ERROR("%s unable to allocate memory: %s", __func__, strerror(errno)); return false; } item->func = func; //设置回调函数 item->context = context; fixed_queue_enqueue(thread->work_queue, item);//A4: enqueue后dequeue的semophore会从epoll_wait中回来, 然后就会去dequeue.对应B4 return true; }
下面以hci_layer.c中的thread为例说明一下使用.
1. 使用thread_new创建thread
这里仅仅传入thread name:
thread = thread_new("hci_thread"); if (!thread) { LOG_ERROR("%s unable to create thread.", __func__); goto error; } 这里面静默创建了reactor与fixed_queue.
2. 使用thread_post注册thread需要处理的function,即enqueue item function,然后唤醒run_thread dequeue来执行
下面这个就是event_finish_startup为需要执行的函数
thread_post(thread, event_finish_startup, NULL);
这个注册的函数定义如下, 这里面可以看到调用到了HAL层的open, 即libbt-vendor.so中的open, 在switch中的"case BT_VND_OP_USERIAL_OPEN".
static void event_finish_startup(UNUSED_ATTR void *context) { LOG_INFO("%s", __func__); hal->open(); vendor->send_async_command(VENDOR_CONFIGURE_FIRMWARE, NULL); }
对于case BT_VND_OP_USERIAL_OPEN, 可以查看文件hardware/broadcom/libbt/src/bt_vendor_brcm.c中的代码, 这里面调用到了硬件操作, 例如:
case BT_VND_OP_USERIAL_OPEN: { int (*fd_array)[] = (int (*)[]) param; int fd, idx; ALOGW("--------- BT_VND_OP_USERIAL_OPEN Done ------------"); fd = userial_vendor_open((tUSERIAL_CFG *) &userial_init_cfg); //Open硬件操作 if (fd != -1) { for (idx=0; idx < CH_MAX; idx++) (*fd_array)[idx] = fd; retval = 1; } /* retval contains numbers of open fd of HCI channels */ } break; dequeue取出后执行这个函数, 然后返回.
