| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
|---|---|
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #include "CoreThread/BsCoreObjectCore.h" |
| 4 | #include "CoreThread/BsCoreThread.h" |
| 5 | |
| 6 | namespace bs |
| 7 | { |
| 8 | namespace ct |
| 9 | { |
| 10 | Signal CoreObject::mCoreGpuObjectLoadedCondition; |
| 11 | Mutex CoreObject::mCoreGpuObjectLoadedMutex; |
| 12 | |
| 13 | CoreObject::CoreObject() |
| 14 | :mFlags(0) |
| 15 | { } |
| 16 | |
| 17 | CoreObject::~CoreObject() |
| 18 | { |
| 19 | THROW_IF_NOT_CORE_THREAD; |
| 20 | } |
| 21 | |
| 22 | void CoreObject::initialize() |
| 23 | { |
| 24 | { |
| 25 | Lock lock(mCoreGpuObjectLoadedMutex); |
| 26 | setIsInitialized(true); |
| 27 | } |
| 28 | |
| 29 | setScheduledToBeInitialized(false); |
| 30 | |
| 31 | mCoreGpuObjectLoadedCondition.notify_all(); |
| 32 | } |
| 33 | |
| 34 | void CoreObject::synchronize() |
| 35 | { |
| 36 | if (!isInitialized()) |
| 37 | { |
| 38 | #if BS_DEBUG_MODE |
| 39 | if (BS_THREAD_CURRENT_ID == CoreThread::instance().getCoreThreadId()) |
| 40 | BS_EXCEPT(InternalErrorException, "You cannot call this method on the core thread. It will cause a deadlock!"); |
| 41 | #endif |
| 42 | |
| 43 | gCoreThread().submitAll(true); |
| 44 | |
| 45 | Lock lock(mCoreGpuObjectLoadedMutex); |
| 46 | while (!isInitialized()) |
| 47 | { |
| 48 | if (!isScheduledToBeInitialized()) |
| 49 | BS_EXCEPT(InternalErrorException, "Attempting to wait until initialization finishes but object is not scheduled to be initialized."); |
| 50 | |
| 51 | mCoreGpuObjectLoadedCondition.wait(lock); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void CoreObject::_setThisPtr(SPtr<CoreObject> ptrThis) |
| 57 | { |
| 58 | mThis = ptrThis; |
| 59 | } |
| 60 | } |
| 61 | } |