// File: /workspace/github_c_makefile_star2_O1/sharpyfox[P]citrusleaf-client/src/cl_async.c
// Function: citrusleaf_async_init
int citrusleaf_async_init(int size_limit, int num_receiver_threads, cl_async_fail_cb fail_cb_fn, cl_async_success_cb success_cb_fn)
{
	int i, num_threads;

	//Make sure that we do the initialization only once
	if (1 == cf_atomic32_incr(&g_async_initialized)) {

		// Start the receiver threads
		num_threads = num_receiver_threads;
		if (num_threads > MAX_ASYNC_RECEIVER_THREADS) {
			//Limit the threads to the max value even if caller asks for it
			num_threads = MAX_ASYNC_RECEIVER_THREADS;
		}

#if ONEASYNCFD
		g_async_h_szlimit = size_limit * 3;	//Max number of elements in the hash table
		g_async_h_buckets = g_async_h_szlimit/10;//Number of buckets in the hash table

		if (shash_create(&g_cl_async_hashtab, async_trid_hash, sizeof(uint64_t), sizeof(cl_async_work *),
					g_async_h_buckets, SHASH_CR_MT_BIGLOCK) != SHASH_OK) {
			cf_error("Failed to initialize the async work hastable");
			cf_atomic32_decr(&g_async_initialized);
			return -1;
		}
#else
		// create work queue
		g_async_q_szlimit = size_limit;
		if ((g_cl_async_q = cf_queue_create(sizeof(cl_async_work *), true)) == NULL) {
			cf_error("Failed to initialize the async work queue");
			cf_atomic32_decr(&g_async_initialized);
			return -1;
		}

		for (i=0; i<num_threads; i++) {
			pthread_create(&g_async_reciever[i], 0, async_receiver_fn, NULL);
		}
		g_async_num_threads = num_threads;
#endif

		if ((g_cl_workitems_freepool_q = cf_queue_create(sizeof(cl_async_work *), true)) == NULL) {
			cf_error("Failed to create memory pool for workitems");
			return -1;
		}

		g_fail_cb_fn = fail_cb_fn;
		g_success_cb_fn = success_cb_fn;

		// Initialize the stats
		g_async_stats.retries = 0;
		g_async_stats.dropouts = 0;

	}
	
	return(0);	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl $0x1, %eax
lock
xaddl %eax, 0xe4d6(%rip)      # 0x194a8
xorl %r15d, %r15d
testl %eax, %eax
je 0xafeb
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, %rbx
movq %rdx, %r14
movl %esi, %ebp
cmpl $0x20, %esi
movl $0x20, %r13d
cmovll %esi, %r13d
movl %edi, 0xe492(%rip)      # 0x19498
movl $0x8, %edi
movl $0x1, %esi
callq 0xcd6c
leaq 0xe464(%rip), %rcx      # 0x19480
movq %rax, (%rcx)
testq %rax, %rax
je 0xb09a
testl %ebp, %ebp
jle 0xb051
movl %r13d, %ebp
leaq 0xe47e(%rip), %r15      # 0x194b0
leaq -0x4bf(%rip), %r12      # 0xab7a
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x24e0
addq $0x8, %r15
decq %rbp
jne 0xb039
movl %r13d, 0xe454(%rip)     # 0x194ac
movl $0x8, %edi
movl $0x1, %esi
callq 0xcd6c
leaq 0xe432(%rip), %rcx      # 0x194a0
movq %rax, (%rcx)
testq %rax, %rax
je 0xb0d5
movq %r14, 0xe533(%rip)      # 0x195b0
movq %rbx, 0xe534(%rip)      # 0x195b8
xorl %r15d, %r15d
movq %r15, 0xe3fa(%rip)      # 0x19488
movq %r15, 0xe3fb(%rip)      # 0x19490
jmp 0xafd9
leaq 0xc227(%rip), %rax      # 0x172c8
cmpl $0x0, (%rax)
js 0xb0bd
leaq 0xc223(%rip), %rax      # 0x172d0
movq (%rax), %rcx
leaq 0x6142(%rip), %rsi      # 0x111f9
xorl %edi, %edi
xorl %eax, %eax
callq *%rcx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
lock
xaddl %eax, 0xe3d8(%rip)      # 0x194a8
jmp 0xafd9
leaq 0xc1ec(%rip), %rax      # 0x172c8
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0x0, (%rax)
js 0xafd9
leaq 0xc1de(%rip), %rax      # 0x172d0
movq (%rax), %rcx
leaq 0x6127(%rip), %rsi      # 0x11223
xorl %edi, %edi
xorl %eax, %eax
callq *%rcx
jmp 0xafd9