Go Mutex 流程

mutex 流程

type Mutex struct {
    state int32
    sema  uint32
}

等效于

type Mutex struct {
    locked uint
    woken uint
    starving uint
    waiterCount uint
    sema  uint32
}

Lock 流程

%%{ init: { 'flowchart': { 'curve': 'monotoneX' } } }%% flowchart LR ST("Lock") -->CAS{"CAS(locked=1)"} CAS--Yes-->ED CAS-->SlowPathStart(SlowPathStart)-->CANSPAN{CANSPAN} CANSPAN--Yes-->SPAN[SPAN] CANSPAN--No-->CAS0 SPAN--success-->D0[woken=1] D0-->CAS0{"CAS(m,old, new)"}--Yes-->CAS1(( )) CAS1--starving==0 && locked==1-->ED CAS1-->E0[waiterCount+=1]-->E[SemacquireMutex阻塞]-->F[被唤醒] F--starving==0-->H[starving=1]-->SlowPathStart F--starving==1-->G[waiterCount-=1] G-->ED ED(End)

UnLock流程

%%{ init: { 'flowchart': { 'curve': 'monotoneX' } } }%% flowchart TB ST("UnLock") ST-->A[locked=0] A--locked==0 \n&& woken==0 \n&& starving==0 \n&& waiterCount==0-->ED A-->B(( )) B--mutexStarving=1-->B1[Semrelease]-->ED B--mutexStarving=0-->C(( )) C--waiterCount=0==0 \n|| mutexLocked==1 \n|| woken==1 \n|| starving==1-->ED C-->D["CAS(waiterCount-=1,woken=1)"]-->D1[Semrelease]-->ED ED(End)
posted @ 2024-06-24 09:39  Aloe_n  阅读(5)  评论(0编辑  收藏  举报