// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, 0x4(%rsp)
leaq 0xf8a4(%rip), %rdi      # 0x13979
xorl %esi, %esi
callq 0x551c
movq %rbx, %rdi
callq 0x5404
leaq 0x28(%rbx), %r15
movq %r15, %rdi
callq 0x5420
movl 0x28(%r14), %edi
incl %edi
callq 0x5b0d
movl %eax, %ebp
movq 0x8(%r14), %r12
movl %eax, %r13d
movq %r12, %rdi
movq %r13, %rsi
callq 0x543c
movq %rax, 0x58(%rbx)
andq $0x0, 0x64(%rbx)
movl %ebp, 0x60(%rbx)
leaq 0x70(%rbx), %rdi
pushq $0xf
popq %rcx
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
andq $0x0, 0xe8(%rbx)
movb $0x0, 0x118(%rbx)
andq $0x0, 0x108(%rbx)
movq %r12, %rdi
movq %r13, %rsi
callq 0x5445
movq %rax, 0x110(%rbx)
movq 0x8(%r14), %rdi
movslq 0x10(%r14), %rsi
callq 0x544e
movq %rax, 0xf8(%rbx)
movq 0x8(%r14), %rdi
movslq 0x10(%r14), %rsi
callq 0x546d
movq %rax, 0x100(%rbx)
movl 0x60(%rbx), %edx
leaq 0xf80c(%rip), %rsi      # 0x1398f
pushq $0x8
popq %rdi
xorl %eax, %eax
callq 0x5981
pushq $0x1
popq %rdi
callq 0x100e1
movq %r15, %rdi
callq 0x100b5
movl 0x10(%r14), %ebp
xorl %eax, %eax
testl %ebp, %ebp
cmovlel %eax, %ebp
xorl %r12d, %r12d
xorl %r13d, %r13d
cmpq %r13, %rbp
je 0x4228
movq 0x100(%rbx), %r14
leaq (%r14,%r12), %r15
movq %r15, %rdi
callq 0xd798
leaq (%r14,%r12), %rdi
addq $0x8, %rdi
movl $0x2000000, %edx        # imm = 0x2000000
movq %r15, %rsi
leaq 0xf882(%rip), %rcx      # 0x13a5f
callq 0xd7ec
movl %r13d, 0x40(%r14,%r12)
movq %rbx, 0x48(%r14,%r12)
testq %r13, %r13
je 0x421f
pushq $0x8
popq %rdi
leaq 0xf7c7(%rip), %rsi      # 0x139c2
movl %r13d, %edx
xorl %eax, %eax
callq 0x5981
leaq 0x50(%rip), %rdi        # 0x425c
movq %r15, %rsi
callq 0x10919
movq 0xf8(%rbx), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
addq $0x50, %r12
jmp 0x41ae
leaq 0x4(%rsp), %rdi
callq 0x5488
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4247
jmp 0x4247
jmp 0x4247
movq %rax, %rbx
leaq 0x4(%rsp), %rdi
callq 0x5488
movq %rbx, %rdi
callq 0x35e0