// File: /workspace/github_c_makefile_star2_O1/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 0x1db5(%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       # 0x684c
movq %rax, -0x8(%rbx)
leaq -0x148(%rbp), %rsi
movl $0xa, %edi
xorl %edx, %edx
callq 0x20a0
cmpl $-0x1, %eax
je 0x683d
leaq 0x10(%r15), %rbx
movq %rbx, %rdi
callq 0x23f0
incl 0x8(%r15)
movq %rbx, %rdi
callq 0x22b0
cmpl $0x0, 0x4ceb(%rip)      # 0xb42c
je 0x6806
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 0x6785
leaq 0x28(%r12), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x20e0
cmpl $0x1, 0x58(%r12)
jne 0x6772
movl $0x0, 0x58(%r12)
movq %r12, %rdi
callq 0x22b0
cmpl $0x0, 0x4c8f(%rip)      # 0xb42c
je 0x67f9
movq %rbx, %rdi
callq 0x23f0
incl 0xc(%r15)
movq %rbx, %rdi
callq 0x22b0
movq %r14, %rdi
callq 0x6878
testq %rax, %rax
je 0x67d2
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 0x67f1
movq -0x150(%rbp), %rdi
callq 0x21f0
movq %rbx, %rdi
callq 0x22b0
cmpl $0x0, 0x4c2c(%rip)      # 0xb42c
jne 0x6756
movq %rbx, %rdi
callq 0x23f0
decl 0x8(%r15)
movq %rbx, %rdi
callq 0x22b0
movq %fs:0x28, %rax
cmpq -0x30(%rbp), %rax
jne 0x6847
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x28ee
jmp 0x6722
callq 0x2110