// File: 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
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
movq %rdi, -0x1e8(%rbp)
movq %rdi, %rax
movq %rax, -0x1e0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x1dba0
leaq -0x70(%rbp), %rdi
callq 0x1dbc0
leaq -0x90(%rbp), %rdi
callq 0x1dbe0
jmp 0x1cc30
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x1dbc0
movq -0x1e8(%rbp), %rdi
movq -0x1f0(%rbp), %rcx
movl -0x24(%rbp), %eax
movl %eax, -0xd4(%rbp)
movb $0x0, -0xd5(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x1c500
jmp 0x1cc70
movq -0x1e8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1d5b0
movq %rax, -0x1f8(%rbp)
jmp 0x1cc89
movq -0x1f8(%rbp), %rax
movl $0x0, (%rax)
movq -0x10(%rbp), %rsi
xorl %edi, %edi
callq 0x16f30
movl %eax, -0x1fc(%rbp)
jmp 0x1cca9
movl -0x1fc(%rbp), %eax
movl %eax, -0xdc(%rbp)
movq -0x18(%rbp), %rsi
movl $0x0, -0xf4(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf4(%rbp), %rdx
callq 0x1dc10
jmp 0x1ccd8
leaq -0x90(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x1d6c0
jmp 0x1cced
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rdx
leaq -0x118(%rbp), %rdi
callq 0x13d50
jmp 0x1cd06
leaq -0x118(%rbp), %rdi
callq 0x1da00
movq %rax, -0x120(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x1da20
movq %rax, -0x128(%rbp)
movq -0x120(%rbp), %rdi
movq -0x128(%rbp), %rsi
callq 0x13e70
movb %al, -0x1fd(%rbp)
jmp 0x1cd47
movb -0x1fd(%rbp), %al
testb $0x1, %al
jne 0x1cd56
jmp 0x1ce7a
movq -0x120(%rbp), %rsi
leaq -0x158(%rbp), %rdi
callq 0x13ed0
jmp 0x1cd6b
movq -0x10(%rbp), %rsi
leaq -0x158(%rbp), %rdi
callq 0x13eb0
movq %rax, -0x208(%rbp)
jmp 0x1cd84
movq -0x208(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x120(%rbp), %rsi
leaq -0x170(%rbp), %rdi
callq 0x13ed0
jmp 0x1cda7
leaq -0xdc(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x15910
movq %rax, -0x210(%rbp)
jmp 0x1cdc3
movq -0x210(%rbp), %rdx
leaq -0x138(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x1dc40
jmp 0x1cddf
leaq -0x90(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x1dc70
jmp 0x1cdf4
jmp 0x1cdf6
movq -0x120(%rbp), %rdi
xorl %esi, %esi
callq 0x1da90
movq %rdx, -0x220(%rbp)
movq %rax, -0x218(%rbp)
jmp 0x1ce14
movq -0x220(%rbp), %rax
movq -0x218(%rbp), %rcx
movq %rcx, -0x180(%rbp)
movq %rax, -0x178(%rbp)
jmp 0x1cd2c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x1d01e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x1d006
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x1cffa
movl $0x0, -0x184(%rbp)
movl -0x184(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1cf62
movq -0x1e8(%rbp), %rax
movl -0x184(%rbp), %ecx
movl %ecx, -0x1d0(%rbp)
leaq -0xd4(%rbp), %rcx
movq %rcx, -0x1c8(%rbp)
leaq -0x90(%rbp), %rcx
movq %rcx, -0x1c0(%rbp)
movq %rax, -0x1b8(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0x1b0(%rbp)
leaq -0xd0(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x198(%rbp)
leaq -0x190(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0x1d040
jmp 0x1cf0d
leaq -0x40(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x1dcc0
jmp 0x1cf1f
leaq -0x190(%rbp), %rdi
callq 0x1dcf0
movl -0x184(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x184(%rbp)
jmp 0x1ce84
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x1dcf0
jmp 0x1cffa
movl $0x0, -0x1d4(%rbp)
movl -0x1d4(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1cfa4
movslq -0x1d4(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x1dd20
movq %rax, %rdi
callq 0x4550
jmp 0x1cf91
jmp 0x1cf93
movl -0x1d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d4(%rbp)
jmp 0x1cf6c
movb $0x1, -0xd5(%rbp)
testb $0x1, -0xd5(%rbp)
jne 0x1cfc0
movq -0x1e8(%rbp), %rdi
callq 0xcd10
leaq -0xd0(%rbp), %rdi
callq 0x1da70
leaq -0x90(%rbp), %rdi
callq 0x1dd40
leaq -0x70(%rbp), %rdi
callq 0x1da70
leaq -0x40(%rbp), %rdi
callq 0x1dd60
movq -0x1e0(%rbp), %rax
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
movq -0x1e8(%rbp), %rdi
callq 0xcd10
leaq -0xd0(%rbp), %rdi
callq 0x1da70
leaq -0x90(%rbp), %rdi
callq 0x1dd40
leaq -0x70(%rbp), %rdi
callq 0x1da70
leaq -0x40(%rbp), %rdi
callq 0x1dd60
movq -0x98(%rbp), %rdi
callq 0x4580
nopl (%rax)