// File: /workspace/github_cpp_makefile_star2_O3/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 0x4380
testl %eax, %eax
jne 0x7948
cmpb $0x1, 0x28(%rbx)
je 0x794f
movb $0x1, 0x28(%rbx)
leaq 0x48(%rbx), %r14
cmpq %r14, 0x48(%rbx)
jne 0x796e
movq %rbx, %rdi
callq 0x4200
leaq 0x30(%rbx), %r15
movq 0x30(%rbx), %r12
testb %bpl, %bpl
je 0x78af
cmpq %r15, %r12
je 0x78e5
callq 0x75ba
movq %rax, %r13
movq 0x10(%r12), %rsi
movq %rax, %rdi
callq 0x7b68
movq (%r13), %rdi
testq %rdi, %rdi
je 0x789b
callq 0x6556
movq (%r12), %r12
callq 0x75ba
movq %rax, %rdi
xorl %esi, %esi
callq 0x774e
jmp 0x785a
movq (%r12), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x900a
movq %r13, %r12
jmp 0x785a
cmpq %r15, %r12
je 0x78e5
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x78d1
movq 0x1e8(%rax), %rax
shlq $0x8, %rax
je 0x78d1
movq (%r12), %r12
jmp 0x78af
movq (%r12), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x900a
movq %r13, %r12
jmp 0x78af
movq %rbx, %rdi
callq 0x4380
testl %eax, %eax
jne 0x7948
movb $0x0, 0x28(%rbx)
movq 0x48(%rbx), %rsi
cmpq %r14, %rsi
je 0x791a
movq 0x30(%rbx), %rdi
movq %r14, %rdx
callq 0x4120
movq 0x58(%rbx), %rax
addq %rax, 0x40(%rbx)
movq $0x0, 0x58(%rbx)
movq %r14, %rdi
callq 0x834e
movq %r14, 0x50(%rbx)
movq %r14, 0x48(%rbx)
movq $0x0, 0x58(%rbx)
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4200
movl %eax, %edi
callq 0x41a0
leaq 0x17b56(%rip), %rdi     # 0x1f4ac
leaq 0x177f9(%rip), %rsi     # 0x1f156
leaq 0x17b54(%rip), %rcx     # 0x1f4b8
movl $0x350, %edx            # imm = 0x350
callq 0x4190
leaq 0x17b91(%rip), %rdi     # 0x1f506
leaq 0x177da(%rip), %rsi     # 0x1f156
leaq 0x17b35(%rip), %rcx     # 0x1f4b8
movl $0x356, %edx            # imm = 0x356
callq 0x4190
nop