# 系統程式設計 - Introduction to Locks
[TOC]
## 前言
在你花時間了解所謂的「一個 spinlock 解法」(pseudocode、XXX's solution、不知道哪來的投影片)之前,可以看看 2020 有 Google 工程師實作自己的 spinlock 並且 [抱怨 Linux 的 spinlock 很糟糕](https://www.phoronix.com/scan.php?page=news_item&px=Torvalds-On-Scheduler-Woes) 之後,被 Linus Torvalds 本人砲轟是 *pure garbage* 的事件。Torvalds 本人的回應在 [*No nuances, just buggy code (was: related to Spinlock implementation and the Linux Scheduler)*](https://www.realworldtech.com/forum/?threadid=189711&curpostid=189723):
> *In fact, I'd go even further: don't ever make up your own locking routines. You will get the wrong, whether they are spinlocks or not. You'll get memory ordering wrong, or you'll get fairness wrong, or you'll get issues like the above "busy-looping while somebody else has been scheduled out".*
>
> ...
>
> *Because you should **never ever** think that you're clever enough to write your own locking routines.. Because the likelihood is that you aren't (and by that "you" I very much include myself - we've tweaked all the in-kernel locking over decades, and gone through the simple test-and-set to ticket locks to cacheline-efficient queuing locks, and even people who know what they are doing tend to get it wrong several times).*
>
> *There's a reason why you can find **decades** of academic papers on locking. Really. It's hard.*
>
> Linus
## 課程內容
### Let's Talk Locks!
{%youtube 7OpCf6f_BAM %}