debugging: detect semaphore conflicts
- Size
- 6.8K (6,985 bytes)
- Added
- 2026-11-20 02:43
- Downloads
- 2
- Long Description
- ----------------------------------------------------------------------------- This tool help to hunt down semaphore conflicts, or short "deadlocks". It is primarily targeted at developers. What is a semaphore? A semahore (actually, these structures are called "monitors" outside AmigaOs) is a data structure that grants exclusive access to a resource, such that only a single task can operate on a data structure at a time. Semaphores are a service provided by the exec.library. If a task obtains a semaphore, it locks the corresponding resource for exclusive access by this task. Any other task also attempting to obtain the same semaphore at this time will either block (through ObtainSemaphore()) or access will fail (AttemptObtainSemaphore()). Any task waiting on a blocked semaphore will be granted access as soon as the first task releases the semaphore (ReleaseSemaphore()). What is a semaphore conflict? If, in a complex system, at least two semaphores are locked by two tasks, though the order in which they are obtained is not defined, a situation may appear where the tasks lock out themselves such that the program state cannot progress anymore. This is called a "deadlock". Depending on the tasks involved in such a deadlock, the mouse pointer may freeze, or the disk may freeze, or a program may freeze, or a window may freeze... How do such semaphore conflicts arise? It takes at least two semaphores (A and B) and two tasks (1 and 2). Consider that task 1 first obtains semaphore A, then a task switch gives task 2 the CPU. Task 2 obtains semaphore B. While holding semaphore B, task 2 tries to obtain semaphore A, but is blocked out because task 1 holds it already. Hence, the exec scheduler gives task 1 the CPU again. If task 1 now tries to obtain semaphore B, none of the tasks can progress anymore. Task 1 holds semaphore A, and task 2 holds semaphore B, but none can progress as each requires the semaphore of the other task. How can LockSmith help? LockSmith will, upon pressing a "magic key", report which semaphores are held by which tasks. If you see there a circular dependency as the above, something is wrong with your program logic. It is necessary to define a strict order in which semaphores are locked. Unfortunately, many Os calls, in particular around intuition and layers, obtain locks implicitly, and hence require careful consideration of the semaphore dependencies. Locksmith is best combined with SegTracker, such that it can report in which hunk and offset of which program the deadlock was triggered. If the "magic key" is pressed, LockSmith will print for every semaphore: 1) The name of the task that is currently waiting on the semaphore, along with its address, 2) The name of the task that created the semaphore, if this information is available, 3) The name of the task that is currently holding the semaphore, along with its address, 4) A stack dump of the task that is currently blocked at obtaining the semaphore. If addresses in the stack dump point to known segments tracked by SegTracker, it also prints the hunk number and offset of the program within which the address on the stack belongs. The output of LockSmith reads very much like the output of MuForce, see below for an example. Can LockSmith "unlock" the semaphores and let the Amiga continue to operate? No. This would also be dangerous as it is unclear which task is allowed to receive which semaphore. LockSmith is a debugging tool helping to detect semaphore conflicts in programs, and then resolving them by fixing dependency problems of semaphores. It is not a program that magically unlocks semaphore locks (despite its name). That is up to you to fix the software. It pinpoints at the problem, but it doesn't resolve it. ----------------------------------------------------------------------------- Usage: LockSmith should be run early in the startup-sequence, but after SegTracker. Whether you place LockSmith before or after MuForce/MuGuardianAngel does