qlock 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. .TH QLOCK 9
  2. .SH NAME
  3. qlock, qunlock, canqlock, rlock, runlock, wlock, wunlock \- serial synchronisation
  4. .SH SYNOPSIS
  5. .ta \w'\fLvoid 'u
  6. .B
  7. void qlock(QLock *l)
  8. .PP
  9. .B
  10. void qunlock(QLock *l)
  11. .PP
  12. .B
  13. int canqlock(QLock *l)
  14. .PP
  15. .B
  16. void rlock(RWlock *l)
  17. .PP
  18. .B
  19. void runlock(RWlock *l)
  20. .PP
  21. .B
  22. int canrlock(RWlock *l)
  23. .PP
  24. .B
  25. void wlock(RWlock *l)
  26. .PP
  27. .B
  28. void wunlock(RWlock *l)
  29. .SH DESCRIPTION
  30. The primitive locking functions described in
  31. .IR lock (9)
  32. guarantee mutual exclusion, but they implement spin locks,
  33. and should not be used if the process might
  34. .IR sleep (9)
  35. within a critical section.
  36. The following functions serialise access to a resource by forming an orderly
  37. queue of processes.
  38. .PP
  39. Each resource to be controlled is given an associated
  40. .B QLock
  41. structure; it is usually most straightforward to put the
  42. .B QLock
  43. in the structure that represents the resource.
  44. It must be initialised to zero before use
  45. (as guaranteed for global variables and for structures allocated by
  46. .IR malloc ).
  47. .PP
  48. On return from
  49. .IR qlock ,
  50. the process has acquired the lock
  51. .IR l ,
  52. and can assume exclusive access to the associated resource.
  53. If the lock is not immediately available, the requesting process is placed on a
  54. FIFO queue of processes that have requested the lock.
  55. Processes on this list are blocked in the
  56. .L Queueing
  57. state.
  58. .PP
  59. .I Qunlock
  60. unlocks
  61. .I l
  62. and schedules the first process queued for it (if any).
  63. .PP
  64. .I Canqlock
  65. is a non-blocking form of
  66. .IR qlock .
  67. It tries to obtain the lock
  68. .I l
  69. and returns true if successful, and 0 otherwise;
  70. it always returns immediately.
  71. .PP
  72. .B RWlock
  73. is a form of lock for resources that have distinct readers and writers.
  74. It allows concurrent readers but gives each writer exclusive access.
  75. A caller announces its read or write intentions by choice of lock (and unlock) function;
  76. the system assumes the caller will not modify a structure accessed under read lock.
  77. .PP
  78. .I Rlock
  79. acquires
  80. .I l
  81. for reading.
  82. The holder can read but agrees not to modify the resource.
  83. There may be several concurrent readers.
  84. .I Canrlock
  85. is non-blocking: it returns non-zero if it successfully acquired the lock immediately,
  86. and 0 if the resource was unavailable.
  87. .PP
  88. .I Runlock
  89. returns a read lock;
  90. the last reader out enables the first writer waiting (if any).
  91. .PP
  92. .I Wlock
  93. acquires a write lock.
  94. The holder of such a lock may assume exclusive access to the resource,
  95. and is allowed to modify it.
  96. .PP
  97. .I Wunlock
  98. returns a write lock.
  99. The next pending process, whether reader or writer, is scheduled.
  100. .SH SOURCE
  101. .B /sys/src/9/port/qlock.c
  102. .br
  103. .SH SEE ALSO
  104. .IR lock (9),
  105. .IR splhi (9)