| 614 | } |
| 615 | |
| 616 | HJobContext JobSystemCreate(const JobSystemCreateParams* create_params) |
| 617 | { |
| 618 | if (create_params == 0) |
| 619 | return 0; |
| 620 | |
| 621 | JobContext* context = new JobContext(); |
| 622 | context->m_Initialized = 1; |
| 623 | |
| 624 | context->m_ThreadContext.m_Context = context; |
| 625 | context->m_ThreadContext.m_Generation = 1; |
| 626 | |
| 627 | #if defined(DM_HAS_THREADS) |
| 628 | uint32_t thread_count = create_params->m_ThreadCount; |
| 629 | context->m_UseThreads = thread_count > 0; |
| 630 | if (context->m_UseThreads) |
| 631 | { |
| 632 | context->m_ThreadContext.m_Mutex = dmMutex::New(); |
| 633 | context->m_ThreadContext.m_WakeupCond = dmConditionVariable::New(); |
| 634 | context->m_ThreadContext.m_Run = true; |
| 635 | |
| 636 | context->m_Threads.SetCapacity(thread_count); |
| 637 | context->m_Threads.SetSize(thread_count); |
| 638 | |
| 639 | for (int i = 0; i < thread_count; ++i) |
| 640 | { |
| 641 | char name_buf[32]; |
| 642 | const char* thread_name_prefix = create_params->m_ThreadNamePrefix ? create_params->m_ThreadNamePrefix : "defoldjob"; |
| 643 | // According to doc for pthread_set_name: https://man7.org/linux/man-pages/man3/pthread_setname_np.3.html |
| 644 | assert(strlen(thread_name_prefix) < 16-3); // account for "_00" |
| 645 | |
| 646 | dmSnPrintf(name_buf, sizeof(name_buf), "%s_%d", thread_name_prefix, i); |
| 647 | context->m_Threads[i] = dmThread::New(JobThread, 0x80000, (void*)&context->m_ThreadContext, name_buf); |
| 648 | } |
| 649 | } |
| 650 | else |
| 651 | { |
| 652 | context->m_ThreadContext.m_Mutex = 0; |
| 653 | context->m_ThreadContext.m_WakeupCond = 0; |
| 654 | context->m_ThreadContext.m_Run = false; |
| 655 | } |
| 656 | #else |
| 657 | context->m_UseThreads = false; |
| 658 | #endif |
| 659 | return context; |
| 660 | } |
| 661 | |
| 662 | void JobSystemDestroy(HJobContext context) |
| 663 | { |