// File: /workspace/github_cpp_makefile_star2_O1/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 0x5380
testl %eax, %eax
jne 0x89fe
cmpb $0x1, 0x28(%rbx)
je 0x8a05
movb $0x1, 0x28(%rbx)
leaq 0x48(%rbx), %r14
cmpq %r14, 0x48(%rbx)
jne 0x8a24
movq %rbx, %rdi
callq 0x5200
leaq 0x30(%rbx), %r15
movq 0x30(%rbx), %r12
testb %bpl, %bpl
je 0x8965
cmpq %r15, %r12
je 0x899b
callq 0x865a
movq %rax, %r13
movq 0x10(%r12), %rsi
movq %rax, %rdi
callq 0x8c1e
movq (%r13), %rdi
testq %rdi, %rdi
je 0x8951
callq 0x757a
movq (%r12), %r12
callq 0x865a
movq %rax, %rdi
xorl %esi, %esi
callq 0x87fe
jmp 0x8910
movq (%r12), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0xa090
movq %r13, %r12
jmp 0x8910
cmpq %r15, %r12
je 0x899b
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x8987
movq 0x1e8(%rax), %rax
shlq $0x8, %rax
je 0x8987
movq (%r12), %r12
jmp 0x8965
movq (%r12), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0xa090
movq %r13, %r12
jmp 0x8965
movq %rbx, %rdi
callq 0x5380
testl %eax, %eax
jne 0x89fe
movb $0x0, 0x28(%rbx)
movq 0x48(%rbx), %rsi
cmpq %r14, %rsi
je 0x89d0
movq 0x30(%rbx), %rdi
movq %r14, %rdx
callq 0x5120
movq 0x58(%rbx), %rax
addq %rax, 0x40(%rbx)
movq $0x0, 0x58(%rbx)
movq %r14, %rdi
callq 0x9414
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 0x5200
movl %eax, %edi
callq 0x51a0
leaq 0x17aa0(%rip), %rdi     # 0x204ac
leaq 0x17743(%rip), %rsi     # 0x20156
leaq 0x17a9e(%rip), %rcx     # 0x204b8
movl $0x350, %edx            # imm = 0x350
callq 0x5190
leaq 0x17adb(%rip), %rdi     # 0x20506
leaq 0x17724(%rip), %rsi     # 0x20156
leaq 0x17a7f(%rip), %rcx     # 0x204b8
movl $0x356, %edx            # imm = 0x356
callq 0x5190
nop