// // Get pointer to the object // template <typename T> T * ASLoader<T>::GetObject(CCHAR_P szLibraryName, CCHAR_P szClassName) { // Check cache HandleInfo * pHandleInfo = CheckLibraryHandle(szLibraryName); // Locad library if (pHandleInfo == NULL) { DoLoadLibrary(szLibraryName); pHandleInfo = CheckLibraryHandle(szLibraryName); } STLW::string sInitHandler(szClassName); sInitHandler.append(sLibraryPrefix); // Initialize class #if defined(linux) || defined(sun) // Linux brain-damaged stupidity void * vPtr = dlsym(pHandleInfo -> handle, sInitHandler.c_str()); InitPtr vVInitPtr; memcpy((void *)(&vVInitPtr), (void *)(&vPtr), sizeof(void *)); #else InitPtr vVInitPtr = (InitPtr)dlfunc(pHandleInfo -> handle, sInitHandler.c_str()); #endif if (vVInitPtr == NULL) { STLW::string sTMP("Cannot get magic symbol \""); sTMP.append(sInitHandler); sTMP.append("\" from library \""); sTMP.append(szLibraryName); sTMP.append("\""); throw UnixException(sTMP.c_str(), errno); } // Number of references ++(pHandleInfo -> refcount); // Create object T * pASObject = ((*vVInitPtr)()); // All done if (pASObject != NULL) { return pASObject; } STLW::string sTMP("Internal error in module \""); sTMP.append(szClassName); sTMP.append("\", library \""); sTMP.append(szLibraryName); sTMP.append("\""); throw UnixException(sTMP.c_str(), errno); // Make compiler happy; this should *not* happened return NULL; }
// // Constructor // Mutex::Mutex(const bool bRecursive) { INT_32 iRC = pthread_mutexattr_init(&oAttributes); if (iRC != 0) { throw UnixException("Mutex::Mutex()", iRC); } if (bRecursive) { pthread_mutexattr_settype(&oAttributes, PTHREAD_MUTEX_RECURSIVE); } iRC = pthread_mutex_init(&oMutex, &oAttributes); if (iRC != 0) { pthread_mutexattr_destroy(&oAttributes); throw UnixException("Mutex::Mutex()", iRC); } }
void Discoverable::runDeviceService() { try { std::tr1::shared_ptr<Service_t> service(RemoteDiscovery::service()); std::string address = "urn:"; address += randUuid( service->getSoap() ); std::string xaddrs = baseServ_->m_endpoint; Scopes_t scopes = Scopes_t(); scopes.item = vector_join(baseServ_->m_scopes, " "); EndpointReference_t endpoint = EndpointReference_t(); endpoint.address = &address; probeMatches_.resize(1); probeMatches_.back().endpoint = &endpoint; probeMatches_.back().scopes = &scopes; probeMatches_.back().types = &baseServ_->m_type; probeMatches_.back().xaddrs = &xaddrs; probeMatches_.back().version = 1; proxy_.reset(RemoteDiscovery::proxy()); proxy_->hello(probeMatches_.back()); SIGRLOG( SIGRDEBUG2, "[runDeviceService] Starting the service loop" ); if( service->bind(this) == -1 ) throw UnixException( errno ); while (true) { pthread_mutex_lock(&mutex_); if(bExitRequest_) { service->destroy(); bExitRequest_ = false; break; } pthread_mutex_unlock(&mutex_); if (service->accept() == -1) { SIGRLOG( SIGRWARNING, "Accept failed" ); continue; } if( service->serve() != 0 ) continue; service->destroy(); } service.reset(); proxy_.reset(RemoteDiscovery::proxy()); proxy_->bye(probeMatches_.back()); } catch (std::exception & ex) { SIGRLOG( SIGRWARNING, "[runDeviceService] Caught an exception: %s", ex.what() ); } SIGRLOG( SIGRDEBUG0, "[runDeviceService] The service loop stopped" ); }
// // Try to lock mutex // Mutex::State Mutex::TryLock() { const INT_32 iRC = pthread_mutex_trylock(&oMutex); if (iRC == EBUSY) { return LOCKED; } if (iRC != 0) { throw UnixException("Mutex::TryLock()", iRC); } return OK; }
// // Unlock mutex // Mutex::State Mutex::Unlock() { const INT_32 iRC = pthread_mutex_unlock(&oMutex); if (iRC == EPERM) { return NO; } if (iRC != 0) { throw UnixException("Mutex::Unlock()", iRC); } return OK; }
// // Lock mutex // Mutex::State Mutex::Lock() { const INT_32 iRC = pthread_mutex_lock(&oMutex); if (iRC == EDEADLK) { return DEADLOCK; } if (iRC != 0) { throw UnixException("Mutex::Lock()", iRC); } return OK; }
// // Load library // template <typename T>void * ASLoader<T>::DoLoadLibrary(CCHAR_P szLibraryName) { HandleInfo oHandleInfo; // Okay, try to load function oHandleInfo.handle = dlopen(szLibraryName, RTLD_NOW | RTLD_GLOBAL); oHandleInfo.refcount = 0; // Error? if (oHandleInfo.handle == NULL) { STLW::string sTMP("Cannot open library \""); sTMP.append(szLibraryName); sTMP.append("\": "); sTMP.append(dlerror()); throw UnixException(sTMP.c_str(), errno); } mLibraryMap[szLibraryName] = oHandleInfo; return oHandleInfo.handle; }