// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdi, %rbx
movq %fs:0x28, %rax
movq %rax, -0x30(%rbp)
xorps %xmm0, %xmm0
leaq -0xb0(%rbp), %r14
movaps %xmm0, 0x70(%r14)
movaps %xmm0, 0x60(%r14)
movaps %xmm0, 0x50(%r14)
movaps %xmm0, 0x40(%r14)
movaps %xmm0, 0x30(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, (%r14)
movl (%rdi), %edx
leaq 0x2318(%rip), %rsi      # 0x847a
movq %r14, %rdi
xorl %eax, %eax
callq 0x2390
pushq $0xf
popq %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x2270
movq 0x10(%rbx), %r15
leaq -0x140(%rbp), %rbx
movq %rbx, %rdi
callq 0x2250
andl $0x0, 0x80(%rbx)
leaq 0x14e(%rip), %rax       # 0x62e8
movq %rax, -0x8(%rbx)
pushq $0xa
popq %rdi
leaq -0x148(%rbp), %rsi
xorl %edx, %edx
callq 0x20a0
cmpl $-0x1, %eax
je 0x62c2
leaq 0x10(%r15), %rbx
movq %rbx, %rdi
callq 0x23f0
incl 0x8(%r15)
movq %rbx, %rdi
callq 0x22b0
leaq 0x68(%r15), %r14
leaq 0x38(%r15), %rax
movq %rax, -0x150(%rbp)
cmpl $0x0, 0x5246(%rip)      # 0xb42c
je 0x628b
movq 0xa0(%r15), %r12
movq %r12, %rdi
callq 0x23f0
leaq 0x28(%r12), %r13
cmpl $0x1, 0x58(%r12)
je 0x6215
movq %r13, %rdi
movq %r12, %rsi
callq 0x20e0
jmp 0x6200
andl $0x0, 0x58(%r12)
movq %r12, %rdi
callq 0x22b0
cmpl $0x0, 0x5202(%rip)      # 0xb42c
je 0x61df
movq %rbx, %rdi
callq 0x23f0
incl 0xc(%r15)
movq %rbx, %rdi
callq 0x22b0
movq %r14, %rdi
callq 0x6314
testq %rax, %rax
je 0x625f
movq %rax, %r12
movq 0x10(%rax), %rdi
callq *0x8(%rax)
movq %r12, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x23f0
decl 0xc(%r15)
cmpl $0x0, 0xc(%r15)
jne 0x627e
movq -0x150(%rbp), %rdi
callq 0x21f0
movq %rbx, %rdi
callq 0x22b0
jmp 0x61df
movq %rbx, %rdi
callq 0x23f0
decl 0x8(%r15)
movq %rbx, %rdi
callq 0x22b0
movq %fs:0x28, %rax
cmpq -0x30(%rbp), %rax
jne 0x62e3
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4d2f(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x21b6(%rip), %rdi      # 0x8489
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x23b0
jmp 0x61b8
callq 0x2110