// File: /workspace/github_cpp_makefile_star2_O3/flastest[P]HeterogeneousParallelDijkstra/dijkstra_cond_var.cc
// Function: parallel_dijkstra_thread(int&, int, std::priority_queue<std::pair<unsigned long, float>, std::vector<std::pair<unsigned long, float>, std::allocator<std::pair<unsigned long, float>>>, Offer_Comparator>&, std::map<unsigned long, float, std::less<unsigned long>, std::allocator<std::pair<unsigned long const, float>>>&, std::map<unsigned long, std::shared_ptr<std::mutex>, std::less<unsigned long>, std::allocator<std::pair<unsigned long const, std::shared_ptr<std::mutex>>>>&, std::map<unsigned long, std::shared_ptr<std::mutex>, std::less<unsigned long>, std::allocator<std::pair<unsigned long const, std::shared_ptr<std::mutex>>>>&, std::map<unsigned long, unsigned long, std::less<unsigned long>, std::allocator<std::pair<unsigned long const, unsigned long>>>&, boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, boost::no_property, boost::property<boost::edge_weight_t, int, boost::no_property>, boost::no_property, boost::listS> const&)
void parallel_dijkstra_thread(int& not_done,
					int thread_id,
					offer_pqueue_t& offer_pq,
					distance_map_t& distances,
					mutex_map_t& distance_mutexes,
					mutex_map_t& predecessor_mutexes,
					predecessor_map_t& predecessors,
					const graph_t& graph)
{
	//std::cout<<"hi, I'm " << thread_id <<std::endl;

	//if (DEBUG_THREAD) print_thread_debug(thread_id, "starting thread", debug_file_stream);
	//if (DEBUG) std::cout<<"starting thread: "<<thread_id<<"!!!!!!" <<std::endl;
	const auto weight = get(edge_weight, graph);
	//if (DEBUG) std::cout<< "is thread done? it shouldn't be. This should be true " <<!done[thread_id]<<std::endl;

	while (not_done > 0){
		if(!offer_pq.empty())
		{
			//if (DEBUG_THREAD) 
			//{

			//	print_thread_debug(thread_id, "offer_pq isn't empty", debug_file_stream);
			//	print_thread_debug(thread_id, std::to_string(offer_pq.size())+" amount of offers in offer pqueue", debug_file_stream);
			//}
			//if(DEBUG) std::cout<<"the offer pq isn't empty."<<std::endl;

			bool offer_found = false;

			offer_t offer;
			{
			//	print_thread_debug(thread_id, "locking offer", debug_file_stream);
			//	if (DEBUG) std::cout<<"locking offer"<<std::endl;
				std::lock_guard<std::mutex> guard(pq_mutex);
				if(!offer_pq.empty())
				{
					offer = offer_pq.top();
					offer_pq.pop();
			//		if (DEBUG_THREAD) print_thread_debug(thread_id, std::to_string(offer_pq.size())+" amount of offers in offer pqueue", debug_file_stream);
			//		if (DEBUG_THREAD) print_thread_debug(thread_id, "offer is locked. its node is "+std::to_string(offer.first)+" and its dist is " +std::to_string(offer.second), debug_file_stream);
					offer_found = true;
				}
			//	if (DEBUG_THREAD) print_thread_debug(thread_id, "unlocking offer", debug_file_stream);
			}	


			if (offer_found)
			{
			//	if (DEBUG) std::cout<< "we've obtained offer"<<std::endl; 
				const auto vertex = offer.first;
				const auto offer_distance = offer.second;
				bool explore = false;

			//	if (DEBUG) std::cout<<"offer distance is "<<offer_distance<<std::endl;
				{
					std::lock_guard<std::mutex> guard(*distance_mutexes[vertex]);
			//		if (DEBUG_THREAD) print_thread_debug(thread_id, "offer distance is " +std::to_string(offer_distance) + " and the already done distance is " + std::to_string(distances[vertex]), debug_file_stream);
			//		if(DEBUG) std::cout<<"offer distance is "<<offer_distance <<" and distances[vertex] is "<<distances[vertex] <<std::endl;
					if(offer_distance < distances[vertex])
					{
			//			if (DEBUG) std::cout <<"updating distances of " <<vertex<< " to offer_distance" <<std::endl;
			//			if (DEBUG_THREAD) print_thread_debug(thread_id, "updating distance of " + std::to_string(vertex) + " to offer distance", debug_file_stream);
						distances[vertex] = offer_distance;
						explore = true;
					}
				}


				if (explore) 
				{

					auto [start, finish] = out_edges(vertex,graph);

					for(;start != finish; start++)
					{

						vertex_descriptor neighbor = target(*start, graph);

						float weight_of_edge = get(weight, *start);

						float vd = offer_distance + weight_of_edge;

						//now checking to see if we shoud relax the neighbors
						relax_vertex(neighbor, 
							vertex,
						   	vd,  
			   				offer_pq,
			   				distance_mutexes, 
			   				distances,
			   				predecessor_mutexes,
							predecessors,
			   				thread_id );
			//			if(DEBUG_THREAD) print_thread_debug(thread_id, 
			//				"returned from relaxing. how many things in offer_pq now? " + std::to_string(offer_pq.size()), 
			//				debug_file_stream);

					}
				}
			}
		}
			
		else // priority  queue is empty
		{	
			//if(DEBUG_THREAD) print_thread_debug(thread_id, "pqueue is empty! not done is "+std::to_string(not_done), debug_file_stream);

			
			{
				//act as if there's no work to do
				std::unique_lock<std::mutex> guard(done_mutex);
				not_done--;
				//check to see if everyone else agrees that there's no work to do
				if (not_done == 0)
				{
					guard.unlock();
			//		if(DEBUG) std::cout<<"thread "<<thread_id<<" says no more threads have work to do!"<<std::endl;
			//		if(DEBUG_THREAD) print_thread_debug(thread_id, "okay, all the threads are done. returning.", debug_file_stream);
					
					//notify all, tell all the threads to check again so that if they were waiting, they no longer need to.
					cond_var_offer_pq.notify_all();
					return;
				}

				// if some threads are still at work, but the pqueue is empty, there could be more work to do in the future
				//wait here to wait for other threads to finish
				//some threads might either be about to relax and add more to the pqueue, or finding out that they're over too.
				else 
				{
					guard.unlock();
					std::unique_lock<std::mutex> lk(mutex_cond_var);
   					//condition variable wait
			//		if(DEBUG_THREAD) print_thread_debug(thread_id, "okay, I'm about to wait because pq is empty but not all threads r done", debug_file_stream);

    				cond_var_offer_pq.wait(lk);

    				//this thread might be notified to wake up in the case where the last thread finishes, so we need to check for
    				// everyone being done again.
    				std::lock_guard<std::mutex> done_guard(done_mutex);
    				if(not_done == 0){
    					//cond_var_offer_pq.notify_all();	
    					return;
    				}

					//in the case where the thread is notified because there's more work to do, increment not_done and go back to work
					not_done++;
				}
				//just to be on the safe side....
				guard.unlock();
			}
			
			
			//block until someone else is making progress


			//now the thread will go to sleep until the cond var wakes it up
			///std::unique_lock<std::mutex> lck(mutex_cond_var);
			
			//!adding_to_pq might also mean everything is done
			//cond_var_offer_pq.wait(lck);
	//		{
	//			std::lock_guard<std::mutex> guard(done_mutex);
	//			not_done++;
	//		}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, 0x60(%rsp)
movq %r8, 0x50(%rsp)
movq %rcx, 0x30(%rsp)
cmpl $0x0, (%rdi)
jle 0xc545
movq %rdx, %r12
movq %rdi, %r14
leaq 0xa003(%rip), %rbx      # 0x162f0
leaq 0x20(%rsp), %r15
leaq 0x40(%rsp), %r13
leaq 0x68(%rsp), %rbp
movq %rdi, 0x58(%rsp)
movq (%r12), %rax
cmpq 0x8(%r12), %rax
je 0xc442
leaq 0x9fb1(%rip), %rdi      # 0x162c8
callq 0x33a0
testl %eax, %eax
jne 0xc557
movq (%r12), %rax
cmpq 0x8(%r12), %rax
je 0xc4d5
movq (%rax), %rbx
movss 0x8(%rax), %xmm0
movss %xmm0, 0x1c(%rsp)
movq %r12, %rdi
callq 0xca7c
leaq 0x9f78(%rip), %rdi      # 0x162c8
callq 0x3210
movq %rbx, 0x20(%rsp)
movq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0xc8b0
movq (%rax), %r13
movq %r13, %rdi
callq 0x33a0
testl %eax, %eax
jne 0xc557
movq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0xc916
movss (%rax), %xmm0
ucomiss 0x1c(%rsp), %xmm0
jbe 0xc4e3
movq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0xc916
movss 0x1c(%rsp), %xmm0
movss %xmm0, (%rax)
movq %r13, %rdi
callq 0x3210
movq 0x20(%rsp), %rax
movq 0xd8(%rsp), %rcx
movq 0x18(%rcx), %rcx
shlq $0x5, %rax
leaq (%rcx,%rax), %r14
movq (%rcx,%rax), %r15
cmpq %r14, %r15
je 0xc4eb
movq 0x18(%r15), %rax
xorps %xmm0, %xmm0
cvtsi2ssl (%rax), %xmm0
movq 0x10(%r15), %r13
addss 0x1c(%rsp), %xmm0
movss %xmm0, 0x3c(%rsp)
movq 0x20(%rsp), %rbx
movq %rbp, %rdi
movq 0x50(%rsp), %rsi
callq 0xcc52
movq 0xd0(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movss 0x3c(%rsp), %xmm0
movq %r12, %rdx
movq %rbp, %rcx
movq 0x30(%rsp), %r8
movq 0x60(%rsp), %r9
callq 0xbff8
movq %rbp, %rdi
callq 0xce78
movq (%r15), %r15
jmp 0xc3d2
movq %rbx, 0x20(%rsp)
movb $0x0, 0x28(%rsp)
movq %r15, %rdi
callq 0xd63e
movb $0x1, 0x28(%rsp)
decl (%r14)
je 0xc50d
movq %r15, %rdi
callq 0xcace
leaq 0x9ea7(%rip), %rax      # 0x16318
movq %rax, 0x40(%rsp)
movb $0x0, 0x48(%rsp)
movq %r13, %rdi
callq 0xd63e
movb $0x1, 0x48(%rsp)
leaq 0x9eb1(%rip), %rdi      # 0x16340
movq %r13, %rsi
callq 0x30d0
movq %rbx, %rdi
callq 0x33a0
testl %eax, %eax
jne 0xc55e
movl (%r14), %eax
testl %eax, %eax
je 0xc525
incl %eax
movl %eax, (%r14)
movq %rbx, %rdi
callq 0x3210
movq %r13, %rdi
callq 0xcaf6
movq %r15, %rdi
callq 0xcace
movq %r15, %rdi
callq 0xcaf6
jmp 0xc501
leaq 0x9dec(%rip), %rdi      # 0x162c8
callq 0x3210
jmp 0xc501
movq %r13, %rdi
callq 0x3210
movq 0x58(%rsp), %r14
leaq 0x9df9(%rip), %rbx      # 0x162f0
leaq 0x20(%rsp), %r15
leaq 0x40(%rsp), %r13
cmpl $0x0, (%r14)
jg 0xc301
jmp 0xc545
leaq 0x20(%rsp), %rdi
callq 0xcace
leaq 0x9e22(%rip), %rdi      # 0x16340
callq 0x3450
jmp 0xc53b
leaq 0x9dc4(%rip), %rdi      # 0x162f0
callq 0x3210
leaq 0x40(%rsp), %rdi
callq 0xcaf6
leaq 0x20(%rsp), %rdi
callq 0xcaf6
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x31c0
movl %eax, %edi
callq 0x31c0
jmp 0xc591
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0xcaf6
jmp 0xc594
jmp 0xc591
movq %rax, %rbx
leaq 0x9d46(%rip), %rdi      # 0x162c8
jmp 0xc58a
movq %rax, %rbx
movq %r13, %rdi
callq 0x3210
jmp 0xc5ad
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0xcaf6
jmp 0xc5ad
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0xce78
movq %rbx, %rdi
callq 0x34a0