// File: /workspace/github_c_makefile_star2_O0/sharpyfox[P]citrusleaf-client/src/cl_shm.c
// Function: cl_shm_updater_fn
void*
cl_shm_updater_fn(void* gas)
{
	//Take Lock - only one process can update
	int status = pthread_mutex_lock(&(g_shm_pt->shm_lock));

	if (g_shm_info.update_thread_end_cond) {
		pthread_exit(0);
	}

	/* There are only two valid cases when we can access the protected data
	 * 1. We get the lock without any error and
	 * 2. The thread holding the lock died without releasing it */
	if (status != 0) {
		/* Make the lock consistent if the last owner of the lock died while holding the lock */
		/* We should ideally clean the memory here and then make the lock consistent */
		if (status == EOWNERDEAD) {
			pthread_mutex_consistent_np(&(g_shm_pt->shm_lock));
		}
		else {
			/* With Robust mutexes the lock gets in not recoverable state when the process holding the lock
			 * dies and the other process that gets the lock unlocks it without making it consistent
			 * (via pthread_mutex_consistent function) */
			cf_error("Failed to lock shared memory in tend thread. Exiting thread.");
			pthread_exit(0);
		}
	}

	cf_debug("Process %d took over control with pthread_mutex_lock returning %d", getpid(), status);
	g_shm_updater = true;

	do {
		/* Check if the thread is set to exit or not. If it is, gracefully exit*/
		if (g_shm_info.update_thread_end_cond) {
			cl_shm_free_seed_array();
			pthread_exit(0);
		}

		sleep(g_shm_info.update_period);

		if (g_shm_info.update_thread_end_cond) {
			cl_shm_free_seed_array();
			pthread_exit(0);
		}

		// Tend all clusters.
		cf_ll_element *e = cf_ll_get_head(&cluster_ll);

		while(e) {
			cl_shm_update((cl_cluster*)e);
			e = cf_ll_get_next(e);
		}
	} while(1);

	return 0;
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movq 0xef30(%rip), %rdi      # 0x20610
addq $0x10, %rdi
callq 0x23e0
movl %eax, 0x1c(%rsp)
testb $0x1, 0xef14(%rip)      # 0x20608
je 0x116ff
xorl %eax, %eax
movl %eax, %edi
callq 0x24e0
cmpl $0x0, 0x1c(%rsp)
je 0x11753
cmpl $0x82, 0x1c(%rsp)
jne 0x11722
movq 0xeef9(%rip), %rdi      # 0x20610
addq $0x10, %rdi
callq 0x2220
jmp 0x11751
leaq 0xcbaf(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x11748
leaq 0xcba8(%rip), %rax      # 0x1e2e0
movq (%rax), %rcx
xorl %edi, %edi
leaq 0x7e52(%rip), %rsi      # 0x19596
movb $0x0, %al
callq *%rcx
xorl %eax, %eax
movl %eax, %edi
callq 0x24e0
jmp 0x11753
leaq 0xcb7e(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
movl $0x3, %eax
cmpl %ecx, %eax
jg 0x11795
leaq 0xcb74(%rip), %rax      # 0x1e2e0
movq (%rax), %rax
movq %rax, 0x8(%rsp)
callq 0x23d0
movq 0x8(%rsp), %r8
movl %eax, %edx
movl 0x1c(%rsp), %ecx
movl $0x3, %edi
leaq 0x7e43(%rip), %rsi      # 0x195d3
movb $0x0, %al
callq *%r8
movb $0x1, 0xee84(%rip)      # 0x20620
testb $0x1, 0xee65(%rip)      # 0x20608
je 0x117b3
callq 0x11cc0
xorl %eax, %eax
movl %eax, %edi
callq 0x24e0
movl 0xee53(%rip), %edi      # 0x2060c
callq 0x21b0
testb $0x1, 0xee43(%rip)      # 0x20608
je 0x117d5
callq 0x11cc0
xorl %eax, %eax
movl %eax, %edi
callq 0x24e0
leaq 0xec0c(%rip), %rdi      # 0x203e8
callq 0x11a40
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x11809
movq 0x10(%rsp), %rdi
callq 0x11a50
movq 0x10(%rsp), %rdi
callq 0x11c40
movq %rax, 0x10(%rsp)
jmp 0x117e6
jmp 0x1180b
movb $0x1, %al
testb $0x1, %al
jne 0x1179c
jmp 0x11813
xorl %eax, %eax
addq $0x28, %rsp
retq
nopw (%rax,%rax)