Android BlueDroid分析: OSI中的Thread的实现与使用分析

2024-03-04 12:18

本文主要是介绍Android BlueDroid分析: OSI中的Thread的实现与使用分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

说明

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以及reactor

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 safestruct 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中设置的,见下面的B1goto error;return ret;error:;if (ret) {fixed_queue_free(ret->work_queue, osi_free);reactor_free(ret->reactor);}osi_free(ret);return NULL;
}

thread_post

完成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.对应B4return true;
}

run_thread阻塞等待poll_wait

static void *run_thread(void *start_arg) {assert(start_arg != NULL);struct start_arg *start = start_arg;thread_t *thread = start->thread;assert(thread != NULL);if (prctl(PR_SET_NAME, (unsigned long)thread->name) == -1) {LOG_ERROR("%s unable to set thread name: %s", __func__, strerror(errno));start->error = errno;  // B1:设置thread name失败semaphore_post(start->start_sem);//B2:设置好了error变量可以让new_thread返回退出了.return NULL;}thread->tid = gettid();semaphore_post(start->start_sem); //B3: 同B2int fd = fixed_queue_get_dequeue_fd(thread->work_queue); void *context = thread->work_queue;reactor_object_t *work_queue_object = reactor_register(thread->reactor, fd, context, work_queue_read_cb, NULL);//B4:注意里面fd是dequeue的fd,因此在enqueue后actor会执行workqueue_read_cb读取queue的数据进行处理reactor_start(thread->reactor); //B5: poll_wait,等待enqueue来唤醒自己,在没有enqueue之前都是休眠,有了enqueue就是有需要执行的任务(item->func)需要执行了.
reactor_unregister(work_queue_object);// Make sure we dispatch all queued work items before exiting the thread.// This allows a caller to safely tear down by enqueuing a teardown// work item and then joining the thread.size_t count = 0;work_item_t *item = fixed_queue_try_dequeue(thread->work_queue);while (item && count <= fixed_queue_capacity(thread->work_queue)) {item->func(item->context); //取出callback函数进行执行osi_free(item);item = fixed_queue_try_dequeue(thread->work_queue);//逐个取出
++count; }if (count > fixed_queue_capacity(thread->work_queue))LOG_DEBUG("%s growing event queue on shutdown.", __func__);return NULL;
}



使用

下面以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取出后执行这个函数, 然后返回.


这篇关于Android BlueDroid分析: OSI中的Thread的实现与使用分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/773142

相关文章

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态