// File: /workspace/github_c_makefile_star2_O1/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 0x3031
movq %rax, %r14
callq 0x3031
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, %r15
movq 0x18(%r14,%r15,8), %rbx
testq %rbx, %rbx
je 0x2523
cmpl %ebp, 0x10(%rbx)
je 0x2525
xorl %ebx, %ebx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testq %rbx, %rbx
je 0x273d
cmpl %ebp, 0x10(%rbx)
jne 0x273d
cmpl $0x1, 0x50(%rbx)
je 0x273d
movq %r14, (%rsp)
movq 0x140c8(%rax), %r12
movl $0x1, 0x50(%rbx)
leaq 0x38(%rbx), %r13
movq 0x38(%rbx), %r14
cmpq %r14, %r13
je 0x2625
leaq 0x118(%r12), %rbp
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq %rax, (%rcx)
movq (%r14), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%r14), %rax
addq $-0x100, %r14
testq %rax, %rax
cmovneq %rax, %r14
callq 0x3031
leaq 0x140d0(%rax), %rcx
leaq 0x118(%r14), %rdx
movq 0x140d8(%rax), %rsi
movq %rsi, 0x120(%r14)
movq %rdx, (%rsi)
movq %rcx, 0x118(%r14)
movq %rdx, 0x140d8(%rax)
testq %r12, %r12
je 0x25f8
callq 0x3031
leaq 0x140d0(%rax), %rcx
movq 0x140d8(%rax), %rdx
movq %rdx, 0x120(%r12)
movq %rbp, (%rdx)
movq %rcx, 0x118(%r12)
movq %rbp, 0x140d8(%rax)
callq 0x3031
movq 0x140c0(%rax), %rsi
movq 0x140c8(%rax), %rdi
addq $0x48, %rdi
addq $0x48, %rsi
callq 0x312c
movq (%r13), %r14
cmpq %r14, %r13
jne 0x2571
movl 0x14(%rbx), %eax
cmpl 0x18(%rbx), %eax
jne 0x263b
leaq 0x20(%rbx), %rax
cmpq 0x20(%rbx), %rax
je 0x2726
movq 0x20(%rbx), %r15
addq $0x20, %rbx
cmpq %r15, %rbx
je 0x273b
leaq 0x118(%r12), %r13
leaq 0x1a67(%rip), %r14      # 0x40c2
movq (%r15), %rax
movq 0x8(%r15), %rcx
movq %rax, (%rcx)
movq (%r15), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%r15), %rax
addq $-0x100, %r15
testq %rax, %rax
cmovneq %rax, %r15
cmpq %r12, %r15
je 0x2718
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x1120
callq 0x3031
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 0x26f8
callq 0x3031
leaq 0x140d0(%rax), %rcx
movq 0x140d8(%rax), %rdx
movq %rdx, 0x120(%r12)
movq %r13, (%rdx)
movq %rcx, 0x118(%r12)
movq %r13, 0x140d8(%rax)
callq 0x3031
movq 0x140c0(%rax), %rsi
movq 0x140c8(%rax), %rdi
addq $0x48, %rdi
addq $0x48, %rsi
callq 0x312c
movq (%rbx), %r15
cmpq %r15, %rbx
jne 0x265b
jmp 0x273b
movq (%rsp), %rax
movq $0x0, 0x18(%rax,%r15,8)
movq %rbx, %rdi
callq 0x1030
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq