Semaphores are a widely used and foundational synchronization and coordination construct used for shared memory multithreaded programming. They are a keystone concept, in the sense that most other synchronization constructs can be implemented in terms of semaphores, although the converse does not generally hold. Semaphores and the quality of their implementation are of consequence as they remain heavily used in the Linux kernel and are also available for application programming via the pthreads programming interface. We first show that semaphores can be implemented by borrowing ideas from the classic ticket lock algorithm. The resulting "ticket-semaphore" algorithm is simple and compact (space efficient) but does not scale well because of the detrimental impact of global spinning. We then transform "ticket-semaphore" into the "TWA-semaphore" by the applying techniques derived from the "TWA - Ticket Locks Augmented with a Waiting Array" algorithm, yielding a scalable semaphore that remains compact and has extremely low latency.
翻译:信号量是一种广泛使用的基础同步与协调原语,用于共享内存多线程编程。它是一个关键概念,因为大多数其他同步原语都可以基于信号量实现,尽管反之通常不成立。信号量及其实现质量具有重要意义,因为它们在Linux内核中仍被大量使用,并且也可通过pthreads编程接口用于应用程序编程。我们首先证明,可以通过借鉴经典票号锁算法的思想来实现信号量。由此产生的"票号信号量"算法简单紧凑(空间高效),但由于全局自旋的有害影响,其扩展性不佳。然后,我们通过应用源自"TWA - 带等待数组的票号锁"算法的技术,将"票号信号量"转化为"TWA信号量",从而得到一个可扩展、保持紧凑且具有极低延迟的信号量。