// File: /workspace/github_c_makefile_star2_O3/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, 0xd7e8(%rip)      # 0x184a8
xorl %r15d, %r15d
testl %eax, %eax
je 0xacd9
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x20, %esi
movl $0x20, %r13d
movl %edi, 0xd7b0(%rip)      # 0x18498
movl %esi, %ebp
movl $0x8, %edi
movq %rcx, %rbx
movq %rdx, %r14
cmovll %esi, %r13d
movl $0x1, %esi
callq 0xc964
leaq 0xd776(%rip), %rcx      # 0x18480
testq %rax, %rax
movq %rax, (%rcx)
je 0xad88
testl %ebp, %ebp
jle 0xad3f
movl %r13d, %ebp
leaq 0xd790(%rip), %r15      # 0x184b0
leaq -0x4ca(%rip), %r12      # 0xa85d
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x24e0
addq $0x8, %r15
decq %rbp
jne 0xad27
movl $0x8, %edi
movl $0x1, %esi
movl %r13d, 0xd75c(%rip)     # 0x184ac
callq 0xc964
leaq 0xd744(%rip), %rcx      # 0x184a0
testq %rax, %rax
movq %rax, (%rcx)
je 0xadc3
xorl %r15d, %r15d
movq %r14, 0xd842(%rip)      # 0x185b0
movq %rbx, 0xd843(%rip)      # 0x185b8
movq %r15, 0xd70c(%rip)      # 0x18488
movq %r15, 0xd70d(%rip)      # 0x18490
jmp 0xacc7
leaq 0xb539(%rip), %rax      # 0x162c8
cmpl $0x0, (%rax)
js 0xadab
leaq 0xb535(%rip), %rax      # 0x162d0
leaq 0x5477(%rip), %rsi      # 0x10219
xorl %edi, %edi
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
lock
xaddl %eax, 0xd6f0(%rip)      # 0x184a8
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0xacc7
leaq 0xb4fe(%rip), %rax      # 0x162c8
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0x0, (%rax)
js 0xacc7
leaq 0xb4f0(%rip), %rax      # 0x162d0
leaq 0x545c(%rip), %rsi      # 0x10243
xorl %edi, %edi
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
jmp 0xacc7