// File: /workspace/github_cpp_makefile_star2_O2/flastest[P]HeterogeneousParallelDijkstra/dijkstra_cond_var.cc
// Function: parallel_dijkstra(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&, unsigned long const&, std::map<unsigned long, unsigned long, std::less<unsigned long>, std::allocator<std::pair<unsigned long const, unsigned long>>>&, int)
distance_map_t parallel_dijkstra(const graph_t &graph, 
						const vertex_descriptor &source, 
						predecessor_map_t &predecessors,
						int NUM_THREADS)
{

//	debug_file_stream.open("debuglog"+std::to_string(NUM_THREADS)+".txt");
//	if(DEBUG_THREAD) print_thread_debug(NUM_THREADS+1, "initial print for cond variable. that's the number of threads ", debug_file_stream);
	
	//all the threads, pretty self explanatory
	vector<thread> threads;

	mutex_map_t distance_mutexes;

	//I need a new pqueue for all the offers.
	offer_pqueue_t offer_pq;

	//let's the mutex list for the predecessors
	mutex_map_t predecessor_mutexes;

	//this keeps track of the threads that are done
	int done = NUM_THREADS;

	distance_map_t distances = initialize_distances_from_source_and_mutex_maps(graph, distance_mutexes, predecessor_mutexes);


	//for using BGL's graph_t (adjacency table):
	//https://www.boost.org/doc/libs/1_60_0/libs/graph/doc/adjacency_list.html

	//set the source node's distance from source to zero
	distances[source] = 0;

	const auto weight = get(edge_weight, graph);

	//now to enqueue source's neighbors in the pqueue
	offer_pq.push(offer_t(source,0.0f));
	auto [start, finish] = out_edges(source,graph);
	for(;start != finish; start++)
	{
//		if (DEBUG) std::cout << "adding an offer from source to "<<target(*start,graph)<<" of distance "<<get(weight, *start)<<std::endl;
		offer_t offer(target(*start,graph),get(weight, *start));
		offer_pq.push(offer);
	}

	//ok now all the threads gotta do their things. 
	for (int thread_id = 0; thread_id < NUM_THREADS;)
	{
		threads.push_back(
			std::thread([&, thread_id](){
				parallel_dijkstra_thread(
					done,
					thread_id,
					offer_pq,
					distances,
					distance_mutexes,
					predecessor_mutexes,
					predecessors,
					graph);}));
		thread_id++;
	}

	//join threads!

	for(int thread = 0; thread < NUM_THREADS; thread++)
	{
//		if(DEBUG) std::cout <<"thread "<<thread<<" of "<<NUM_THREADS << " is joining"<<std::endl;
		threads[thread].join();
//		if(DEBUG) std::cout<<"thread " <<thread<<" joined!" <<std::endl;
	}
//	if (DEBUG) std::cout<<"joined threads!"<<std::endl;
//	debug_file_stream.close();
	return distances;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %r8d, %ebp
movq %rcx, 0x48(%rsp)
movq %rdx, %rbx
movq %rsi, %r15
andq $0x0, 0x40(%rsp)
leaq 0xb0(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq %rdi, %r12
andq $0x0, 0x60(%rsp)
leaq 0x80(%rsp), %rax
andl $0x0, (%rax)
xorps %xmm0, %xmm0
andq $0x0, 0x8(%rax)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x50(%rsp)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movl %r8d, 0x24(%rsp)
leaq 0xa8(%rsp), %rdx
leaq 0x78(%rsp), %rcx
callq 0xb613
movq %r12, %rdi
movq %rbx, %rsi
callq 0xbd3a
movq %r12, 0x8(%rsp)
andl $0x0, (%rax)
movq (%rbx), %rax
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
andl $0x0, 0x8(%rsi)
leaq 0x50(%rsp), %rdi
callq 0xbd8a
movq (%rbx), %r14
shlq $0x5, %r14
addq 0x18(%r15), %r14
leaq 0x50(%rsp), %r12
leaq 0x10(%rsp), %rbx
movq %r14, %r13
movq (%r13), %r13
cmpq %r14, %r13
je 0xbb1a
movq 0x10(%r13), %rax
movq 0x18(%r13), %rcx
movq %rax, 0x10(%rsp)
cvtsi2ssl (%rcx), %xmm0
movss %xmm0, 0x18(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0xbe5a
jmp 0xbaed
xorl %r14d, %r14d
testl %ebp, %ebp
cmovlel %r14d, %ebp
leaq 0x28(%rsp), %rbx
leaq 0x30(%rsp), %r13
cmpl %r14d, %ebp
je 0xbbc9
andq $0x0, 0x28(%rsp)
pushq $0x48
popq %rdi
callq 0x42a0
leaq 0xb21d(%rip), %rcx      # 0x16d68
movq %rcx, (%rax)
movl %r14d, 0x8(%rax)
leaq 0x24(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %r12, 0x18(%rax)
movq 0x8(%rsp), %rcx
movq %rcx, 0x20(%rax)
leaq 0xa8(%rsp), %rcx
movq %rcx, 0x28(%rax)
leaq 0x78(%rsp), %rcx
movq %rcx, 0x30(%rax)
movq 0x48(%rsp), %rcx
movq %rcx, 0x38(%rax)
movq %r15, 0x40(%rax)
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x4260
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xbbae
movq (%rdi), %rax
callq *0x8(%rax)
movq %r13, %rdi
movq %rbx, %rsi
callq 0xcc92
movq %rbx, %rdi
callq 0xbe70
incl %r14d
jmp 0xbb2d
movl %ebp, %ebx
shlq $0x3, %rbx
xorl %r14d, %r14d
movq 0x8(%rsp), %r12
cmpq %r14, %rbx
je 0xbbef
movq 0x30(%rsp), %rdi
addq %r14, %rdi
callq 0x4440
addq $0x8, %r14
jmp 0xbbd7
leaq 0x78(%rsp), %rdi
callq 0xc1a2
leaq 0x50(%rsp), %rdi
callq 0xc1b6
leaq 0xa8(%rsp), %rdi
callq 0xc1a2
leaq 0x30(%rsp), %rdi
callq 0xbe7e
movq %r12, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xbc63
jmp 0xbc38
movq %rax, %r14
jmp 0xbc73
movq %rax, %r14
jmp 0xbc6b
jmp 0xbc63
movq %rax, %r14
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xbc66
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0xbc66
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0xbe70
jmp 0xbc66
movq %rax, %r14
movq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x8ef0
leaq 0x78(%rsp), %rdi
callq 0xc1a2
leaq 0x50(%rsp), %rdi
callq 0xc1b6
leaq 0xa8(%rsp), %rdi
callq 0xc1a2
leaq 0x30(%rsp), %rdi
callq 0xbe7e
movq %r14, %rdi
callq 0x4470