3 ms·
It's still a lock in the sense that it momentarily prevents another process from using a resource.
by amelius 7y ago
It's still a lock in the sense that it momentarily prevents another process from using a resource.
- kccqzy 7y agoIt is different. The lock prefix in x86 is only relevant in a multiprocessing (i.e. parallel) environment. But the general concept of locking is applicable to concurrent environments as well. For example when there are multiple processes on a uniprocessor machine, you still need to use locks (mutexes) which are implemented using, say, cmpxchg8b (atomically compare and exchange eight bytes), but in the absence of a second processor there's no need to use the lock prefix. Here on a machine level there's never really on any locking, but on an application level you are using locks.
- denormalfloat 7y agoThe correct term to use here is blocking. The hardware bus locks finish in a bounded amount of time and allow the other processors to eventually make progress.
- amelius 7y agoThat is a good distinction, but I'm not sure if it's always true. A peripheral device on a shared bus could still hold the lock for an undetermined time.
- jasonwatkinspdx 7y agoWhen people say "lock the bus" they don't literally mean it's like there's a single bus and mutex. Atomic operations execute atop the cache consistency protocol, which typically looks like: https://en.wikipedia.org/wiki/MOESI_protocol https://en.wikipedia.org/wiki/MOESI_protocol It is indeed true that atomic operations will execute in a bounded time, and processors generally provide fairness guarantees as well.
- amelius 7y agoYes, and the cache hierarchy ultimately depends on the memory bus. I suppose this bus, which may be shared with many other devices, doesn't always have bounded-time guarantee.
- gpderetta 7y agoEven to main memory there is not necessarily a single memory bus. Intracore or even intrasocket synchronization need not (and usually doesn't) go through main memory anyway.
- amelius 7y agoTrue, but some atomic instructions may need to access main memory to complete their operation. Whether shortcuts can be taken in most cases is not relevant for worst-case considerations.
- gpderetta 7y agoSure any memory access might access main memory. There is no special casing for atomic though.
- bonzini 7y agoThey may need to access main memory, but the RMW operation don't happen over the memory bus. The processor appropriates the cache line just like any other memory access, and then operates atomically on the cache line.
- amelius 7y agoAnd what if the cache line is full/dirty?
- bonzini 7y agoThe cache coherency protocol takes care of that. In other words the first part is just a memory load and can vary from 0 to a few hundred clock cycles, the second is local to the processor and has a more or less fixed cost. The worst-case execution time is completely dominated by the first part, the best case instead is dominated by the second.
- gpderetta 7y agoThe only resource that is held exclusively during an RMW is a single cache line, and that's not due to the lock prefix.