// File: /workspace/github_c_makefile_star2_O3/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 0x1e59(%rip), %rsi      # 0x847a
movq %r14, %rdi
xorl %eax, %eax
callq 0x2390
movl $0xf, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x2270
movq 0x10(%rbx), %r15
leaq -0x140(%rbp), %rbx
movq %rbx, %rdi
callq 0x2250
movl $0x0, 0x80(%rbx)
leaq 0x14a(%rip), %rax       # 0x67a8
movq %rax, -0x8(%rbx)
leaq -0x148(%rbp), %rsi
movl $0xa, %edi
xorl %edx, %edx
callq 0x20a0
cmpl $-0x1, %eax
je 0x6799
leaq 0x10(%r15), %rbx
movq %rbx, %rdi
callq 0x23f0
incl 0x8(%r15)
movq %rbx, %rdi
callq 0x22b0
cmpl $0x0, 0x4d8f(%rip)      # 0xb42c
je 0x6762
leaq 0x68(%r15), %r14
leaq 0x38(%r15), %rax
movq %rax, -0x150(%rbp)
movq 0xa0(%r15), %r12
movq %r12, %rdi
callq 0x23f0
cmpl $0x1, 0x58(%r12)
je 0x66e1
leaq 0x28(%r12), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x20e0
cmpl $0x1, 0x58(%r12)
jne 0x66ce
movl $0x0, 0x58(%r12)
movq %r12, %rdi
callq 0x22b0
cmpl $0x0, 0x4d33(%rip)      # 0xb42c
je 0x6755
movq %rbx, %rdi
callq 0x23f0
incl 0xc(%r15)
movq %rbx, %rdi
callq 0x22b0
movq %r14, %rdi
callq 0x67d4
testq %rax, %rax
je 0x672e
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 0x674d
movq -0x150(%rbp), %rdi
callq 0x21f0
movq %rbx, %rdi
callq 0x22b0
cmpl $0x0, 0x4cd0(%rip)      # 0xb42c
jne 0x66b2
movq %rbx, %rdi
callq 0x23f0
decl 0x8(%r15)
movq %rbx, %rdi
callq 0x22b0
movq %fs:0x28, %rax
cmpq -0x30(%rbp), %rax
jne 0x67a3
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x28ee
jmp 0x667e
callq 0x2110