// File: /workspace/github_cpp_makefile_star2_O3/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, 0x70(%rsp)
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
xorl %eax, %eax
movq %rax, 0x60(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
leaq 0xb0(%rsp), %rcx
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
movq %rax, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
leaq 0x80(%rsp), %rcx
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
movl %r8d, 0x2c(%rsp)
leaq 0xa8(%rsp), %rdx
leaq 0x78(%rsp), %rcx
callq 0xc138
movq %r14, %rdi
movq %rbx, %rsi
movq %r14, 0x8(%rsp)
callq 0xc916
xorl %ecx, %ecx
movl %ecx, (%rax)
movq (%rbx), %rax
leaq 0x18(%rsp), %rsi
movq %rax, (%rsi)
movl %ecx, 0x8(%rsi)
leaq 0x30(%rsp), %rdi
callq 0xc97c
movq (%rbx), %rax
movq 0x18(%r15), %rcx
shlq $0x5, %rax
leaq (%rcx,%rax), %r14
movq (%rcx,%rax), %r13
cmpq %r14, %r13
je 0xc6b7
leaq 0x30(%rsp), %rbx
leaq 0x18(%rsp), %r12
movq 0x10(%r13), %rax
movq 0x18(%r13), %rcx
movq %rax, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtsi2ssl (%rcx), %xmm0
movss %xmm0, 0x20(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xcb0c
movq (%r13), %r13
cmpq %r14, %r13
jne 0xc689
testl %ebp, %ebp
movq 0x8(%rsp), %r12
jle 0xc7a1
xorl %r14d, %r14d
leaq 0x10(%rsp), %r13
leaq 0x18(%rsp), %rbx
leaq 0x50(%rsp), %r12
movq $0x0, 0x10(%rsp)
movl $0x48, %edi
callq 0x32a0
leaq 0x9680(%rip), %rcx      # 0x15d70
movq %rcx, (%rax)
movl %r14d, 0x8(%rax)
leaq 0x2c(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x30(%rsp), %rcx
movq %rcx, 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 0x70(%rsp), %rcx
movq %rcx, 0x38(%rax)
movq %r15, 0x40(%rax)
movq %rax, 0x18(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x3260
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0xc756
movq (%rdi), %rax
callq *0x8(%rax)
movq %r12, %rdi
movq %r13, %rsi
callq 0xd75a
cmpq $0x0, 0x10(%rsp)
jne 0xc818
incl %r14d
cmpl %r14d, %ebp
jne 0xc6d6
testl %ebp, %ebp
movq 0x8(%rsp), %r12
jle 0xc7a1
movl %ebp, %ebx
shlq $0x3, %rbx
xorl %r14d, %r14d
movq 0x50(%rsp), %rdi
addq %r14, %rdi
callq 0x3460
addq $0x8, %r14
cmpq %r14, %rbx
jne 0xc78b
leaq 0x78(%rsp), %rdi
callq 0xce78
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xc7c2
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x32b0
leaq 0xa8(%rsp), %rdi
callq 0xce78
leaq 0x50(%rsp), %rdi
callq 0xcba6
movq %r12, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xc81d
jmp 0xc81d
movq %rax, %r14
jmp 0xc82a
jmp 0xc81d
jmp 0xc81d
movq %rax, %r14
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0xc820
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0xc820
cmpq $0x0, 0x10(%rsp)
je 0xc81d
callq 0x30b0
movq %rax, %r14
movq 0x8(%rsp), %rdi
callq 0x9fea
leaq 0x78(%rsp), %rdi
callq 0xce78
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xc84b
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x32b0
leaq 0xa8(%rsp), %rdi
callq 0xce78
leaq 0x50(%rsp), %rdi
callq 0xcba6
movq %r14, %rdi
callq 0x34a0