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

相关文章

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

Python yield与yield from的简单使用方式

《Pythonyield与yieldfrom的简单使用方式》生成器通过yield定义,可在处理I/O时暂停执行并返回部分结果,待其他任务完成后继续,yieldfrom用于将一个生成器的值传递给另一... 目录python yield与yield from的使用代码结构总结Python yield与yield

Go语言使用select监听多个channel的示例详解

《Go语言使用select监听多个channel的示例详解》本文将聚焦Go并发中的一个强力工具,select,这篇文章将通过实际案例学习如何优雅地监听多个Channel,实现多任务处理、超时控制和非阻... 目录一、前言:为什么要使用select二、实战目标三、案例代码:监听两个任务结果和超时四、运行示例五

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连