下拉刷新
项目详情
navbar_avatar分享
repo_avatar
HelloGitHub 评分
0 人评分
图文并茂的 Python 源码阅读笔记项目
待认领
收藏
分享
4k
星数
中文
C
主语言
活跃
14
贡献者
5
Issues
组织
最新版本
431
Forks
协议
更多

图文并茂的 Python 源码阅读笔记项目。阅读的是比较新的 CPython 3.8 版本,重点是项目一直在更新维护

static void take_gil(PyThreadState *tstate) { /* 忽略 */ while (_Py_atomic_load_relaxed(&_PyRuntime.ceval.gil.locked)) { /* 只要 gil 是锁住的状态, 进入这个循环 */ int timed_out = 0; unsigned long saved_switchnum; saved_switchnum = _PyRuntime.ceval.gil.switch_number; /* 释放 gil.mutex, 并在以下两种条件下唤醒 1. 等待 INTERVAL 微秒(默认 5000) 2. 还没有等待到 5000 微秒但是收到了 gil.cond 的信号 */ COND_TIMED_WAIT(_PyRuntime.ceval.gil.cond, _PyRuntime.ceval.gil.mutex, INTERVAL, timed_out); /* 当前持有 gil.mutex 这把互斥锁 */ if (timed_out && _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil.locked) && _PyRuntime.ceval.gil.switch_number == saved_switchnum) { /* 如果超过了等待时间, 并且这段等待时间里 gil 的持有者没有变更过, 则尝试让当前持有 gil 的线程进行释放gil 把 gil_drop_request 值设为 1, 持有锁的线程看到这个值的时候, 会尝试放弃 gil */ SET_GIL_DROP_REQUEST(); } /* 继续回到 while 循环, 检查 gil 是否为锁住状态 */ } /* 忽略 */ }

评论

评分:
暂无精选评论