// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %rax
lock
xaddl %eax, 0xd1d1(%rip)      # 0x174a8
xorl %r15d, %r15d
testl %eax, %eax
je 0xa2f0
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
cmpl $0x20, %esi
pushq $0x20
popq %rbp
cmovll %esi, %ebp
movl %edi, 0xd193(%rip)      # 0x17498
pushq $0x8
popq %rdi
pushq $0x1
popq %rsi
callq 0xbe10
leaq 0xd169(%rip), %rcx      # 0x17480
testq %rax, %rax
movq %rax, (%rcx)
je 0xa395
xorl %r13d, %r13d
testl %ebp, %ebp
leaq 0xd185(%rip), %r15      # 0x174b0
leaq -0x404(%rip), %r12      # 0x9f2e
cmovgl %ebp, %r13d
subq $0x1, %r13
jb 0xa351
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x24e0
addq $0x8, %r15
jmp 0xa336
movl %ebp, 0xd155(%rip)      # 0x174ac
pushq $0x8
popq %rdi
pushq $0x1
popq %rsi
callq 0xbe10
leaq 0xd137(%rip), %rcx      # 0x174a0
testq %rax, %rax
movq %rax, (%rcx)
je 0xa3cc
xorl %r15d, %r15d
movq %r14, 0xd235(%rip)      # 0x175b0
movq %rbx, 0xd236(%rip)      # 0x175b8
movq %r15, 0xd0ff(%rip)      # 0x17488
movq %r15, 0xd100(%rip)      # 0x17490
jmp 0xa2de
leaq 0xaf2c(%rip), %rax      # 0x152c8
cmpl $0x0, (%rax)
js 0xa3b8
leaq 0xaf28(%rip), %rax      # 0x152d0
leaq 0x4e7a(%rip), %rsi      # 0xf229
xorl %edi, %edi
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
pushq $-0x1
popq %r15
movl %r15d, %eax
lock
xaddl %eax, 0xd0e1(%rip)      # 0x174a8
jmp 0xa2de
leaq 0xaef5(%rip), %rax      # 0x152c8
pushq $-0x1
popq %r15
cmpl $0x0, (%rax)
js 0xa2de
leaq 0xaee9(%rip), %rax      # 0x152d0
leaq 0x4e65(%rip), %rsi      # 0xf253
xorl %edi, %edi
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
jmp 0xa2de