亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1624 | 回復(fù): 0
打印 上一主題 下一主題

l4中 mutex 實現(xiàn)的流程及代碼分析 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-12-22 08:51 |只看該作者 |倒序瀏覽

    只有多任務(wù)系統(tǒng)才需要mutex,這點應(yīng)該不用解釋,在l4系統(tǒng)中實現(xiàn)mutex 的本質(zhì)
就是讓無法獲得mutex的任務(wù),通過調(diào)度交出當(dāng)前運行權(quán),交由其他任務(wù)繼續(xù)運行,
當(dāng)獲得mutex的任務(wù)在釋放mutex時,如果發(fā)現(xiàn)有對metex的lock請求,也同樣交出運行權(quán),

  然后前面無法獲得mutex的任務(wù)或?qū)⒗^續(xù)運行,重新進(jìn)行l(wèi)ock mutex測試,然后繼續(xù)上面的流程,

獲得就繼續(xù)運行,沒有獲得就交出運行權(quán)

先看下mutex的結(jié)構(gòu):

 

  1. struct mutex {
  2.     L4_Word_t fHolder;
  3.     L4_Word_t fNeeded;
  4.     L4_Word_t fCount;
  5.     };

 

具體的流程圖如下:

上圖中如果 mutex lock  不成功,調(diào)用thread_swith切換到fHolder所保存的 ,已經(jīng)成功mutex lock的
的thread
 
實現(xiàn)代碼:
  1. stmdb sp!, {r4, r5, lr} /* r4 and r5 are local variables,
  2. lr needed when we do the syscall */
  3. /* r4 -- saved lock address */
  4. ldr r4, [r0]
  5. /* Here we load the utcb address */
  6. mov r3, #0xff000000
  7. /* r5 -- saved UTCB address ,liunote see arch\arm\libs\l4\include\vregs.h */
  8. ldr r5, [r3, #0xff0]        /* uctb => 0xff000ff0 */
  9. /* From here r4 and r5 are safe, the thread_switch
  10. syscall will not trash these registers */
  11. /* r0 is tmp */
  12. /* First we set the point at which we want to
  13. be restarted in case of preemption */
  14. adr r0, preempt_handler
  15. str r0, [r5, #__L4_TCR_PREEMPT_CALLBACK_IP*4] /* preempt_callback_ip */
  16. /* And then we enable the preemption callback to
  17. occur */
  18. mov r0, #32
  19. strb r0, [r5, #TCR_PREEMPT_FLAGS] /* arch\arm\libs\l4\include\vregs.h __L4_TCR_PREEMPT_FLAGS 's last byte !!! */
  20. LABEL(preempt_handler) /* If preempt we restart here */
  21. /* r0 is lock holder */
  22. ldr r0, [r4]
  23. /* r1 is me (real tid is in the user-defined handle)*/
  24. ldr r1, [r5, #__L4_TCR_USER_DEFINED_HANDLE*4]
  25. /* test if lock holder is == 0 */
  26. cmp r0, #0 /* mutex-> fHolder 為0,表示沒有被占*/
  27. beq grab_lock /* liunote , get lock ok !!! */
  28. /* or if lock holder is me */
  29. cmp r0, r1
  30. beq exit_path /* We already have the lock so we jump forward to
  31. the part where we turn off preemption and return */
  32. /* we couldn't get the lock so we fall through to here */
  33. /* r0 holds lock holder, will be argument to the system call */
  34. /* r1 is temp */
  35. /* Load syscall ptr */ /* liunote : r0 is lock holder (mutex-> fHolder), 然后L4_ThreadSwitch 切換到lock holer 去了 */
  36. ldr ip, =L4_ThreadSwitch
  37. mov r1, #1
  38. str r1, [r4, #4] /* Let current lock holder know there is contention liunote [r4,#4] ==> mutex -> fNeeded =1 ,讓目前holder 住的thread 知道有其他mutex 請求
  39. so that it knows to yield at the end of its timeslice , 這樣時間片到了要放棄,或者unlock muxtex時主動放棄*/
  40. /* Load branch address */
  41. ldr ip, [ip]
  42. stmdb sp!, {r4-r11}
  43. mov lr, pc
  44. /* do the system call */
  45. mov pc, ip
  46. ldmia sp!, {r4-r11}
  47. /* After syscall return to preempt_handler */ /* 沒有l(wèi)ock 住的thread 到這里thread switch , 然后會切回來繼續(xù)執(zhí)行l(wèi)ock !!! */
  48. b preempt_handler
  49. LABEL(grab_lock)
  50. /* The lock is free -- we try to grab it before being preempted */
  51. /* r0 is tmp */
  52. mov r0, #0
  53. str r0, [r4, #4] /* If we get this far, then noone of a higher priority than liunote [r4,#4] ==> mutex -> fNeeded =0
  54. us wants the lock, so we can unset the yield needed flag */
  55. /* Now we store ourself as the lock handler, this is transaction complete, although
  56. we still might be preempted right here, in which case we validaly have the lock
  57. and the preempt handler will go through sucessfully */
  58. str r1, [r4] /* mutex-> fHolder = thread user define handler (__L4_TCR_USER_DEFINED_HANDLE) !!! */
  59. strb r0, [r5, #TCR_PREEMPT_FLAGS] /* liunote 設(shè)置utcb的__L4_TCR_PREEMPT_FLAGS 最后1byte 為0 */
  60. ldmia sp!, {r4, r5, pc} /* RETURN POINT */
  61. LABEL(exit_path)
  62. /* Exit path that occurs if we were preempted, before returning --
  63. same as above, however we need to zero, r0 first */
  64. mov r0, #0
  65. strb r0, [r5, #TCR_PREEMPT_FLAGS]
  66. ldmia sp!, {r4, r5, pc} /* RETURN POINT */

 

mutex unlock 的實現(xiàn)很簡單:

 

  1. struct mutex *mtx = (struct mutex *) *mutex;
  2.     mtx->fHolder = 0;

  3.     if (mtx->fNeeded) {
  4.     mtx->fNeeded = 0;
  5.     L4_ThreadSwitch(L4_nilthread); /*
  6.     }

 

關(guān)于L4_ThreadSwitch 切到nil thread,表示放棄運行,交由系統(tǒng)調(diào)度

 

您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP