// File: tewalds[P]pentagod/agentpns.cpp
// Function: AgentPNS::PNSThread::run()
void AgentPNS::PNSThread::run(){
	while(true){
		switch(agent->threadstate){
		case Thread_Cancelled:  //threads should exit
			return;

		case Thread_Wait_Start: //threads are waiting to start
		case Thread_Wait_Start_Cancelled:
			agent->runbarrier.wait();
			CAS(agent->threadstate, Thread_Wait_Start, Thread_Running);
			CAS(agent->threadstate, Thread_Wait_Start_Cancelled, Thread_Cancelled);
			break;

		case Thread_Wait_End:   //threads are waiting to end
			agent->runbarrier.wait();
			CAS(agent->threadstate, Thread_Wait_End, Thread_Wait_Start);
			break;

		case Thread_Running:    //threads are running
			if(agent->root.terminal()){ //solved
				CAS(agent->threadstate, Thread_Running, Thread_Wait_End);
				break;
			}
			if(agent->ctmem.memalloced() >= agent->memlimit){ //out of memory, start garbage collection
				CAS(agent->threadstate, Thread_Running, Thread_GC);
				break;
			}

			pns(agent->rootboard, &agent->root, 0, INF32/2, INF32/2);
			break;

		case Thread_GC:         //one thread is running garbage collection, the rest are waiting
		case Thread_GC_End:     //once done garbage collecting, go to wait_end instead of back to running
			if(agent->gcbarrier.wait()){
				logerr("Starting solver GC with limit " + to_str(agent->gclimit) + " ... ");

				Time starttime;
				agent->garbage_collect(& agent->root);

				Time gctime;
				agent->ctmem.compact(1.0, 0.75);

				Time compacttime;
				logerr(to_str(100.0*agent->ctmem.meminuse()/agent->memlimit, 1) + " % of tree remains - " +
					to_str((gctime - starttime)*1000, 0)  + " msec gc, " + to_str((compacttime - gctime)*1000, 0) + " msec compact\n");

				if(agent->ctmem.meminuse() >= agent->memlimit/2)
					agent->gclimit = (unsigned int)(agent->gclimit*1.3);
				else if(agent->gclimit > 5)
					agent->gclimit = (unsigned int)(agent->gclimit*0.9); //slowly decay to a minimum of 5

				CAS(agent->threadstate, Thread_GC,     Thread_Running);
				CAS(agent->threadstate, Thread_GC_End, Thread_Wait_End);
			}
			agent->gcbarrier.wait();
			break;
		}
	}
}

// Assembly:
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rdi, 0x1f0(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movl 0x9f0(%rax), %eax
movq %rax, 0x20(%rsp)
subq $0x6, %rax
ja 0x3426e
movq 0x20(%rsp), %rax
leaq 0xd7e2(%rip), %rcx      # 0x41308
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
addq $0x1f8, %rsp            # imm = 0x1F8
retq
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0xa10, %rdi            # imm = 0xA10
callq 0x2c5d0
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x9f0, %rdi            # imm = 0x9F0
movl $0x1, 0x1ec(%rsp)
movl $0x3, 0x1e8(%rsp)
leaq 0x1ec(%rsp), %rsi
leaq 0x1e8(%rsp), %rdx
callq 0x35c60
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x9f0, %rdi            # imm = 0x9F0
movl $0x2, 0x1e4(%rsp)
movl $0x0, 0x1e0(%rsp)
leaq 0x1e4(%rsp), %rsi
leaq 0x1e0(%rsp), %rdx
callq 0x35c60
jmp 0x3426e
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0xa10, %rdi            # imm = 0xA10
callq 0x2c5d0
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x9f0, %rdi            # imm = 0x9F0
movl $0x6, 0x1dc(%rsp)
movl $0x1, 0x1d8(%rsp)
leaq 0x1dc(%rsp), %rsi
leaq 0x1d8(%rsp), %rdx
callq 0x35c60
jmp 0x3426e
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0xae0, %rdi            # imm = 0xAE0
callq 0x35c90
testb $0x1, %al
jne 0x33c37
jmp 0x33c77
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x9f0, %rdi            # imm = 0x9F0
movl $0x3, 0x1d4(%rsp)
movl $0x6, 0x1d0(%rsp)
leaq 0x1d4(%rsp), %rsi
leaq 0x1d0(%rsp), %rdx
callq 0x35c60
jmp 0x3426e
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x50, %rdi
callq 0x35cd0
movq 0x28(%rsp), %rcx
movq 0x38(%rcx), %rcx
cmpq 0x40(%rcx), %rax
jb 0x33cd8
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x9f0, %rdi            # imm = 0x9F0
movl $0x3, 0x1cc(%rsp)
movl $0x4, 0x1c8(%rsp)
leaq 0x1cc(%rsp), %rsi
leaq 0x1c8(%rsp), %rdx
callq 0x35c60
jmp 0x3426e
movq 0x28(%rsp), %rdi
movq 0x38(%rdi), %rsi
addq $0x10, %rsi
movq 0x38(%rdi), %rdx
addq $0xae0, %rdx            # imm = 0xAE0
xorl %ecx, %ecx
movl $0x1ffffffe, %r9d       # imm = 0x1FFFFFFE
movl %r9d, %r8d
callq 0x34280
jmp 0x3426e
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0xa70, %rdi            # imm = 0xA70
callq 0x2c5d0
testb $0x1, %al
jne 0x33d23
jmp 0x34259
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movl 0x48(%rax), %esi
leaq 0x168(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0xe560
movq 0x18(%rsp), %rdx
leaq 0xd661(%rip), %rsi      # 0x413ae
leaq 0x188(%rsp), %rdi
callq 0xe5f0
jmp 0x33d5c
leaq 0xc907(%rip), %rdx      # 0x4066a
leaq 0x1a8(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x93b0
jmp 0x33d7a
leaq 0x1a8(%rsp), %rdi
callq 0x15ad0
jmp 0x33d89
leaq 0x1a8(%rsp), %rdi
callq 0x4210
leaq 0x188(%rsp), %rdi
callq 0x4210
leaq 0x168(%rsp), %rdi
callq 0x4210
leaq 0x150(%rsp), %rdi
callq 0x5500
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
movq %rdi, %rsi
addq $0xae0, %rsi            # imm = 0xAE0
callq 0x34ad0
leaq 0x148(%rsp), %rdi
callq 0x5500
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x50, %rdi
vmovss 0xc791(%rip), %xmm0     # 0x40588
vmovss 0xc78d(%rip), %xmm1     # 0x4058c
callq 0x35cf0
leaq 0x140(%rsp), %rdi
callq 0x5500
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x50, %rdi
callq 0x362e0
movq %rax, %rcx
movq 0x28(%rsp), %rax
vcvtusi2sd %rcx, %xmm0, %xmm0
vmovsd 0xa45f(%rip), %xmm1     # 0x3e298
vmulsd %xmm1, %xmm0, %xmm0
movq 0x38(%rax), %rax
vcvtusi2sdq 0x40(%rax), %xmm1, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
leaq 0x80(%rsp), %rdi
movq %rdi, 0x10(%rsp)
movl $0x1, %esi
callq 0x1e870
movq 0x10(%rsp), %rsi
leaq 0xc801(%rip), %rdx      # 0x40670
leaq 0xa0(%rsp), %rdi
callq 0x93b0
jmp 0x33e7e
leaq 0x148(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x116e0
vmovsd %xmm0, 0x8(%rsp)
jmp 0x33e9b
vmovsd 0x8(%rsp), %xmm0
vmovsd 0x9167(%rip), %xmm1     # 0x3d010
vmulsd %xmm1, %xmm0, %xmm0
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x1e870
jmp 0x33ebb
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0xe470
jmp 0x33ed7
leaq 0xc7a8(%rip), %rdx      # 0x40686
leaq 0xe0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x93b0
jmp 0x33ef5
leaq 0x140(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x116e0
vmovsd %xmm0, (%rsp)
jmp 0x33f11
vmovsd (%rsp), %xmm0
vmovsd 0x90f2(%rip), %xmm1     # 0x3d010
vmulsd %xmm1, %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x1e870
jmp 0x33f30
leaq 0x100(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0xe470
jmp 0x33f4c
leaq 0xc73e(%rip), %rdx      # 0x40691
leaq 0x120(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x93b0
jmp 0x33f6a
leaq 0x120(%rsp), %rdi
callq 0x15ad0
jmp 0x33f79
leaq 0x120(%rsp), %rdi
callq 0x4210
leaq 0x100(%rsp), %rdi
callq 0x4210
leaq 0x40(%rsp), %rdi
callq 0x4210
leaq 0xe0(%rsp), %rdi
callq 0x4210
leaq 0xc0(%rsp), %rdi
callq 0x4210
leaq 0x60(%rsp), %rdi
callq 0x4210
leaq 0xa0(%rsp), %rdi
callq 0x4210
leaq 0x80(%rsp), %rdi
callq 0x4210
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x50, %rdi
callq 0x362e0
movq 0x28(%rsp), %rcx
movq 0x38(%rcx), %rcx
movq 0x40(%rcx), %rcx
shrq %rcx
cmpq %rcx, %rax
jb 0x341c1
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rcx
vcvtusi2sdl 0x48(%rcx), %xmm0, %xmm0
vmovsd 0xc582(%rip), %xmm1     # 0x405a0
vmulsd %xmm1, %xmm0, %xmm0
vcvttsd2usi %xmm0, %ecx
movq 0x38(%rax), %rax
movl %ecx, 0x48(%rax)
jmp 0x341fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x3408e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x34081
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
leaq 0x1a8(%rsp), %rdi
callq 0x4210
leaq 0x188(%rsp), %rdi
callq 0x4210
leaq 0x168(%rsp), %rdi
callq 0x4210
jmp 0x34273
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x341af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x341a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x34198
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x3418b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x3417e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x34174
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x34167
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x4210
leaq 0x100(%rsp), %rdi
callq 0x4210
leaq 0x40(%rsp), %rdi
callq 0x4210
leaq 0xe0(%rsp), %rdi
callq 0x4210
leaq 0xc0(%rsp), %rdi
callq 0x4210
leaq 0x60(%rsp), %rdi
callq 0x4210
leaq 0xa0(%rsp), %rdi
callq 0x4210
leaq 0x80(%rsp), %rdi
callq 0x4210
jmp 0x34273
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
cmpl $0x5, 0x48(%rax)
jbe 0x341f9
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rcx
vcvtusi2sdl 0x48(%rcx), %xmm0, %xmm0
vmovsd 0xc3b0(%rip), %xmm1     # 0x40598
vmulsd %xmm1, %xmm0, %xmm0
vcvttsd2usi %xmm0, %ecx
movq 0x38(%rax), %rax
movl %ecx, 0x48(%rax)
jmp 0x341fb
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x9f0, %rdi            # imm = 0x9F0
movl $0x4, 0x3c(%rsp)
movl $0x3, 0x38(%rsp)
leaq 0x3c(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x35c60
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x9f0, %rdi            # imm = 0x9F0
movl $0x5, 0x34(%rsp)
movl $0x6, 0x30(%rsp)
leaq 0x34(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x35c60
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0xa70, %rdi            # imm = 0xA70
callq 0x2c5d0
jmp 0x33afc
movq 0x160(%rsp), %rdi
callq 0x4660