// Insert a key-value pair into the name database. // The key is constructed by concatenating rPrefix and rName. // If exceptOnErr is TRUE, then upon encountering an error, this method // will throw an exception. This is sometimes useful for indicating an // error from within an object constructor. OsStatus OsUtil::insertKeyValue(const UtlString& rPrefix, const UtlString& rName, const intptr_t value, UtlBoolean exceptOnErr) { OsNameDb* pDict; OsStatus res; assert(rName != "" || rPrefix != ""); pDict = OsNameDb::getNameDb(); UtlString key = rPrefix + rName; res = pDict->insert(key, value); if (exceptOnErr) { switch (res) { case OS_SUCCESS: break; // success, do nothing case OS_NAME_IN_USE: OsSysLog::add(FAC_KERNEL, PRI_ERR, "OsUtil::insertKeyValue - " "name already in use: %s%s", rPrefix.data(), rName.data()); break; default: OsSysLog::add(FAC_KERNEL, PRI_ERR, "OsUtil::insertKeyValue - " "OsStatus = %d", res); break; } } return res; }
int main() #endif { #ifdef TEST UtlMemCheck* pMemCheck = 0; pMemCheck = new UtlMemCheck(); // checkpoint for memory leak check #endif //TEST cout << "Entering main()" << endl; /* ============================ Testing Start ============================= */ /* ============================ OsProcess ==================================== */ #if defined(_WIN32) || defined(__pingtel_on_posix__) //vxworks wont use these classes osPrintf("Starting Process test...\n"); //Text Process Class if (TestProcessClass() != OS_SUCCESS) osPrintf("TestProcessterator FAILED!\n"); //Test Process Iterator if (TestProcessIterator() != OS_SUCCESS) osPrintf("TestProcessterator FAILED!\n"); //Test Process Manager methods if (TestProcessMgr() != OS_SUCCESS) osPrintf("TestProcessterator FAILED!\n"); osPrintf("Finished Process test.\n"); #endif /* ============================ OsTime ==================================== */ OsTime* pTime; pTime = new OsTime(); delete pTime; /* ============================ OsMutex =================================== */ OsMutex* pMutex; pMutex = new OsMutex(0); delete pMutex; /* ============================ OsBSem ==================================== */ OsBSem* pBSem; pBSem = new OsBSem(OsBSem::Q_PRIORITY, OsBSem::FULL); assert(pBSem->acquire() == OS_SUCCESS); assert(pBSem->tryAcquire() == OS_BUSY); assert(pBSem->release() == OS_SUCCESS); delete pBSem; /* ============================ OsCSem ==================================== */ OsCSem* pCSem; // the initial count on the semaphore will be 2 pCSem = new OsCSem(OsCSem::Q_PRIORITY, 2); assert(pCSem->acquire() == OS_SUCCESS); // take it once assert(pCSem->acquire() == OS_SUCCESS); // take it twice assert(pCSem->tryAcquire() == OS_BUSY); assert(pCSem->release() == OS_SUCCESS); // release once assert(pCSem->release() == OS_SUCCESS); // release twice delete pCSem; /* ============================ OsLock ==================================== */ // Create a binary semaphore for use with an OsLock object pBSem = new OsBSem(OsBSem::Q_PRIORITY, OsBSem::FULL); // Acquire semaphore at the start of the method, release it on exit guardedWithBSem(*pBSem); delete pBSem; /* ============================ OsRWMutex ================================= */ OsRWMutex* pRWMutex; pRWMutex = new OsRWMutex(OsRWMutex::Q_FIFO); assert(pRWMutex->acquireRead() == OS_SUCCESS); assert(pRWMutex->tryAcquireWrite() == OS_BUSY); assert(pRWMutex->releaseRead() == OS_SUCCESS); assert(pRWMutex->tryAcquireWrite() == OS_SUCCESS); assert(pRWMutex->tryAcquireRead() == OS_BUSY); assert(pRWMutex->releaseWrite() == OS_SUCCESS); delete pRWMutex; /* ============================ OsReadLock and OsWriteLock ================ */ // Create an OsRWMutex for use with OsReadLock and OsWriteLock objects pRWMutex = new OsRWMutex(OsRWMutex::Q_FIFO); // Acquire read lock at the start of the method, release it on exit guardedForReading(*pRWMutex); // Acquire write lock at the start of the method, release it on exit guardedForWriting(*pRWMutex); delete pRWMutex; /* ============================ OsNameDb ================================== */ OsNameDb* pNameDb; int storedInt; pNameDb = OsNameDb::getNameDb(); assert(pNameDb->isEmpty()); assert(pNameDb->numEntries() == 0); assert(pNameDb->insert("test1", 1) == OS_SUCCESS); assert(pNameDb->insert("test1", 2) == OS_NAME_IN_USE); assert(!pNameDb->isEmpty()); assert(pNameDb->numEntries() == 1); assert(pNameDb->insert("test2", 2) == OS_SUCCESS); assert(pNameDb->numEntries() == 2); assert(pNameDb->lookup("test1", NULL) == OS_SUCCESS); assert(pNameDb->lookup("test1", &storedInt) == OS_SUCCESS); assert(storedInt == 1); assert(pNameDb->lookup("test2", &storedInt) == OS_SUCCESS); assert(storedInt == 2); assert(pNameDb->lookup("test3", NULL) == OS_NOT_FOUND); pNameDb->remove("test1"); pNameDb->remove("test2"); delete pNameDb; /* ============================ OsMsgQ ==================================== */ OsMsgQ* pMsgQ1; OsMsg* pMsg1; OsMsg* pMsg2; OsMsg* pRecvMsg; pMsgQ1 = new OsMsgQ(OsMsgQ::DEF_MAX_MSGS, OsMsgQ::DEF_MAX_MSG_LEN, OsMsgQ::Q_PRIORITY, "MQ1"); pMsg1 = new OsMsg(OsMsg::UNSPECIFIED, 0); pMsg2 = new OsMsg(OsMsg::UNSPECIFIED, 0); assert(pMsgQ1->isEmpty()); assert(pMsgQ1->numMsgs() == 0); assert(pMsgQ1->getSendHook() == NULL); pMsgQ1->setSendHook(msgSendHook); assert(pMsgQ1->getSendHook() == msgSendHook); OsStatus stat = pMsgQ1->send(*pMsg1); assert(stat == OS_SUCCESS); assert(!pMsgQ1->isEmpty()); assert(pMsgQ1->numMsgs() == 1); stat = pMsgQ1->send(*pMsg2); assert(stat == OS_SUCCESS); assert(pMsgQ1->numMsgs() == 2); stat = pMsgQ1->receive(pRecvMsg); assert(stat == OS_SUCCESS); delete pRecvMsg; assert(pMsgQ1->numMsgs() == 1); stat = pMsgQ1->receive(pRecvMsg); assert(stat == OS_SUCCESS); delete pRecvMsg; assert(pMsgQ1->numMsgs() == 0); delete pMsg1; delete pMsg2; #if !defined(_VXWORKS) try { OsMsgQ* pMsgQ2 = new OsMsgQ(OsMsgQ::DEF_MAX_MSGS, OsMsgQ::DEF_MAX_MSG_LEN, OsMsgQ::Q_PRIORITY, "MQ1"); delete pMsgQ2; } catch (const OsExcept* exc) { UtlString txt; cout << "Exception:" << endl; cout << " Major Code: " << exc->getMajorCode() << endl; cout << " Minor Code: " << exc->getMinorCode() << endl; txt = exc->getText(); cout << " Text: " << txt.data() << endl; txt = exc->getContext(); cout << " Context: " << txt.data() << endl; delete exc; } #endif delete pMsgQ1; /* ============================ OsCallback ================================ */ OsCallback* pCallback; pCallback = new OsCallback(12345, handleTimerEvent); pCallback->signal(67890); delete pCallback; /* ============================ OsEvent =================================== */ OsTime eventTimeout(2,0); OsEvent* pEvent; cout << "Testing OsEvent, please wait..." << endl; pEvent = new OsEvent(12345); int epochTime = time(NULL); assert(pEvent->wait(eventTimeout) != OS_SUCCESS); pEvent->signal(67890); assert(pEvent->wait(eventTimeout) == OS_SUCCESS); pEvent->reset(); assert(pEvent->wait(eventTimeout) != OS_SUCCESS); epochTime = time(NULL) - epochTime; // Make sure we waited (approximately) 2 seconds each time. assert(epochTime > 2 && epochTime < 6); delete pEvent; cout << "Done testing OsEvent." << endl; /* ============================ OsConfigDb ================================ */ OsConfigDb* pConfigDb; pConfigDb = new OsConfigDb(); delete pConfigDb; /* ============================ OsTimerTask =============================== */ OsTimerTask* pTimerTask; pTimerTask = OsTimerTask::getTimerTask(); OsTask::delay(500); // wait 1/2 second osPrintf("Going to delete the timer task.\n"); delete pTimerTask; /* ============================ OsTimer =================================== */ pTimerTask = OsTimerTask::getTimerTask(); OsTask::delay(500); // wait 1/2 second OsCallback* pNotifier; OsCallback* pNotifier2; OsTimer* pTimer; OsTimer* pTimer2; OsTime tenMsec(0, 10000);// timer offset ten msec into the future OsTime oneSecond(1,0); // timer offset one second into the future OsTime twoSeconds(2,0); // timer offset two seconds into the future OsTime tenSeconds(10,0); // timer offset ten seconds into the future OsTime tenYears(10*365*24*60*60, 0); // ten years into the future cout << "About to handle timer 1 (immediate)" << endl; pNotifier = new OsCallback(1, handleTimerEvent); pTimer = new OsTimer(*pNotifier); cout << " Oneshot timer 1 armed at " << OsDateTime::getSecsSinceEpoch() << " secs" << endl; pTimer->oneshotAfter(OsTime::NO_WAIT); delete pTimer; delete pNotifier; cout << "About to handle timer 2" << endl; pNotifier = new OsCallback(2, handleTimerEvent); pTimer = new OsTimer(*pNotifier); cout << " Oneshot timer 2 armed at " << OsDateTime::getSecsSinceEpoch() << " secs" << endl; pTimer->oneshotAfter(oneSecond); delete pTimer; // delete the timer before it can expire delete pNotifier; cout << "About to handle timer 3" << endl; pNotifier = new OsCallback(3, handleTimerEvent); pTimer = new OsTimer(*pNotifier); cout << " Oneshot timer 3 armed at " << OsDateTime::getSecsSinceEpoch() << " secs" << endl; pTimer->oneshotAfter(oneSecond); pTimer->stop(); // stop the timer before it can expire delete pTimer; delete pNotifier; cout << "About to handle timer 4 (after 1 sec)" << endl; pNotifier = new OsCallback(4, handleTimerEvent); pTimer = new OsTimer(*pNotifier); cout << " Oneshot timer armed at " << OsDateTime::getSecsSinceEpoch() << endl; pTimer->oneshotAfter(oneSecond); OsTask::delay(1500); // sleep for 1.5 seconds delete pTimer; delete pNotifier; cout << "About to handle timer 5" << endl; pNotifier = new OsCallback(5, handleTimerEvent); pTimer = new OsTimer(*pNotifier); cout << " Periodic timer 5 armed at " << OsDateTime::getSecsSinceEpoch() << " secs" << endl; pTimer->periodicEvery(oneSecond, oneSecond); delete pTimer; // delete the timer before it can expire delete pNotifier; cout << "About to handle timer 6" << endl; pNotifier = new OsCallback(6, handleTimerEvent); pTimer = new OsTimer(*pNotifier); cout << " Periodic timer 6 armed at " << OsDateTime::getSecsSinceEpoch() << " secs" << endl; pTimer->periodicEvery(oneSecond, oneSecond); pTimer->stop(); // stop the timer before it can expire delete pTimer; delete pNotifier; cout << "About to handle timer 7 (immediate, then every second)" << endl; cout << "About to handle timer 8 (immediate, then every two seconds)" << endl; pNotifier = new OsCallback(7, handleTimerEvent); pNotifier2 = new OsCallback(8, handleTimerEvent); pTimer = new OsTimer(*pNotifier); pTimer2 = new OsTimer(*pNotifier2); cout << " Periodic timer 7 armed at " << OsDateTime::getSecsSinceEpoch() << " secs" << endl; pTimer->periodicEvery(OsTime::NO_WAIT, oneSecond); cout << " Periodic timer 8 armed at " << OsDateTime::getSecsSinceEpoch() << " secs" << endl; pTimer2->periodicEvery(OsTime::NO_WAIT, twoSeconds); OsTask::delay(4500); // sleep for 4.5 seconds pTimer->stop(); pTimer2->stop(); delete pTimer; delete pTimer2; delete pNotifier; delete pNotifier2; cout << "About to handle timer 9 (after ten seconds)" << endl; pNotifier = new OsCallback(9, handleTimerEvent); pTimer = new OsTimer(*pNotifier); cout << " Oneshot timer 9 armed at " << OsDateTime::getSecsSinceEpoch() << " secs" << endl; pTimer->oneshotAfter(tenSeconds); OsTask::delay(12000); // sleep for 12 seconds delete pTimer; delete pNotifier; cout << "About to handle timer 10 (after 6912 seconds)" << endl; OsTime secs6912(6912, 0); pNotifier = new OsCallback(10, handleTimerEvent); pTimer = new OsTimer(*pNotifier); cout << " Oneshot timer 10 armed at " << OsDateTime::getSecsSinceEpoch() << " secs" << endl; pTimer->oneshotAfter(secs6912); OsTask::delay(12000); // sleep for 12 seconds delete pTimer; delete pNotifier; cout << "Verify that we can schedule timers in the distant future" << endl; pNotifier = new OsCallback(11, handleTimerEvent); pTimer = new OsTimer(*pNotifier); cout << " Oneshot timer armed 11 at " << OsDateTime::getSecsSinceEpoch() << " secs" << endl; pTimer->oneshotAfter(tenYears); pTimer->stop(); pTimer->periodicEvery(OsTime::NO_WAIT, tenYears); pTimer->stop(); delete pTimer; delete pNotifier; cout << "Run 100x/second timer for 2 seconds..." << endl; int rapidTimerExpirations = 0; pNotifier = new OsCallback(&rapidTimerExpirations, handleRapidTimerEvent); pTimer = new OsTimer(*pNotifier); cout << " Rapid-fire timer armed at " << OsDateTime::getSecsSinceEpoch() << " secs" << endl; pTimer->periodicEvery(OsTime::NO_WAIT, tenMsec); OsTask::delay(2000); pTimer->stop(); cout << " Rapid-fire timer stopped at " << OsDateTime::getSecsSinceEpoch() << " secs" << endl; cout << "Rapid-fire timer expired " << rapidTimerExpirations << " times" << endl; delete pTimer; delete pNotifier; /* ============================ OsTimerTask cleanup ======================= */ // The timer task may have been created indirectly (as a result of // creating OsTimer objects). If the timer task exists, delete it now. pTimerTask = OsTimerTask::getTimerTask(); if (pTimerTask != NULL) { osPrintf("Going to delete the timer task.\n"); delete pTimerTask; } /* ============================ OsNameDb cleanup ========================== */ // The name database may have been created indirectly (as a result of // creating objects with global names). If the name database exists, // delete it now. pNameDb = OsNameDb::getNameDb(); if (pNameDb != NULL) delete pNameDb; /* ============================ Testing Finish ============================ */ cout << "Leaving main()" << endl; #ifdef TEST assert(pMemCheck->delta() == 0); // check for memory leak delete pMemCheck; #endif //TEST return 0; }