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集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函