// File: /workspace/github_cpp_makefile_star2_O2/dragazo[P]cpp-gc/GarbageCollection.cpp
// Function: GC::disjoint_module_container::BACKGROUND_COLLECTOR_ONLY___collect(bool)
void GC::disjoint_module_container::BACKGROUND_COLLECTOR_ONLY___collect(bool collect)
{
	{
		std::lock_guard<std::mutex> internal_lock(internal_mutex);

		// we should not already be collecting (this is background collector only)
		assert(!collecting);

		// enter collecting mode
		collecting = true;

		// the add cache should be empty (just came out of a non-collecting phase)
		assert(disjunction_add_cache.empty());
	}

	// if performing a real collection
	if (collect)
	{
		// for each stored disjunction:
		// (the EXPLICIT std::list::iterator ensures it is indeed a LIST).
		// (otherwise we need to constantly update the end iterator after each erasure).
		for (auto i = disjunctions.begin(), end = disjunctions.end(); i != end; )
		{
			// lock and use this disjunction as the local disjunction - then get the raw handle
			disjoint_module *const raw_handle = (disjoint_module::local_handle() = *i).get();

			// if it's still allive, perform a collection on it
			if (raw_handle)
			{
				// perform the collection
				raw_handle->collect();
				++i;

				// afterwards unlink the handle - we don't want to keep them alive longer than they need to be
				disjoint_module::local_handle() = nullptr;
			}
			// otherwise it's invalid (dangling) - erase it
			else i = disjunctions.erase(i);
		}
	}
	// otherwise just performing a cull
	else
	{
		// for each stored disjunction:
		// (the EXPLICIT std::list::iterator ensures it is indeed a LIST).
		// (otherwise we need to constantly update the end iterator after each erasure).
		for (auto i = disjunctions.begin(), end = disjunctions.end(); i != end; )
		{
			// if this is a dangling pointer, erase it
			if (i->expired()) i = disjunctions.erase(i);
			else ++i;
		}
	}

	{
		std::lock_guard<std::mutex> internal_lock(internal_mutex);

		// exit collecting mode
		collecting = false;

		// apply all the cached disjunction insertions
		disjunctions.splice(disjunctions.begin(), disjunction_add_cache);
		disjunction_add_cache.clear(); // just to be sure
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %rbx
callq 0x7694
cmpb $0x1, 0x28(%rbx)
je 0x6ab3
movb $0x1, 0x28(%rbx)
leaq 0x48(%rbx), %r14
cmpq %r14, 0x48(%rbx)
jne 0x6ad2
movq %rbx, %rdi
callq 0x5220
leaq 0x30(%rbx), %r15
movq 0x30(%rbx), %r12
testb %bpl, %bpl
je 0x6a48
cmpq %r15, %r12
je 0x6a7a
leaq 0x10(%r12), %r13
callq 0x67be
movq %rax, %rbp
movq %rax, %rdi
movq %r13, %rsi
callq 0x6af2
movq (%rbp), %rdi
testq %rdi, %rdi
je 0x6a38
callq 0x58fc
movq (%r12), %r12
callq 0x67be
movq %rax, %rdi
xorl %esi, %esi
callq 0x6914
jmp 0x69f8
movq %r15, %rdi
movq %r12, %rsi
callq 0x7300
movq %rax, %r12
jmp 0x69f8
cmpq %r15, %r12
je 0x6a7a
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x6a6a
movq 0x1e8(%rax), %rax
shlq $0x8, %rax
je 0x6a6a
movq (%r12), %r12
jmp 0x6a48
movq %r15, %rdi
movq %r12, %rsi
callq 0x7300
movq %rax, %r12
jmp 0x6a48
movq %rbx, %rdi
callq 0x7694
movb $0x0, 0x28(%rbx)
movq 0x30(%rbx), %rsi
movq %r15, %rdi
movq %r14, %rdx
callq 0x85e6
movq %r14, %rdi
callq 0x730e
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5220
leaq 0x179f2(%rip), %rdi     # 0x1e4ac
leaq 0x17695(%rip), %rsi     # 0x1e156
leaq 0x179f0(%rip), %rcx     # 0x1e4b8
movl $0x350, %edx            # imm = 0x350
callq 0x51b0
leaq 0x17a2d(%rip), %rdi     # 0x1e506
leaq 0x17676(%rip), %rsi     # 0x1e156
leaq 0x179d1(%rip), %rcx     # 0x1e4b8
movl $0x356, %edx            # imm = 0x356
callq 0x51b0
nop