// File: /workspace/github_c_makefile_star2_O3/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 0x1935c(%rip), %rax     # 0x1d680
cmpb $0x1, (%rax)
jne 0x4337
leaq 0x11631(%rip), %rdi     # 0x15961
xorl %esi, %esi
callq 0xf4a3
movq %rbx, %rdi
xorl %esi, %esi
callq 0x34d0
testl %eax, %eax
jne 0x4504
leaq 0x28(%rbx), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x33e0
testl %eax, %eax
jne 0x450d
movl 0x28(%r14), %edi
incl %edi
callq 0x5f02
movl %eax, %ebp
movq 0x8(%r14), %r12
movl %eax, %r13d
leaq (,%r13,4), %rsi
movq %r12, %rdi
callq 0xe882
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 0xe882
movq %rax, 0x110(%rbx)
movq 0x8(%r14), %rdi
movslq 0x10(%r14), %r12
movq %r12, %rsi
shlq $0x6, %rsi
callq 0xe882
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 0xe882
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 0x1153d(%rip), %rsi     # 0x15977
movl $0x8, %edi
xorl %eax, %eax
callq 0x5d74
movl $0x1, %edi
callq 0x115a1
movq %r15, %rdi
callq 0x1155d
movslq 0x10(%r14), %r15
testq %r15, %r15
jle 0x44eb
movl $0x48, %r12d
xorl %r13d, %r13d
movq 0x100(%rbx), %r14
leaq (%r14,%r12), %rbp
addq $-0x48, %rbp
movq %rbp, %rdi
callq 0xe880
leaq (%r14,%r12), %rdi
addq $-0x40, %rdi
movl $0x2000000, %edx        # imm = 0x2000000
movq %rbp, %rsi
leaq 0x115ab(%rip), %rcx     # 0x15a47
callq 0xe8d4
movl %r13d, -0x8(%r14,%r12)
movq %rbx, (%r14,%r12)
testq %r13, %r13
je 0x44df
movl $0x8, %edi
leaq 0x114ef(%rip), %rsi     # 0x159aa
movl %r13d, %edx
xorl %eax, %eax
callq 0x5d74
leaq 0x6a(%rip), %rdi        # 0x4536
movq %rbp, %rsi
callq 0x11fa5
movq 0xf8(%rbx), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
addq $0x50, %r12
cmpq %r13, %r15
jne 0x446e
leaq 0x4(%rsp), %rdi
callq 0x5a06
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11505(%rip), %rdi     # 0x15a10
jmp 0x4514
leaq 0x11518(%rip), %rdi     # 0x15a2c
xorl %eax, %eax
callq 0x5b09
jmp 0x4521
jmp 0x4521
jmp 0x4521
movq %rax, %rbx
leaq 0x4(%rsp), %rdi
callq 0x5a06
movq %rbx, %rdi
callq 0x35e0