| Disable interrupts;/*关中断*/ |
| Access the resource (read/write from/to variables);/*读/写变量*/ |
| Reenable interrupts;/*重新允许中断*/ |
| void Function (void) |
| { |
| OS_ENTER_CRITICAL(); |
| . |
| ./*在这里处理共享数据*/ |
| . |
| OS_EXIT_CRITICAL(); |
| } |
| Disable interrupts;关中断 |
| if (‘Access Variable’ is 0) {如果资源不可用,标志为0 |
| Set variable to 1;置资源不可用,标志为1 |
| Reenable interrupts;重开中断 |
| Access the resource;处理该资源 |
| Disable interrupts;关中断 |
| Set the ‘Access Variable’ back to 0;清资源不可使用,标志为0 |
| Reenable interrupts;重新开中断 |
| } else {否则 |
| Reenable interrupts;开中断 |
| /* You don’t have access to the resource, try back later; */ /*资源不可使用,以后再试; */ |
| } |
| 程序清单2.6用给任务切换上锁,然后开锁的方法实现数据共享. |
| void Function (void) |
| { |
| OSSchedLock(); |
| . |
| ./* You can access shared data in here (interrupts are recognized) */ |
| ./*在这里处理共享数据(中断是开着的)*/ |
| OSSchedUnlock(); |
| } |
| 通过获得信号量处理共享数据 |
| OS_EVENT *SharedDataSem; |
| void Function (void) |
| { |
| INT8U err; |
| OSSemPend(SharedDataSem, 0, &err); |
| . |
| ./* You can access shared data in here (interrupts are recognized) */ |
| ./*共享数据的处理在此进行,(中断是开着的)*/ |
| OSSemPost(SharedDataSem); |
| } |
| 程序清单2.8隐含的信号量。 |
| INT8U CommSendCmd(char *cmd, char *response, INT16U timeout) |
| { |
| Acquire port's semaphore; |
| Send command to device; |
| Wait for response (with timeout); |
| if (timed out) { |
| Release semaphore; |
| return (error code); |
| } else { |
| Release semaphore; |
| return (no error); |
| } |
| } |
| BUF *BufReq(void) |
| { |
| BUF *ptr; |
| Acquire a semaphore; |
| Disable interrupts; |
| ptr= BufFreeList; |
| BufFreeList = ptr->BufNext; |
| Enable interrupts; |
| return (ptr); |
| } |
| void BufRel(BUF *ptr) |
| { |
| Disable interrupts; |
| ptr->BufNext = BufFreeList; |
| BufFreeList= ptr; |
| Enable interrupts; |
| Release semaphore; |
| } |
| 欢迎光临 嵌入式开发交流网论坛 (http://www.dianzixuexi.com/bbs/) | Powered by Discuz! X3.2 |