Can Test and Set be implemented in software without hardware support?

人盡茶涼 提交于 2019-12-08 19:53:19

问题


Here's the Test and Set written in software:

boolean TestAndSet(boolean *target) {
    boolean rv = *target;
    *target = TRUE;
    return rv;
}

and

do {
    while(TestAndSetLock(&lock))
        ; // do nothing
        // critical section
    lock = FALSE;
        // remainder section
} while(TRUE);

Can we use the mechanism in CPUs that do not support test-and-set at the hardware level? If so, how is atomicity assured?


回答1:


You can use Lamport's 'bakery' mutual exclusion algorithm on machines w/o TAS/CAS to gate access to the 'atomic' (protected) value.

http://www.disi.unige.it/person/DelzannoG/SO1/AA0607/bakery.htm

It only gets complicated if you don't have a reasonably limited 'N' processes.




回答2:


Test and Set cannot be implemented in software without hardware support the way you proposed.

It is because TestAndSet above is supposed to be a mechanism to be used to ensure mutual exclusion, but it is not atomic. Atomicity, however, is just another name for mutual exclusion: the internal variables of TestAndSet must be protected by ensuring that two processes cannot execute its code concurrently.

So this way you defined a method for ensuring mutual exclusion which itself requires some mechanism to ensure mutual exclusion. This trick can be played a number of times, but there will not be any real progress without some sort of hardware support.



来源:https://stackoverflow.com/questions/25117263/can-test-and-set-be-implemented-in-software-without-hardware-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!