“OTP 源码编译”的版本间的差异
来自Dennis的知识库
Dennis zhuang(讨论 | 贡献) (→erl_process.c) |
Dennis zhuang(讨论 | 贡献) (→erl_lock_check.c) |
||
第57行: | 第57行: | ||
* and the emulator aborts. The lock checker is only | * and the emulator aborts. The lock checker is only | ||
* intended to be enabled when debugging. | * intended to be enabled when debugging. | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | * include/internal/ 下面是各平台的锁的实现,比如 spinlock, rwlock, x86 平台在 i386 目录下,比如 spinlock.h,使用 xchgb 指令汇编实现 | ||
+ | |||
+ | <pre> | ||
+ | static ETHR_INLINE int | ||
+ | ethr_native_spin_trylock(ethr_native_spinlock_t *lock) | ||
+ | { | ||
+ | char tmp = 1; | ||
+ | __asm__ __volatile__( | ||
+ | "xchgb %b0, %1" | ||
+ | : "=q"(tmp), "=m"(lock->lock) | ||
+ | : "0"(tmp) : "memory"); | ||
+ | return tmp == 0; | ||
+ | } | ||
+ | static ETHR_INLINE void | ||
+ | ethr_native_spin_lock(ethr_native_spinlock_t *lock) | ||
+ | { | ||
+ | for(;;) { | ||
+ | if (__builtin_expect(ethr_native_spin_trylock(lock) != 0, 1)) | ||
+ | break; | ||
+ | do { | ||
+ | __asm__ __volatile__("rep;nop" : "=m"(lock->lock) : : "memory"); | ||
+ | } while (ethr_native_spin_is_locked(lock)); | ||
+ | } | ||
+ | } | ||
+ | |||
</pre> | </pre> |
2017年1月7日 (六) 11:29的版本
目录 |
OTP 源码阅读杂记
编译
git clone https://github.com/erlang/otp.git cd otp export ERL_TOP=`pwd` ./otp_build setup brew install fop openssl ./configure --help #查看编译选项 ./configure --with-ssl=/usr/local/opt/openssl make
erl_process.c
- 优先运行 port 直到完成。
/* * Find a new port to run. */ if (RUNQ_READ_LEN(&rq->ports.info.len)) { int have_outstanding_io; have_outstanding_io = erts_port_task_execute(rq, &esdp->current_port); if ((!erts_eager_check_io && have_outstanding_io && fcalls > 2*input_reductions) || rq->halt_in_progress) { /* * If we have performed more than 2*INPUT_REDUCTIONS since * last call to erl_sys_schedule() and we still haven't * handled all I/O tasks we stop running processes and * focus completely on ports. * * One could argue that this is a strange behavior. The * reason for doing it this way is that it is similar * to the behavior before port tasks were introduced. * We don't want to change the behavior too much, at * least not at the time of writing. This behavior * might change in the future. * * /rickard */ goto check_activities_to_run; } }
erl_lock_check.c
- 确保 BEAM 的加锁顺序是一致的,防止死锁。
* Description: A lock checker that checks that each thread acquires * locks according to a predefined global lock order. The * global lock order is used to prevent deadlocks. If the * lock order is violated, an error message is printed * and the emulator aborts. The lock checker is only * intended to be enabled when debugging.
- include/internal/ 下面是各平台的锁的实现,比如 spinlock, rwlock, x86 平台在 i386 目录下,比如 spinlock.h,使用 xchgb 指令汇编实现
static ETHR_INLINE int ethr_native_spin_trylock(ethr_native_spinlock_t *lock) { char tmp = 1; __asm__ __volatile__( "xchgb %b0, %1" : "=q"(tmp), "=m"(lock->lock) : "0"(tmp) : "memory"); return tmp == 0; } static ETHR_INLINE void ethr_native_spin_lock(ethr_native_spinlock_t *lock) { for(;;) { if (__builtin_expect(ethr_native_spin_trylock(lock) != 0, 1)) break; do { __asm__ __volatile__("rep;nop" : "=m"(lock->lock) : : "memory"); } while (ethr_native_spin_is_locked(lock)); } }