lock 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. .TH LOCK 9
  2. .SH NAME
  3. lock, canlock, ilock, iunlock, unlock \- spin locks
  4. .SH SYNOPSIS
  5. .ta \w'\fLvoid 'u
  6. .B
  7. void lock(Lock *l)
  8. .PP
  9. .B
  10. int canlock(Lock *l)
  11. .PP
  12. .B
  13. void unlock(Lock *l)
  14. .PP
  15. .B
  16. void ilock(Lock *l)
  17. .PP
  18. .B
  19. void iunlock(Lock *l)
  20. .SH DESCRIPTION
  21. These primitives control access to shared
  22. resources using spin locks.
  23. They in turn are used to build higher-level synchronisation mechanisms
  24. such as those described in
  25. .IR sleep (9),
  26. .IR qlock (9)
  27. and
  28. .IR qio (9).
  29. They should be used only to protect short critical sections
  30. that update shared data structures.
  31. .PP
  32. .I Lock
  33. loops repeatedly attempting acquire the spin lock
  34. .I l
  35. until it succeeds.
  36. .I Lock
  37. should not be used to lock a structure shared with an interrupt handler
  38. unless interrupts are disabled by
  39. .IR splhi (9)
  40. before attempting the lock;
  41. it is better to use
  42. .IR ilock ,
  43. below.
  44. .PP
  45. .I Canlock
  46. is non-blocking.
  47. Only one attempt is made for the lock.
  48. It returns non-zero if the lock was successfully acquired; 0 otherwise.
  49. .PP
  50. .I Unlock
  51. releases the lock
  52. .IR l .
  53. A lock must be unlocked only by the locking process.
  54. .PP
  55. When called by a process, the functions above temporarily boost its priority
  56. to the highest priority,
  57. .BR PriLock ;
  58. its original priority is restored at the end of the critical section by
  59. .IR unlock .
  60. On a uniprocessor, if
  61. .I l
  62. is unavailable,
  63. .I lock
  64. can reschedule unless interrupts are disabled before entering
  65. .I lock
  66. or there is no current process (eg, when executing the scheduler).
  67. .PP
  68. .I Ilock
  69. disables interrupts before attempting to acquire the lock.
  70. It should be used to lock a resource shared between a process and an interrupt handler.
  71. On a uniprocessor, disabling interrupts is sufficient to exclude an interrupt handler
  72. from the critical section,
  73. and on a multiprocessor the spin lock excludes an interrupt handler running on another processor.
  74. .I Ilock
  75. never reschedules the caller, nor must a caller allow itself to be rescheduled
  76. (eg, by calling
  77. .IR sleep (9))
  78. before releasing the lock.
  79. .PP
  80. .I Iunlock
  81. releases a lock previously got by
  82. .IR ilock .
  83. .SH SOURCE
  84. .B /sys/src/9/port/taslock.c
  85. .br
  86. .B /sys/src/9/*/l.s
  87. .SH SEE ALSO
  88. .IR qlock (9)
  89. .SH DIAGNOSTICS
  90. The lock functions
  91. guard against the possibility of never acquiring the lock by capping the number of lock attempts.
  92. If the limit is reached, a message of
  93. the following form is written on the console:
  94. .IP
  95. .EX
  96. lock loop on \fIlock-address\fP key \fIkey-value\fP pc \fIcaller-pc\fP held by pc \fIlock-pc\fP
  97. .EE
  98. .PP
  99. Most lock loops represent deadlocks caused by failing to unlock a resource,
  100. attempting to lock (eg, by recursive call) a resource already held by the process,
  101. inconsistent locking and unlocking of nested resources, using a spin-lock
  102. to guard code that reschedules, using
  103. .I lock
  104. not
  105. .I ilock
  106. to interlock with an interrupt routine, and similar blunders.