9.5 Binder系统_驱动情景分析_transaction_stack机制

参考文章:
http://www.cnblogs.com/samchen2009/p/3316001.html

test_server服务进程可能有多个线程,而在发送数据的时候handle只表示了那个进程

(1)发给谁?handle只表示了那个进程,数据是发给进程还是某个线程

一般数据放在binder_proc的todo链表,其会唤醒等待与binder_proc.wait上的空闲线程;

对于双向传输,则放在binder_thread.todo链表上,然后唤醒该线程(用transaction_stack来判断是否是双向传输)

(2)回复给谁?没有handle表示目的进程,必定在某个地方记录之前的发送者

某个地方就是transaction_stack

test_client步骤分析:

(1)发送BC_TRANSACTION类型数据

 a、单进程也会有一个binder_thread,其在调用ioctl的时候被创建,其thread->transaction_stack开始为空,表示非双向传输。

b、入栈 test_client里面的binder_thread.transaction_stack结构,其指向的就是binder_transaction结构体,binder_transaction结构体的成员from_parent = transaction_stack,刚开始为null、from=test_client、to_proc=test_server、to_thread = thread(test_server的某个线程,单线程的时候就是test_server)

c、数据将放在test_server的binder_proc.todo链表中;唤醒test_server的binder_proc.wait上的线程

(4)接受BR_REPLY类型数据

唤醒后返回给用户空间,不涉及栈

test_server步骤分析:

(2)接受BR_TRANSACTION类型数据

在binder_thread_read函数中做如下事情:

a、从test_server的binder_proc.todo链表中取出数据处理

b、入栈 test_server里面的binder_thread.transaction_stack结构,其指向的就是binder_transaction结构体,binder_transaction结构体的成员to_parent = transaction_stack,刚开始为null、from=test_client、to_proc=test_server、to_thread = thread((test_server的某个线程,单线程的时候就是test_server))

(这里的binder_transaction和test_client中的binder_transaction是一个,通过from_parent放入发送者的栈,通过to_parent放入接受者的栈,transaction_stack的字面意思就是传输栈)

(3)发送BC_REPLY类型数据

a、发送给谁:从栈中取出binder_transaction,其中from=test_client、to_proc=test_server、to_thread = thread,可知要回复给前面的发送者test_client

b、出栈, test_server里面的binder_thread.transaction_stack结构等于in_reliy_to->to_parent,其为NULL,即transaction_stack等于NULL

c、数据copy_from_user进内核态传给test_client

d、出栈, test_client里面的binder_thread.transaction_stack结构等于transaction_stack->from_parent,其为NULL,即transaction_stack等于NULL

e、放入todo链表,唤醒

在双向传输过程中transaction_stack的作用

P1进程提供S1服务,其有线程t1、t11

P2进程提供S2服务,其有线程t2、t22

P3进程提供S3服务,其有线程t3、t33

假设:t1线程要使用S2服务,它会给t2线程发送数据,t2线程在处理过程中要用到S3服务,它会给t2线程发送数据,如果t3线程又要用到S1服务,这个时候t3是给t1还是新建个线程t11发送数据呢

如果新建一个线程,那么只要调用S1服务就新建一次,这样太浪费资源了,所以t3是给t1发送数据,这就是双向传输

即:P1的t1发送服务请求后休眠,在服务处理过程中又用到S1服务,会发送服务请求给P1的t1来处理S1服务

情景分析:

一、t1传送binder_transaction1结构体给t2:

(1)t1发送BC_TRANSACTION

  t1.sp(栈).binder_transaction1.form_parent = NULL

  binder_transaction1.from = t1;

  binder_transaction1.to_proc = P2;

  binder_transaction1.from_parent = NULL;

(4)收到t3的BR_TRANSACTION 

  t1.sp(栈).binder_transaction3.to_parent = binder_transaction1

(5)t1发出BC_REPLY

  出栈:t1.sp(栈).binder_transaction1.form_parent = NULL

  出栈:t3.sp(栈).binder_transaction2.to_parent =NULL

(8)t1收到t2的BR_REPLY 处理完毕

二、t2接受t1的binder_transaction1,然后发送binder_transaction2给t2

(2)t2接受BR_TRANSACTION

  t2.sp(栈).binder_transaction1.to_parent = NULL

 t2发送BC_TRANSACTION 

  t2.sp(栈).binder_transaction2.from_parent = binder_transaction1

  binder_transaction2.from = t2;

  binder_transaction2.to_proc = P3;

  binder_transaction2.from_parent = binder_transaction1;

(7)t2收到t3的BR_REPLY

 处理binder_transaction2,从t3.sp中取出binder_transaction1(栈顶),其成员from为t1,所以发出BC_REPLY给t1

  出栈:t2.sp(栈) = NULL

  出栈:t1.sp(栈) =NULL

(三)t3接受t2的binder_transaction2,然后发送binder_transaction3给t1(经过一窜代码分析出来是发给t1,不是发给P1,见图)

(3)t3接受BR_TRANSACTION

  t3.sp(栈).binder_transaction2.to_parent = NULL

 t3发送BC_TRANSACTION 

  t3.sp(栈).binder_transaction3.from_parent = binder_transaction2

  binder_transaction3.from = t3;

  binder_transaction3.to_proc = P1;

  binder_transaction3.to_thread= t1;

(6)t3收到t1发送的BR_REPLY

 处理binder_transaction1,从t2.sp中取出binder_transaction2(栈顶),其成员from为t2,所以发出BC_REPLY给t2

  出栈:t3.sp(栈) = NULL

  出栈:t2.sp(栈).binder_transaction1.to_parent =NULL

原文地址:https://www.cnblogs.com/liusiluandzhangkun/p/9151723.html