diff --git a/BlueBerry/Bundles/org.blueberry.core.jobs/src/berryIJobStatus.h b/BlueBerry/Bundles/org.blueberry.core.jobs/src/berryIJobStatus.h index e2ccc16681..f56c2d8081 100644 --- a/BlueBerry/Bundles/org.blueberry.core.jobs/src/berryIJobStatus.h +++ b/BlueBerry/Bundles/org.blueberry.core.jobs/src/berryIJobStatus.h @@ -1,45 +1,45 @@ /*=================================================================== -The Medical Imaging Interaction Toolkit (MITK) +BlueBerry Platform Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef _BERRYIJOBSTATUS_H #define _BERRYIJOBSTATUS_H #include "berryIStatus.h" #include "berryObject.h" #include "berryJob.h" namespace berry { /** * Represents status relating to the execution of jobs. * @see IStatus * @noimplement This interface is not intended to be implemented by clients. * @noextend This interface is not intended to be extended by clients. */ struct IJobStatus : public IStatus { berryObjectMacro(IJobStatus) /** * Returns the job associated with this status. * @return the job associated with this status */ virtual Job::Pointer GetJob() = 0; }; } #endif /*_BERRYIJOBSTATUS_H */ \ No newline at end of file diff --git a/BlueBerry/Bundles/org.blueberry.core.jobs/src/berryJobStatus.h b/BlueBerry/Bundles/org.blueberry.core.jobs/src/berryJobStatus.h index 26248ee5d4..b09be80d00 100644 --- a/BlueBerry/Bundles/org.blueberry.core.jobs/src/berryJobStatus.h +++ b/BlueBerry/Bundles/org.blueberry.core.jobs/src/berryJobStatus.h @@ -1,105 +1,105 @@ /*=================================================================== -The Medical Imaging Interaction Toolkit (MITK) +BlueBerry Platform Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef _BERRYJOBSTATUS_H #define _BERRYJOBSTATUS_H #include "berryIJobStatus.h" #include "berryStatus.h" #include "berryJob.h" #include namespace berry { class BERRY_JOBS JobStatus : public IJobStatus { public: berryObjectMacro(JobStatus) /** * Creates a new job status with no interesting error code or exception. * @param severity * @param job * @param message */ JobStatus( const Status::Severity& severity, Job::Pointer sptr_job, const std::string& message) ; /** * @see IJobStatus#GetJob() */ Job::Pointer GetJob() ; /** * @see org.blueberry.solstice.common IStatus GetChildren() */ std::vector GetChildren() const ; /** * @see org.blueberry.solstice.common IStatus GetCode() */ int GetCode() const ; /** *@see org.blueberry.solstice.common IStatus GetException () */ std::exception GetException() const ; /** * @see org.blueberry.solstice.common GetMessage () */ std::string GetMessage() const ; /** * @see org.blueberry.solstice.common IStatus GetPlugin() */ std::string GetPlugin() const ; /** * @see org.blueberry.solstice.common IStatus GetSeverity () */ Severity GetSeverity() const ; /** * @see org.blueberry.solstice.common IsMultiStatus () */ bool IsMultiStatus() const ; /** * @see org.blueberry.solstice.common IStatus IsOk () */ bool IsOK() const ; /** * @see org.blueberry.solstice.common IStatus Matches () */ bool Matches(const Severities& severityMask) const ; private: Job::Pointer m_myJob ; Status::Pointer m_internalStatus ; }; } #endif /* _BERRYJOBSTATUS_H */ \ No newline at end of file diff --git a/BlueBerry/Bundles/org.blueberry.core.jobs/src/internal/berryWorkerPool.cpp b/BlueBerry/Bundles/org.blueberry.core.jobs/src/internal/berryWorkerPool.cpp index bdfe7b6094..c57dc20b4c 100644 --- a/BlueBerry/Bundles/org.blueberry.core.jobs/src/internal/berryWorkerPool.cpp +++ b/BlueBerry/Bundles/org.blueberry.core.jobs/src/internal/berryWorkerPool.cpp @@ -1,219 +1,219 @@ /*=================================================================== -The Medical Imaging Interaction Toolkit (MITK) +BlueBerry Platform Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #define NOMINMAX #include "berryWorkerPool.h" #include "berryJobManager.h" #include #include #include namespace berry { WorkerPool::WorkerPool(JobManager* myJobManager) : m_ptrManager(myJobManager), m_numThreads(0), m_sleepingThreads(0), m_threads( 10), m_busyThreads(0) // m_isDaemon(false), { } const long WorkerPool::BEST_BEFORE = 60000; const int WorkerPool::MIN_THREADS = 1; void WorkerPool::Shutdown() { Poco::ScopedLock LockMe(m_mutexOne); for(int i = 0; i<= m_numThreads; i++) { notify(); } } void WorkerPool::Add(Worker::Pointer worker) { Poco::Mutex::ScopedLock lock(m_mutexOne); m_threads.push_back(worker); } void WorkerPool::DecrementBusyThreads() { Poco::ScopedLock lockOne(m_mutexOne); //impossible to have less than zero busy threads if (--m_busyThreads < 0) { //TODO Decrementbusythreads if (jobmanager.debug) assert.istrue(false, integer.tostring(busythreads)); m_busyThreads = 0; } } void WorkerPool::IncrementBusyThreads() { Poco::ScopedLock lockOne(m_mutexOne); if (++m_busyThreads > m_numThreads) { m_busyThreads = m_numThreads; } } bool WorkerPool::Remove(Worker::Pointer worker) { Poco::ScopedLock lockOne(m_mutexOne); std::vector::iterator end = std::remove(m_threads.begin(), m_threads.end(), worker); bool removed = end != m_threads.end(); m_threads.erase(end); return removed; } void WorkerPool::EndWorker(Worker::Pointer sptr_worker) { Poco::ScopedLock lock(m_mutexOne); Remove(sptr_worker); } void WorkerPool::Sleep(long duration) { Poco::ScopedLock lock(m_mutexOne); m_sleepingThreads++; m_busyThreads--; try { wait(duration); throw FinallyThrowException(); } catch (FinallyThrowException&) { m_sleepingThreads--; m_busyThreads++; } catch (...) { m_sleepingThreads--; m_busyThreads++; } } InternalJob::Pointer WorkerPool::StartJob(Worker* worker) { // if we're above capacity, kill the thread { Poco::Mutex::ScopedLock lockOne(m_mutexOne); if (!m_ptrManager->IsActive()) { // must remove the worker immediately to prevent all threads from expiring Worker::Pointer sptr_worker(worker); EndWorker(sptr_worker); return InternalJob::Pointer(0); } //set the thread to be busy now in case of reentrant scheduling IncrementBusyThreads(); } Job::Pointer ptr_job(0); try { ptr_job = m_ptrManager->StartJob(); //spin until a job is found or until we have been idle for too long Poco::Timestamp idleStart; while (m_ptrManager->IsActive() && ptr_job == 0) { long tmpSleepTime = long(m_ptrManager->SleepHint()); if (tmpSleepTime > 0) Sleep(std::min(tmpSleepTime, BEST_BEFORE)); ptr_job = m_ptrManager->StartJob(); //if we were already idle, and there are still no new jobs, then the thread can expire { Poco::Mutex::ScopedLock lockOne(m_mutexOne); Poco::Timestamp tmpCurrentTime; long long tmpTime = tmpCurrentTime - idleStart; if (ptr_job == 0 && (tmpTime > BEST_BEFORE) && (m_numThreads - m_busyThreads) > MIN_THREADS) { //must remove the worker immediately to prevent all threads from expiring Worker::Pointer sptr_worker(worker); EndWorker(sptr_worker); return InternalJob::Pointer(0); } } } if (ptr_job != 0) { //if this job has a rule, then we are essentially acquiring a lock //if ((job.getRule() != 0) && !(job instanceof ThreadJob)) { // //don't need to re-aquire locks because it was not recorded in the graph // //that this thread waited to get this rule // manager.getLockManager().addLockThread(Thread.currentThread(), job.getRule()); // } //see if we need to wake another worker if (m_ptrManager->SleepHint() <= 0) JobQueued(); } throw FinallyThrowException(); } catch (FinallyThrowException&) { //decrement busy thread count if we're not running a job if (ptr_job == 0) DecrementBusyThreads(); } catch (...) { DecrementBusyThreads(); } return ptr_job; } void WorkerPool::JobQueued() { Poco::ScopedLock lockOne(m_mutexOne); //if there is a sleeping thread, wake it up if (m_sleepingThreads > 0) { notify(); return; } //create a thread if all threads are busy if (m_busyThreads >= m_numThreads) { WorkerPool::WeakPtr wp_WorkerPool(WorkerPool::Pointer(this)); Worker::Pointer sptr_worker(new Worker(wp_WorkerPool)); Add(sptr_worker); sptr_worker->Start(); return; } } void WorkerPool::EndJob(InternalJob::Pointer job, IStatus::Pointer result) { DecrementBusyThreads(); //TODO LockManager // //need to end rule in graph before ending job so that 2 threads // //do not become the owners of the same rule in the graph // if ((job.getRule() != null) && !(job instanceof ThreadJob)) { // //remove any locks this thread may be owning on that rule // manager.getLockManager().removeLockCompletely(Thread.currentThread(), job.getRule()); // } m_ptrManager->EndJob(job, result, true); // //ensure this thread no longer owns any scheduling rules // manager.implicitJobs.endJob(job); } } \ No newline at end of file diff --git a/BlueBerry/Testing/org.blueberry.core.runtime.tests/src/berryPreferencesServiceTest.cpp b/BlueBerry/Testing/org.blueberry.core.runtime.tests/src/berryPreferencesServiceTest.cpp index ac5015bf6f..029cfc10cc 100644 --- a/BlueBerry/Testing/org.blueberry.core.runtime.tests/src/berryPreferencesServiceTest.cpp +++ b/BlueBerry/Testing/org.blueberry.core.runtime.tests/src/berryPreferencesServiceTest.cpp @@ -1,140 +1,140 @@ /*=================================================================== -The Medical Imaging Interaction Toolkit (MITK) +BlueBerry Platform Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "berryPreferencesServiceTest.h" #include #include #include #include "berryLog.h" #include #include #include "Poco/File.h" #include "Poco/Path.h" #include "Poco/AutoPtr.h" #include #include #include using namespace std; namespace berry { PreferencesServiceTest::PreferencesServiceTest(const std::string& testName) : berry::TestCase(testName) {} CppUnit::Test* PreferencesServiceTest::Suite() { CppUnit::TestSuite* suite = new CppUnit::TestSuite("PreferencesServiceTest"); CppUnit_addTest(suite, PreferencesServiceTest, TestAll); return suite; } void PreferencesServiceTest::TestAll() { try { IPreferencesService::Pointer prefService = Platform::GetServiceRegistry().GetServiceById(IPreferencesService::ID); assert(prefService.IsNotNull()); /// Test for: IPreferences::GetSystemPreferences() IPreferences::Pointer sysPrefs = prefService->GetSystemPreferences(); assert(sysPrefs.IsNotNull()); /// Test for: IPreferences::GetUserPreferences(std::string name) IPreferences::Pointer testUserPrefs = prefService->GetUserPreferences("testUser"); assert(testUserPrefs.IsNotNull()); /// Test for: IPreferences::GetUsers() std::vector userList = prefService->GetUsers(); // userList should now contain "testUser" bool userListContainsTestUser = false; for (std::vector::iterator it = userList.begin() ; it != userList.end(); it++) { if(*it == "testUser") { userListContainsTestUser = true; break; } } assert(userListContainsTestUser); IBerryPreferencesService::Pointer berryPrefService = prefService.Cast(); // optional test for IBerryPreferencesService if(berryPrefService.IsNotNull()) { /// Test for: IBerryPreferencesService::ExportPreferences(Poco::File f, std::string name="") // write general prefs std::string sysPrefsExportFilePath = Poco::Path::temp() + Poco::Path::separator() + "systemBerryPreferences"; Poco::File sysPrefsExportFile(sysPrefsExportFilePath); sysPrefs->PutInt("testNumber", 1); berryPrefService->ExportPreferences(sysPrefsExportFile); // assert somethings was written assert(sysPrefsExportFile.getSize() > 0); // write testUser prefs std::string testUserPrefsExportFilePath = Poco::Path::temp() + Poco::Path::separator() + "testUserBerryPreferences"; Poco::File testUserPrefsExportFile(testUserPrefsExportFilePath); testUserPrefs->PutInt("testNumber", 2); berryPrefService->ExportPreferences(testUserPrefsExportFile, "testUser"); assert(testUserPrefsExportFile.getSize() > 0); /// Test for: IBerryPreferencesService::ImportPreferences(Poco::File f, std::string name="") // import general prefs // change testNumber value sysPrefs->PutInt("testNumber", 3); berryPrefService->ImportPreferences(sysPrefsExportFile); // "testNumber" preference should now again be overwritten with its old value 1 assert(sysPrefs->GetInt("testNumber", 3) == 1); // import testUser prefs // change testNumber value testUserPrefs->PutInt("testNumber", 4); berryPrefService->ImportPreferences(testUserPrefsExportFile, "testUser"); // "testNumber" preference should now again be overwritten with its old value 2 assert(testUserPrefs->GetInt("testNumber", 4) == 2); // delete files again sysPrefsExportFile.remove(); testUserPrefsExportFile.remove(); } } catch (Poco::CreateFileException& e) { std::string msg = "Failed to create preferences file: "; msg.append(e.what()); this->fail( msg ); } catch (std::exception& e) { this->fail( e.what() ); } catch (...) { this->fail( "unknown exception occured" ); } } } \ No newline at end of file diff --git a/BlueBerry/Testing/org.blueberry.core.runtime.tests/src/berryPreferencesTest.cpp b/BlueBerry/Testing/org.blueberry.core.runtime.tests/src/berryPreferencesTest.cpp index 30d6a0ac6f..6798a4bb9e 100644 --- a/BlueBerry/Testing/org.blueberry.core.runtime.tests/src/berryPreferencesTest.cpp +++ b/BlueBerry/Testing/org.blueberry.core.runtime.tests/src/berryPreferencesTest.cpp @@ -1,177 +1,177 @@ /*=================================================================== -The Medical Imaging Interaction Toolkit (MITK) +BlueBerry Platform Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "berryPreferencesTest.h" #include #include #include #include #include #include "berryLog.h" #include "Poco/File.h" #include "Poco/Path.h" #include #include #include using namespace std; namespace berry { PreferencesTest::PreferencesTest(const std::string& testName) : berry::TestCase(testName) {} CppUnit::Test* PreferencesTest::Suite() { CppUnit::TestSuite* suite = new CppUnit::TestSuite("PreferencesTest"); CppUnit_addTest(suite, PreferencesTest, TestAll); return suite; } // simple class for testing berry changed events class TestPreferencesChangedListener { public: TestPreferencesChangedListener(IBerryPreferences* _berryPrefNode) : numCalled(0), berryPrefNode(_berryPrefNode) { berryPrefNode->OnChanged.AddListener( berry::MessageDelegate1 ( this, &TestPreferencesChangedListener::PreferencesChanged ) ); }; ~TestPreferencesChangedListener() { berryPrefNode->OnChanged.RemoveListener( berry::MessageDelegate1 ( this, &TestPreferencesChangedListener::PreferencesChanged ) ); }; void PreferencesChanged(const IBerryPreferences*) { ++numCalled; } int numCalled; IBerryPreferences* berryPrefNode; }; void PreferencesTest::TestAll() { IPreferencesService::Pointer prefService = Platform::GetServiceRegistry().GetServiceById(IPreferencesService::ID); assert(prefService.IsNotNull()); /// Test for: IPreferences::GetSystemPreferences() IPreferences::Pointer root = prefService->GetSystemPreferences(); assert(root.IsNotNull()); { BERRY_INFO << "testing Preferences::Node(), Preferences::NodeExists(), Preferences::Parent(), " "Preferences::ChildrenNames(), Preferences::RemoveNode()"; berry::IPreferences::Pointer editorsNode(0); editorsNode = root->Node("/editors"); assert(editorsNode.IsNotNull()); assert(editorsNode->NodeExists("/editors")); assert(editorsNode->Parent() == root); berry::IPreferences::Pointer editorsGeneralNode = root->Node("/editors/general"); assert(editorsNode->NodeExists("/editors/general")); berry::IPreferences::Pointer editorsSyntaxNode = root->Node("/editors/syntax"); assert(editorsGeneralNode->NodeExists("/editors/syntax")); berry::IPreferences::Pointer editorsFontNode = root->Node("/editors/font"); assert(editorsSyntaxNode->NodeExists("/editors/font")); vector childrenNames; childrenNames.push_back("general"); childrenNames.push_back("syntax"); childrenNames.push_back("font"); assert(editorsNode->ChildrenNames() == childrenNames); editorsFontNode->RemoveNode(); try { editorsFontNode->Parent(); failmsg("this should throw a Poco::IllegalStateException"); } catch (Poco::IllegalStateException) { // expected } } // testing methods // Preferences::put*() // Preferences::get*() { BERRY_INFO << "testing Preferences::put*(), Preferences::get*(), OnChanged"; assert(root->NodeExists("/editors/general")); berry::IPreferences::Pointer editorsGeneralNode = root->Node("/editors/general"); IBerryPreferences::Pointer berryEditorsGeneralNode = editorsGeneralNode.Cast< IBerryPreferences >(); assert(berryEditorsGeneralNode.IsNotNull()); TestPreferencesChangedListener listener(berryEditorsGeneralNode.GetPointer()); std::string strKey = "Bad words";std::string strValue = "badword1 badword2"; editorsGeneralNode->Put(strKey, strValue); assert(listener.numCalled == 1); assert(editorsGeneralNode->Get(strKey, "") == strValue); assert(editorsGeneralNode->Get("wrong key", "default value") == "default value"); strKey = "Show Line Numbers";bool bValue = true; editorsGeneralNode->PutBool(strKey, bValue); assert(listener.numCalled == 2); assert(editorsGeneralNode->GetBool(strKey, !bValue) == bValue); strKey = "backgroundcolor"; strValue = "#00FF00"; editorsGeneralNode->PutByteArray(strKey, strValue); assert(listener.numCalled == 3); assert(editorsGeneralNode->GetByteArray(strKey, "") == strValue); strKey = "update time"; double dValue = 1.23; editorsGeneralNode->PutDouble(strKey, dValue); assert(editorsGeneralNode->GetDouble(strKey, 0.0) == dValue); strKey = "update time float"; float fValue = 1.23f; editorsGeneralNode->PutFloat(strKey, fValue); assert(editorsGeneralNode->GetFloat(strKey, 0.0f) == fValue); strKey = "Break on column"; int iValue = 80; editorsGeneralNode->PutInt(strKey, iValue); assert(editorsGeneralNode->GetInt(strKey, 0) == iValue); strKey = "Maximum number of words"; long lValue = 11000000; editorsGeneralNode->PutLong(strKey, lValue); assert(editorsGeneralNode->GetLong(strKey, 0) == lValue); } } } \ No newline at end of file