// File: /workspace/github_c_makefile_star2_O1/deplinenoise[P]tundra/src/BuildQueue.cpp
// Function: t2::BuildQueueInit(t2::BuildQueue*, t2::BuildQueueConfig const*)
void BuildQueueInit(BuildQueue* queue, const BuildQueueConfig* config)
  {
    ProfilerScope prof_scope("Tundra BuildQueueInit", 0);
    CHECK(config->m_MaxExpensiveCount > 0 && config->m_MaxExpensiveCount <= config->m_ThreadCount);

    MutexInit(&queue->m_Lock);
    CondInit(&queue->m_WorkAvailable);

    // Compute queue capacity. Allocate space for a power of two number of
    // indices that's at least one larger than the max number of nodes. Because
    // the queue is treated as a ring buffer, we want W=R to mean an empty
    // buffer.
    uint32_t capacity = NextPowerOfTwo(config->m_MaxNodes + 1);

    MemAllocHeap* heap = config->m_Heap;

    queue->m_Queue              = HeapAllocateArray<int32_t>(heap, capacity);
    queue->m_QueueReadIndex     = 0;
    queue->m_QueueWriteIndex    = 0;
    queue->m_QueueCapacity      = capacity;
    queue->m_Config             = *config;
    queue->m_PendingNodeCount   = 0;
    queue->m_FailedNodeCount    = 0;
    queue->m_QuitSignalled      = false;
    queue->m_ExpensiveRunning   = 0;
    queue->m_ExpensiveWaitCount = 0;
    queue->m_ExpensiveWaitList  = HeapAllocateArray<NodeState*>(heap, capacity);

    queue->m_Threads = HeapAllocateArrayZeroed<ThreadId>(config->m_Heap, config->m_ThreadCount);
    queue->m_ThreadState = HeapAllocateArrayZeroed<ThreadState>(config->m_Heap, config->m_ThreadCount);

    CHECK(queue->m_Queue);

    Log(kDebug, "build queue initialized; ring buffer capacity = %u", queue->m_QueueCapacity);

    // Block all signals on the main thread.
    SignalBlockThread(true);
    SignalHandlerSetCondition(&queue->m_WorkAvailable);

    // Create build threads.
    for (int i = 0, thread_count = config->m_ThreadCount; i < thread_count; ++i)
    {
      ThreadState* thread_state = &queue->m_ThreadState[i];

      ThreadStateInit(thread_state, queue, MB(32), i);

      if (i > 0)
      {
        Log(kDebug, "starting build thread %d", i);
        queue->m_Threads[i] = ThreadStart(BuildThreadRoutine, thread_state);
      }
    }
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
movl $0x0, 0x4(%rsp)
leaq 0x192ac(%rip), %rax     # 0x1d680
cmpb $0x1, (%rax)
jne 0x43e7
leaq 0x11599(%rip), %rdi     # 0x15979
xorl %esi, %esi
callq 0xfb72
movq %rbx, %rdi
xorl %esi, %esi
callq 0x34d0
testl %eax, %eax
jne 0x45b4
leaq 0x28(%rbx), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x33e0
testl %eax, %eax
jne 0x45bd
movl 0x28(%r14), %edi
incl %edi
callq 0x607e
movl %eax, %ebp
movq 0x8(%r14), %r12
movl %eax, %r13d
leaq (,%r13,4), %rsi
movq %r12, %rdi
callq 0xef62
movq %rax, 0x58(%rbx)
xorl %eax, %eax
movq %rax, 0x64(%rbx)
movl %ebp, 0x60(%rbx)
leaq 0x70(%rbx), %rdi
movl $0xf, %ecx
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, 0xe8(%rbx)
movb $0x0, 0x118(%rbx)
movq %rax, 0x108(%rbx)
shlq $0x3, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0xef62
movq %rax, 0x110(%rbx)
movq 0x8(%r14), %rdi
movslq 0x10(%r14), %r12
movq %r12, %rsi
shlq $0x6, %rsi
callq 0xef62
movq %rax, %r13
shlq $0x3, %r12
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x31b0
movq %r13, 0xf8(%rbx)
movq 0x8(%r14), %rdi
movslq 0x10(%r14), %r13
imulq $0x1900, %r13, %rsi     # imm = 0x1900
callq 0xef62
movq %rax, %r12
shlq $0x4, %r13
leaq (,%r13,4), %rdx
addq %r13, %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x31b0
movq %r12, 0x100(%rbx)
movl 0x60(%rbx), %edx
leaq 0x114a5(%rip), %rsi     # 0x1598f
movl $0x8, %edi
xorl %eax, %eax
callq 0x5ef0
movl $0x1, %edi
callq 0x11c6d
movq %r15, %rdi
callq 0x11c29
movslq 0x10(%r14), %r15
testq %r15, %r15
jle 0x459b
movl $0x48, %r12d
xorl %r13d, %r13d
movq 0x100(%rbx), %r14
leaq (%r14,%r12), %rbp
addq $-0x48, %rbp
movq %rbp, %rdi
callq 0xef60
leaq (%r14,%r12), %rdi
addq $-0x40, %rdi
movl $0x2000000, %edx        # imm = 0x2000000
movq %rbp, %rsi
leaq 0x11513(%rip), %rcx     # 0x15a5f
callq 0xefb4
movl %r13d, -0x8(%r14,%r12)
movq %rbx, (%r14,%r12)
testq %r13, %r13
je 0x458f
movl $0x8, %edi
leaq 0x11457(%rip), %rsi     # 0x159c2
movl %r13d, %edx
xorl %eax, %eax
callq 0x5ef0
leaq 0x6a(%rip), %rdi        # 0x45e6
movq %rbp, %rsi
callq 0x126e1
movq 0xf8(%rbx), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
addq $0x50, %r12
cmpq %r13, %r15
jne 0x451e
leaq 0x4(%rsp), %rdi
callq 0x5b82
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1146d(%rip), %rdi     # 0x15a28
jmp 0x45c4
leaq 0x11480(%rip), %rdi     # 0x15a44
xorl %eax, %eax
callq 0x5c85
jmp 0x45d1
jmp 0x45d1
jmp 0x45d1
movq %rax, %rbx
leaq 0x4(%rsp), %rdi
callq 0x5b82
movq %rbx, %rdi
callq 0x35e0