diff --git a/doc/samples/meshviewer/meshviewer.cpp b/doc/samples/meshviewer/meshviewer.cpp index 91fb211f..8f963320 100644 --- a/doc/samples/meshviewer/meshviewer.cpp +++ b/doc/samples/meshviewer/meshviewer.cpp @@ -226,7 +226,7 @@ void MeshViewer::TickGame(float seconds) { MeshViewerLoadJob* job = GetLoadJob(m_file_name); if (!job) - m_file_loader->AddWork(job); + m_file_loader->AddJob(job); } //Check work done diff --git a/src/lol/sys/thread.h b/src/lol/sys/thread.h index 96c1cb4c..c45e21e8 100644 --- a/src/lol/sys/thread.h +++ b/src/lol/sys/thread.h @@ -147,7 +147,9 @@ protected: array m_threads; array m_job_dispatch; private: +#if LOL_FEATURE_THREADS queue m_spawnqueue, m_donequeue; +#endif //LOL_FEATURE_THREADS queue m_jobqueue; queue m_resultqueue; }; diff --git a/src/lol/sys/threadtypes.h b/src/lol/sys/threadtypes.h index 2b7f2357..1d308c08 100644 --- a/src/lol/sys/threadtypes.h +++ b/src/lol/sys/threadtypes.h @@ -34,7 +34,7 @@ public: { } //Work stuff - bool AddWork(ThreadJob* job); + bool AddJob(ThreadJob* job); bool GetWorkResult(array& results); protected: diff --git a/src/sys/thread.cpp b/src/sys/thread.cpp index e8eb3de0..2633f5f3 100644 --- a/src/sys/thread.cpp +++ b/src/sys/thread.cpp @@ -42,7 +42,7 @@ bool BaseThreadManager::Start() //Add minimum threads m_threads.reserve(m_thread_count); - AddThreads(m_thread_min); + AddThreads(m_thread_count /*FIX THAT (TOUKY) m_thread_min*/); return true; } @@ -119,15 +119,19 @@ void BaseThreadManager::BaseThreadWork() { //Try to retrieve a job ThreadJob* job = m_jobqueue.pop(); +#if LOL_FEATURE_THREADS //Stop thread if (job->GetJobType() == ThreadJobType::THREAD_STOP) { -#if LOL_FEATURE_THREADS break; -#endif //LOL_FEATURE_THREADS } //Or work - else if (*job == ThreadJobType::WORK_TODO) + else +#else //LOL_FEATURE_THREADS + if (!job) + return; +#endif //LOL_FEATURE_THREADS + if (*job == ThreadJobType::WORK_TODO) { if (job->DoWork()) job->SetJobType(ThreadJobType::WORK_SUCCESSED); @@ -170,11 +174,14 @@ void BaseThreadManager::TickGame(float seconds) } } + //TODO: TOUKY: FIX THAT + /* //Resize thread count if needed if (m_threads.count() > m_jobqueue.count() && m_threads.count() > m_thread_min) StopThreads((int)(m_threads.Count() - m_thread_min)); else if (m_threads.count() < m_jobqueue.count()) AddThreads((int)(lol::min(m_jobqueue.count(), (ptrdiff_t)m_thread_count) - m_threads.count())); + */ } } /* namespace lol */ diff --git a/src/sys/threadbase.h b/src/sys/threadbase.h index 90c77501..d3accf34 100644 --- a/src/sys/threadbase.h +++ b/src/sys/threadbase.h @@ -40,46 +40,56 @@ class mutex_base public: mutex_base() { +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H pthread_mutex_init(&m_mutex, nullptr); #elif defined _WIN32 InitializeCriticalSection(&m_mutex); #endif +#endif //LOL_FEATURE_THREADS } ~mutex_base() { +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H pthread_mutex_destroy(&m_mutex); #elif defined _WIN32 DeleteCriticalSection(&m_mutex); #endif +#endif //LOL_FEATURE_THREADS } void lock() { +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H pthread_mutex_lock(&m_mutex); #elif defined _WIN32 EnterCriticalSection(&m_mutex); #endif +#endif //LOL_FEATURE_THREADS } void unlock() { +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H pthread_mutex_unlock(&m_mutex); #elif defined _WIN32 LeaveCriticalSection(&m_mutex); #endif +#endif //LOL_FEATURE_THREADS } private: +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H pthread_mutex_t m_mutex; #elif defined _WIN32 CRITICAL_SECTION m_mutex; #endif +#endif //LOL_FEATURE_THREADS }; template @@ -89,6 +99,7 @@ public: queue_base() { m_start = m_count = 0; +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H m_poppers = m_pushers = 0; pthread_mutex_init(&m_mutex, nullptr); @@ -99,10 +110,12 @@ public: m_full_sem = CreateSemaphore(nullptr, 0, CAPACITY, nullptr); InitializeCriticalSection(&m_mutex); #endif +#endif //LOL_FEATURE_THREADS } ~queue_base() { +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H pthread_cond_destroy(&m_empty_cond); pthread_cond_destroy(&m_full_cond); @@ -112,39 +125,12 @@ public: CloseHandle(m_full_sem); DeleteCriticalSection(&m_mutex); #endif - } - - ptrdiff_t count() - { - ptrdiff_t current_count = 0; -#if defined HAVE_PTHREAD_H - pthread_mutex_lock(&m_mutex); - /* If queue is full, wait on the "full" cond var. */ - m_pushers++; - while (m_count == CAPACITY) - pthread_cond_wait(&m_full_cond, &m_mutex); - m_pushers--; -#elif defined _WIN32 - WaitForSingleObject(m_empty_sem, INFINITE); - EnterCriticalSection(&m_mutex); -#endif - - current_count = (ptrdiff_t)m_count; - -#if defined HAVE_PTHREAD_H - /* If there were poppers waiting, signal the "empty" cond var. */ - if (m_poppers) - pthread_cond_signal(&m_empty_cond); - pthread_mutex_unlock(&m_mutex); -#elif defined _WIN32 - LeaveCriticalSection(&m_mutex); - ReleaseSemaphore(m_full_sem, 1, nullptr); -#endif - return current_count; +#endif //LOL_FEATURE_THREADS } void push(T value) { +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H pthread_mutex_lock(&m_mutex); /* If queue is full, wait on the "full" cond var. */ @@ -156,11 +142,13 @@ public: WaitForSingleObject(m_empty_sem, INFINITE); EnterCriticalSection(&m_mutex); #endif +#endif //LOL_FEATURE_THREADS /* Push value */ m_values[(m_start + m_count) % CAPACITY] = value; m_count++; +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H /* If there were poppers waiting, signal the "empty" cond var. */ if (m_poppers) @@ -170,10 +158,12 @@ public: LeaveCriticalSection(&m_mutex); ReleaseSemaphore(m_full_sem, 1, nullptr); #endif +#endif //LOL_FEATURE_THREADS } bool try_push(T value) { +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H pthread_mutex_lock(&m_mutex); /* If queue is full, wait on the "full" cond var. */ @@ -188,11 +178,13 @@ public: return false; EnterCriticalSection(&m_mutex); #endif +#endif //LOL_FEATURE_THREADS /* Push value */ m_values[(m_start + m_count) % CAPACITY] = value; m_count++; +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H /* If there were poppers waiting, signal the "empty" cond var. */ if (m_poppers) @@ -202,12 +194,14 @@ public: LeaveCriticalSection(&m_mutex); ReleaseSemaphore(m_full_sem, 1, nullptr); #endif +#endif //LOL_FEATURE_THREADS return true; } T pop() { +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H pthread_mutex_lock(&m_mutex); /* Wait until there is something in the queue. Be careful, we @@ -221,12 +215,19 @@ public: WaitForSingleObject(m_full_sem, INFINITE); EnterCriticalSection(&m_mutex); #endif +#endif //LOL_FEATURE_THREADS + +#if !LOL_FEATURE_THREADS + if (m_count == 0) + return T(0); +#endif //!LOL_FEATURE_THREADS /* Pop value */ T ret = m_values[m_start]; m_start = (m_start + 1) % CAPACITY; m_count--; +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H /* If there were pushers waiting, signal the "full" cond var. */ if (m_pushers) @@ -236,12 +237,14 @@ public: LeaveCriticalSection(&m_mutex); ReleaseSemaphore(m_empty_sem, 1, nullptr); #endif +#endif //LOL_FEATURE_THREADS return ret; } bool try_pop(T &ret) { +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H pthread_mutex_lock(&m_mutex); if (m_count == 0) @@ -255,12 +258,19 @@ public: return false; EnterCriticalSection(&m_mutex); #endif +#endif //LOL_FEATURE_THREADS + +#if !LOL_FEATURE_THREADS + if (m_count == 0) + return false; +#endif //!LOL_FEATURE_THREADS /* Pop value */ ret = m_values[m_start]; m_start = (m_start + 1) % CAPACITY; m_count--; +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H /* If there were pushers waiting, signal the "full" cond var. */ if (m_pushers) @@ -270,6 +280,7 @@ public: LeaveCriticalSection(&m_mutex); ReleaseSemaphore(m_empty_sem, 1, nullptr); #endif +#endif //LOL_FEATURE_THREADS return true; } @@ -278,6 +289,7 @@ private: static size_t const CAPACITY = N; T m_values[CAPACITY]; size_t m_start, m_count; +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H size_t m_poppers, m_pushers; pthread_mutex_t m_mutex; @@ -286,6 +298,7 @@ private: HANDLE m_empty_sem, m_full_sem; CRITICAL_SECTION m_mutex; #endif +#endif //LOL_FEATURE_THREADS }; class thread_base @@ -294,6 +307,7 @@ public: thread_base(std::function function) : m_function(function) { +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H /* Set the joinable attribute for systems who don't play nice */ pthread_attr_t attr; @@ -304,15 +318,18 @@ public: m_thread = CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)trampoline, this, 0, &m_tid); #endif +#endif //LOL_FEATURE_THREADS } virtual ~thread_base() { +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H pthread_join(m_thread, nullptr); #elif defined _WIN32 WaitForSingleObject(m_thread, INFINITE); #endif +#endif //LOL_FEATURE_THREADS } private: @@ -325,12 +342,14 @@ private: std::function m_function; +#if LOL_FEATURE_THREADS #if defined HAVE_PTHREAD_H pthread_t m_thread; #elif defined _WIN32 HANDLE m_thread; DWORD m_tid; #endif +#endif //LOL_FEATURE_THREADS }; } /* namespace lol */ diff --git a/src/sys/threadtypes.cpp b/src/sys/threadtypes.cpp index 86471c32..afede9fa 100644 --- a/src/sys/threadtypes.cpp +++ b/src/sys/threadtypes.cpp @@ -16,7 +16,7 @@ using namespace lol; //DefaultThreadManager -------------------------------------------------------- -bool DefaultThreadManager::AddWork(ThreadJob* job) +bool DefaultThreadManager::AddJob(ThreadJob* job) { return AddWork(job); } @@ -40,6 +40,7 @@ public: m_path = path; } String& GetPath() { return m_path; } + long int GetTime() { return m_time; } bool HasUpdated() { return m_updated; } void Restart() { @@ -135,7 +136,7 @@ void FileUpdateTester::TreatResult(ThreadJob* result) FileUpdateTesterJob* job = static_cast(result); if (job->HasUpdated()) { - m_files[job->GetPath()]->SetTime(job->m_time); + m_files[job->GetPath()]->SetTime(job->GetTime()); m_files[job->GetPath()]->SetUpdated(true); job->Restart(); m_job_done << job;