// File: /workspace/github_c_makefile_star2_O0/TeaInside[P]teavpn/src/third_party/thpool/thpool.c
// Function: thread_do
static void* thread_do(struct thread* thread_p){

	/* Set thread name for profiling and debuging */
	char thread_name[128] = {0};
	sprintf(thread_name, "thread-pool-%d", thread_p->id);

#if defined(__linux__)
	/* Use prctl instead to prevent using _GNU_SOURCE flag and implicit declaration */
	prctl(PR_SET_NAME, thread_name);
#elif defined(__APPLE__) && defined(__MACH__)
	pthread_setname_np(thread_name);
#else
	err("thread_do(): pthread_setname_np is not supported on this system");
#endif

	/* Assure all threads have been created before starting serving */
	thpool_* thpool_p = thread_p->thpool_p;

	/* Register signal handler */
	struct sigaction act;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	act.sa_handler = thread_hold;
	if (sigaction(SIGUSR1, &act, NULL) == -1) {
		err("thread_do(): cannot handle SIGUSR1");
	}

	/* Mark thread as alive (initialized) */
	pthread_mutex_lock(&thpool_p->thcount_lock);
	thpool_p->num_threads_alive += 1;
	pthread_mutex_unlock(&thpool_p->thcount_lock);

	while(threads_keepalive){

		bsem_wait(thpool_p->__jobqueue.has_jobs);

		if (threads_keepalive){

			pthread_mutex_lock(&thpool_p->thcount_lock);
			thpool_p->num_threads_working++;
			pthread_mutex_unlock(&thpool_p->thcount_lock);

			/* Read job from queue and execute it */
			void* (*func_buff)(void*);
			void*  arg_buff;
			job* job_p = jobqueue_pull(&thpool_p->__jobqueue);
			if (job_p) {
				func_buff = job_p->function;
				arg_buff  = job_p->arg;
				func_buff(arg_buff);
				free(job_p);
			}

			pthread_mutex_lock(&thpool_p->thcount_lock);
			thpool_p->num_threads_working--;
			if (!thpool_p->num_threads_working) {
				pthread_cond_signal(&thpool_p->threads_all_idle);
			}
			pthread_mutex_unlock(&thpool_p->thcount_lock);

		}
	}
	pthread_mutex_lock(&thpool_p->thcount_lock);
	thpool_p->num_threads_alive --;
	pthread_mutex_unlock(&thpool_p->thcount_lock);

	return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %fs:0x28, %rax
movq %rax, -0x8(%rbp)
movq %rdi, -0x130(%rbp)
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x2190
leaq -0x90(%rbp), %rdi
movq -0x130(%rbp), %rax
movl (%rax), %edx
leaq 0x25fe(%rip), %rsi      # 0xb457
movb $0x0, %al
callq 0x23a0
leaq -0x90(%rbp), %rsi
movl $0xf, %edi
movb $0x0, %al
callq 0x2270
movq -0x130(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x138(%rbp)
leaq -0x128(%rbp), %rdi
addq $0x8, %rdi
callq 0x2250
movl $0x0, -0xa0(%rbp)
leaq 0x1da(%rip), %rax       # 0x9080
movq %rax, -0x128(%rbp)
movl $0xa, %edi
leaq -0x128(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2090
cmpl $-0x1, %eax
jne 0x8edf
movq 0x512a(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x258e(%rip), %rsi      # 0xb466
movb $0x0, %al
callq 0x2240
movq -0x138(%rbp), %rdi
addq $0x10, %rdi
callq 0x2400
movq -0x138(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movq -0x138(%rbp), %rdi
addq $0x10, %rdi
callq 0x22b0
movl 0x5517(%rip), %eax      # 0xe42c
cmpl $0x0, %eax
je 0x9025
movq -0x138(%rbp), %rax
movq 0xa0(%rax), %rdi
callq 0x90c0
movl 0x54f5(%rip), %eax      # 0xe42c
cmpl $0x0, %eax
je 0x9020
movq -0x138(%rbp), %rdi
addq $0x10, %rdi
callq 0x2400
movq -0x138(%rbp), %rax
movl 0xc(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc(%rax)
movq -0x138(%rbp), %rdi
addq $0x10, %rdi
callq 0x22b0
movq -0x138(%rbp), %rdi
addq $0x68, %rdi
callq 0x9110
movq %rax, -0x150(%rbp)
cmpq $0x0, -0x150(%rbp)
je 0x8fd1
movq -0x150(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x140(%rbp)
movq -0x150(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x148(%rbp)
movq -0x140(%rbp), %rax
movq -0x148(%rbp), %rdi
callq *%rax
movq -0x150(%rbp), %rdi
callq 0x2030
movq -0x138(%rbp), %rdi
addq $0x10, %rdi
callq 0x2400
movq -0x138(%rbp), %rax
movl 0xc(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0xc(%rax)
movq -0x138(%rbp), %rax
movl 0xc(%rax), %eax
cmpl $0x0, %eax
jne 0x9010
movq -0x138(%rbp), %rdi
addq $0x38, %rdi
callq 0x21f0
movq -0x138(%rbp), %rdi
addq $0x10, %rdi
callq 0x22b0
jmp 0x8f0f
movq -0x138(%rbp), %rdi
addq $0x10, %rdi
callq 0x2400
movq -0x138(%rbp), %rax
movl 0x8(%rax), %ecx
decl %ecx
movl %ecx, 0x8(%rax)
movq -0x138(%rbp), %rdi
addq $0x10, %rdi
callq 0x22b0
movq %fs:0x28, %rax
movq -0x8(%rbp), %rcx
cmpq %rcx, %rax
jne 0x9071
xorl %eax, %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
callq 0x2100
nopw %cs:(%rax,%rax)