How to Share Data between ISR and Task

Recently I developed a library. And another engineer use the API of this library in ISR. Actually this API is designed for Task and this API will get mutex , do something, then release mutex.

 

Then we got strange errors and start to debug.

 

The mutex is from an OS for embedded system. The logic of lock mutex is:

1. get current task

2. if(mutex.lock == 0) {mutex.lock++ and mutex.owner = current_task, then return OK;}

3. if(mutex.owner == current_task) { mutex.lock ++; return OK}

4. Or switch current_task out of CPU.

 

So If an ISR break a Task and both Task and ISR try to get the same mutex(Task gets it  before ISR try to get it), then ISR can get the same mutex and everything works.

My test confirmed this case.

However, if a Task got the mutex, then switch to another Task before release the mutex, and now ISR comes and try to get the mutex. the current task is not the one who hold the mutex, so ISR can't get the mutex. Then go to step 4 and it gets out of control.

My test confirmed this case too.

 

So when ISR try to share data with Task, mutex is a wrong choice. 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值