// File: /workspace/github_c_makefile_star2_O3/whtc123[P]libuvc/uvc.c
// Function: channel_close
int channel_close(channel_t c){
	channel_pool *pool=get_chan_pool();
	channel *chan;
	uvc_ctx *ctx;
	uvc_ctx *ctx_self =uvc_self();
	chan=channel_pool_get(pool,c);
	if(chan == NULL ||chan->id !=c || chan->closeing==1){
		return -1;
	}
	chan->closeing=1;
	
	
	//唤醒所有发送队列，让发生者知道已经发送失败。
	do
    {
        if(queue_empty(&chan->writq) )break;
		ctx = channel_queue_get(&chan->writq);
		uvc_ready(ctx);
		uvc_ready(ctx_self);
		uvc_yield();
    }while(1);


	//如果chanbuf中还有数据，那么不能立即关闭管道，
	//否则发送放无法获知释放已经送达数据。
	if (chanbuf_empty(chan) && queue_empty(&chan->readq)){
		channel_pool_remove(pool, c);
		free(chan); 
	}else{
		do
	    {
	        if(queue_empty(&chan->readq) )break;
			ctx = channel_queue_get(&chan->readq);
			if (ctx!=ctx_self){
				printf("channel closeing,wakeup task[%s]\n",ctx->name);
				uvc_ready(ctx);
				uvc_ready(ctx_self);
				uvc_yield();
			}
	    }while(1);
	}
	
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edi, %ebp
callq 0x2fcb
movq %rax, %rbx
callq 0x2fcb
movslq %ebp, %rcx
imulq $0x66666667, %rcx, %rdx # imm = 0x66666667
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x2c, %rdx
addl %esi, %edx
shll $0xb, %edx
leal (%rdx,%rdx,4), %edx
subl %edx, %ecx
movslq %ecx, %r13
movq 0x18(%rbx,%r13,8), %r14
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testq %r14, %r14
je 0x2716
cmpl %ebp, 0x10(%r14)
jne 0x2716
cmpl $0x1, 0x50(%r14)
je 0x2716
movq %rbx, (%rsp)
movq 0x140c8(%rax), %r12
movl $0x1, 0x50(%r14)
leaq 0x38(%r14), %r15
movq 0x38(%r14), %rbx
cmpq %rbx, %r15
je 0x25fb
leaq 0x118(%r12), %rbp
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq %rax, (%rcx)
movq %rcx, 0x8(%rax)
movq 0x10(%rbx), %rax
addq $-0x100, %rbx
testq %rax, %rax
cmovneq %rax, %rbx
callq 0x2fcb
leaq 0x140d0(%rax), %rcx
leaq 0x118(%rbx), %rdx
movq 0x140d8(%rax), %rsi
movq %rsi, 0x120(%rbx)
movq %rdx, (%rsi)
movq %rcx, 0x118(%rbx)
movq %rdx, 0x140d8(%rax)
testq %r12, %r12
je 0x25cf
callq 0x2fcb
leaq 0x140d0(%rax), %rcx
movq 0x140d8(%rax), %rdx
movq %rdx, 0x120(%r12)
movq %rbp, (%rdx)
movq %rcx, 0x118(%r12)
movq %rbp, 0x140d8(%rax)
callq 0x2fcb
movq 0x140c0(%rax), %rsi
movq 0x140c8(%rax), %rdi
addq $0x48, %rdi
addq $0x48, %rsi
callq 0x30b0
movq (%r15), %rbx
cmpq %rbx, %r15
jne 0x254b
movl 0x14(%r14), %eax
cmpl 0x18(%r14), %eax
sete %al
leaq 0x20(%r14), %rbp
movq 0x20(%r14), %r15
cmpq %r15, %rbp
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x2635
movq (%rsp), %rax
movq $0x0, 0x18(%rax,%r13,8)
movq %r14, %rdi
callq 0x1030
jmp 0x2714
cmpq %r15, %rbp
je 0x2714
leaq 0x118(%r12), %r14
leaq 0x1a75(%rip), %rbx      # 0x40c2
movq (%r15), %rax
movq 0x8(%r15), %rcx
movq %rax, (%rcx)
movq %rcx, 0x8(%rax)
movq 0x10(%r15), %rax
addq $-0x100, %r15
testq %rax, %rax
cmovneq %rax, %r15
cmpq %r12, %r15
je 0x2707
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x1120
callq 0x2fcb
leaq 0x140d0(%rax), %rcx
leaq 0x118(%r15), %rdx
movq 0x140d8(%rax), %rsi
movq %rsi, 0x120(%r15)
movq %rdx, (%rsi)
movq %rcx, 0x118(%r15)
movq %rdx, 0x140d8(%rax)
testq %r12, %r12
je 0x26e7
callq 0x2fcb
leaq 0x140d0(%rax), %rcx
movq 0x140d8(%rax), %rdx
movq %rdx, 0x120(%r12)
movq %r14, (%rdx)
movq %rcx, 0x118(%r12)
movq %r14, 0x140d8(%rax)
callq 0x2fcb
movq 0x140c0(%rax), %rsi
movq 0x140c8(%rax), %rdi
addq $0x48, %rdi
addq $0x48, %rsi
callq 0x30b0
movq (%rbp), %r15
cmpq %r15, %rbp
jne 0x264d
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq