您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Nginx中的原子操作

2025/12/11 15:50:16发布16次查看
在不支持原子操作的处理器上nginx提供了自己的原子操作,但是该原子操作使用c语言模拟的:
static ngx_inline ngx_atomic_uint_tngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old, ngx_atomic_uint_t set){ if (*lock == old) { *lock = set; return 1; } return 0;}
static ngx_inline ngx_atomic_int_tngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add){ ngx_atomic_int_t old; old = *value; *value += add; return old;}
在支持原子操作上的处理器(以x86为例):
#define ngx_smp_lock lock;/** cmpxchgl r, [m]:** if (eax == [m]) {* zf = 1;* [m] = r;* } else {* zf = 0;* eax = [m];* }*** the r means the general register.* the =a and a are the %eax register.* although we can return result in any register, we use a because it is* used in cmpxchgl anyway. the result is actually in %al but not in %eax,* however, as the code is inlined gcc can test %al as well as %eax,* and icc adds movzbl %al, %eax by itself.** the cc means that flags were changed.*/static ngx_inline ngx_atomic_uint_tngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old, ngx_atomic_uint_t set){ u_char res; __asm__ volatile ( ngx_smp_lock cmpxchgl %3, %1; sete %0; : =a (res) : m (*lock), a (old), r (set) : cc, memory); return res;}
/** xaddl r, [m]:** temp = [m];* [m] += r;* r = temp;*** the +r means the general register.* the cc means that flags were changed.*/static ngx_inline ngx_atomic_int_tngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add){ ngx_atomic_uint_t old; __asm__ volatile ( ngx_smp_lock xaddl %2, %1; : =a (old) : m (*value), a (add) : cc, memory); return old;}
自旋锁:
/* old as does not support pause opcode */#define ngx_cpu_pause() __asm__ (.byte 0xf3, 0x90)#define ngx_sched_yield() sched_yield()voidngx_spinlock(ngx_atomic_t *lock, ngx_atomic_int_t value, ngx_uint_t spin){#if (ngx_have_atomic_ops) ngx_uint_t i, n; for ( ;; ) { if (*lock == 0 && ngx_atomic_cmp_set(lock, 0, value)) { return; } if (ngx_ncpu > 1) { for (n = 1; n
信号:
ngx_int_tngx_init_signals(ngx_log_t *log){ ngx_signal_t *sig; struct sigaction sa; for (sig = signals; sig->signo != 0; sig++) { ngx_memzero(&sa, sizeof(struct sigaction)); sa.sa_handler = sig->handler; sigemptyset(&sa.sa_mask); if (sigaction(sig->signo, &sa, null) == -1) {#if (ngx_valgrind) ngx_log_error(ngx_log_alert, log, ngx_errno, sigaction(%s) failed, ignored, sig->signame);#else ngx_log_error(ngx_log_emerg, log, ngx_errno, sigaction(%s) failed, sig->signame); return ngx_error;#endif } } return ngx_ok;}
以上就介绍了nginx中的原子操作,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product