2012-10-13 77 views
1

所以我想我理解信號和等待的源代碼(等待成爲鎖),但我不知道如何實現嘗試鎖。 這裏是我的等待代碼:嘗試鎖的源代碼是什麼?

//if s->type is zero it is a binary semaphore type 
if (s->type == 0) 
    { 
      // binary semaphore 
      // if state is zero, then block task 

      if (s->state == 0) 
      { 
          // block task 


        // ?? move task from ready queue to blocked queue 

           //reschedule the tasks 
        return 1; 
      } 
      // state is non-zero (semaphore already signaled) 
      s->state = 0;    // reset state, and don't block 
      return 0; 
    } 
    else 
    { 
      // counting semaphore 
      s->state--; 
      // ?? implement counting semaphore 
      if (s->state < 0) 
      { 


      } 
    } 

這是我迄今一試鎖:

if (s->type == 0) 
{ 
      // binary semaphore 
      // if state is zero, then block task 

      if (s->state == 0) 
      { 
        tcb[curTask].event = s;   // block task 
        tcb[curTask].state = S_BLOCKED; 

        removeNode(tcb[curTask].priority, READY_QUEUE, curTask); 
        enqueue(tcb[curTask].priority, curTask, BLOCKED_QUEUE); 
        return 1; 
      } 
      // state is non-zero (semaphore already signaled) 
      s->state = 1;           // reset state, and don't block 
      return 0; 
} 
else 
{ 
     s->state--; 
     if (s->state >= 0) 
     { 
      s->state++; 
     } 
     else 
     { 
      tcb[curTask].event = s; 
      tcb[curTask].state = S_BLOCKED; 
      removeNode(tcb[curTask].priority, READY_QUEUE, curTask); 
      enqueue(tcb[curTask].priority, curTask, BLOCKED_QUEUE); 
     } 
} 
+1

嘗試鎖定信號量,如果它已被其他進程鎖定,則立即返回(非阻塞)並返回一些錯誤代碼。 – SparKot

回答

0

我一直在尋找一個非自旋鎖的tryLock。 我已經想出了要做什麼。如果它是一個計數信號量,那麼如果計數是正數並且我消耗資源,那麼我遞減。如果它是零或更少,我什麼也不做,只是返回一個錯誤代碼。我不減少數量或消耗資源。該程序然後能夠繼續超過這一點。如果它是一個二進制信號量,如果資源可用,則將其消耗。然後我將二進制信號量的值改爲消耗。如果它不可用,那麼我返回一個錯誤代碼。

4

定期自旋鎖來實現這樣的(僞C-codish ):

void lock(locktype_t* LockVariable) 
{ 
    while (CompareAndSwap(LockVariable, 
         STATE_UNLOCKED /* state to wait for */, 
         STATE_LOCKED /* new state to try to set */) != 
     STATE_UNLOCKED /* expected state at the beginning of CAS() */) 
    { 
    // spin here, doing nothing useful, waiting for *LockVariable to 
    // first become STATE_UNLOCKED (CAS() returns its last value), after 
    // which we will set it to STATE_LOCKED (CAS() will do that atomically) 
    } 
} 

void unlock(locktype_t* LockVariable) 
{ 
    *LockVariable = STATE_UNLOCKED; 
} 

在不定紡絲和等待鎖成爲第一解鎖是不希望的情況下,我們使用上述這樣的一個環路較少的變體:

int tryToLock(locktype_t* LockVariable) 
{ 
    if (CompareAndSwap(LockVariable, 
        STATE_UNLOCKED /* state to wait for */, 
        STATE_LOCKED /* new state to try to set */) != 
     STATE_UNLOCKED /* expected state at the beginning of CAS() */) 
    { 
    return 0; // the lock is still held by someone else, bail out 
    } 
    return 1; // the lock is now held by us, hurray! 
} 

Compare-and-swap

+0

'如果(AtomicSwap(LockVariable,STATE_LOCKED)== STATE_LOCKED)返回0;'工作呢? – a3f

+0

回答我自己的問題:你會的。 [這個答案](http://stackoverflow.com/questions/5339769/relative-performance-of-swap-vs-compare-and-swap-locks-on-x86)可能是相關的。 – a3f

相關問題