ngx_unlock_mutexes
1 定义ngx_unlock_mutexes 函数 定义在 ./nginx-1.24.0/src/os/unix/ngx_process.cstaticvoidngx_unlock_mutexes(ngx_pid_tpid){ngx_uint_ti;ngx_shm_zone_t*shm_zone;ngx_list_part_t*part;ngx_slab_pool_t*sp;/* * unlock the accept mutex if the abnormally exited process * held it */if(ngx_accept_mutex_ptr){(void)ngx_shmtx_force_unlock(ngx_accept_mutex,pid);}/* * unlock shared memory mutexes if held by the abnormally exited * process */part(ngx_list_part_t*)ngx_cycle-shared_memory.part;shm_zonepart-elts;for(i0;/* void */;i){if(ipart-nelts){if(part-nextNULL){break;}partpart-next;shm_zonepart-elts;i0;}sp(ngx_slab_pool_t*)shm_zone[i].shm.addr;if(ngx_shmtx_force_unlock(sp-mutex,pid)){ngx_log_error(NGX_LOG_ALERT,ngx_cycle-log,0,shared memory zone \%V\ was locked by %P,shm_zone[i].shm.name,pid);}}}ngx_unlock_mutexes 函数 用于在 Nginx master 进程监测到某个 worker 进程异常退出后 强制释放该进程可能持有的共享内存互斥锁防止死锁。2 详解1 函数签名staticvoidngx_unlock_mutexes(ngx_pid_tpid)无返回值 参数 ngx_pid_t pid 异常退出的进程 ID2 逻辑流程1 局部变量 2 处理 Accept 互斥锁 3 遍历共享内存1 局部变量{ngx_uint_ti;ngx_shm_zone_t*shm_zone;ngx_list_part_t*part;ngx_slab_pool_t*sp;2 处理 Accept 互斥锁/* * unlock the accept mutex if the abnormally exited process * held it */if(ngx_accept_mutex_ptr){(void)ngx_shmtx_force_unlock(ngx_accept_mutex,pid);}若异常退出进程持有 accept_mutex则将其解锁 ngx_accept_mutex_ptr 是 accept 锁在共享内存中的地址指针 仅在启用了 accept mutex 时才非空。 ngx_shmtx_force_unlock强制解锁函数3 遍历共享内存/* * unlock shared memory mutexes if held by the abnormally exited * process */part(ngx_list_part_t*)ngx_cycle-shared_memory.part;shm_zonepart-elts;初始化要遍历的 ngx_cycle-shared_memory 链表 ngx_cycle-shared_memory 是 ngx_list_t 类型 其第一个链表节点直接内嵌在 part 成员中。 part 指向该首个节点。 shm_zone 指向该节点内的元素数组elts 元素类型为 ngx_shm_zone_t。for(i0;/* void */;i){if(ipart-nelts){if(part-nextNULL){break;}partpart-next;shm_zonepart-elts;i0;}sp(ngx_slab_pool_t*)shm_zone[i].shm.addr;if(ngx_shmtx_force_unlock(sp-mutex,pid)){ngx_log_error(NGX_LOG_ALERT,ngx_cycle-log,0,shared memory zone \%V\ was locked by %P,shm_zone[i].shm.name,pid);}}}#1 无限循环通过内部条件判断退出。 i 作为当前节点内元素数组的索引。#2 当索引 i 达到当前节点内元素数量 part-nelts 时 检查是否有下一个节点 part-next若 无则链表遍历结束break 退出循环。 若存在下一个节点将 part 移动到下一节点 shm_zone 重新指向新节点的元素数组并将 i 重置为 0。#3 获取当前共享内存区域的实际起始地址 并强制转换为 ngx_slab_pool_t *。 shm_zone[i].shm.addr 在共享内存初始化时被设置为 slab pool 的基地址。#4 对 slab pool 内部的 mutex 调用 ngx_shmtx_force_unlock。 返回值非零表示锁确实被该 pid 持有并已成功强制解锁。 若解锁成功记录一条 ALERT 级别日志
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2528421.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!