void ZookeeperClient::watcher(zhandle_t *zzh, int type, int state, const char *path, void*) { //From cli.c /* Be careful using zh here rather than zzh - as this may be mt code * the client lib may call the watcher before zookeeper_init returns */ this->m_state=state; if (type == ZOO_SESSION_EVENT) { if (state == ZOO_CONNECTED_STATE) { } else if (state == ZOO_AUTH_FAILED_STATE) { this->m_err= getMessage(ERR_CONN_ZKNOAUTH); this->close(); } else if (state == ZOO_EXPIRED_SESSION_STATE) { this->m_err= getMessage(ERR_CONN_ZKEXP); this->close(); } } // signal the cond var { if (state == ZOO_CONNECTED_STATE){ DRILL_MT_LOG(DRILL_LOG(LOG_TRACE) << "Connected to Zookeeper." << std::endl;) } boost::lock_guard<boost::mutex> bufferLock(this->m_cvMutex); this->m_bConnecting=false; }
status_t RecordIterator::next(){ status_t ret=QRY_SUCCESS; this->m_currentRecord++; if(!this->m_pQueryResult->isCancelled()){ if(this->m_pCurrentRecordBatch==NULL || this->m_currentRecord==this->m_pCurrentRecordBatch->getNumRecords()){ boost::lock_guard<boost::mutex> bufferLock(this->m_recordBatchMutex); if(this->m_pCurrentRecordBatch !=NULL){ DRILL_LOG(LOG_TRACE) << "Deleted old Record batch " << (void*) m_pCurrentRecordBatch << std::endl; delete this->m_pCurrentRecordBatch; //free the previous record batch this->m_pCurrentRecordBatch=NULL; } this->m_currentRecord=0; this->m_pQueryResult->waitForData(); if(m_pQueryResult->hasError()){ return m_pQueryResult->getErrorStatus(); } this->m_pCurrentRecordBatch=this->m_pQueryResult->getNext(); if(this->m_pCurrentRecordBatch != NULL){ DRILL_LOG(LOG_TRACE) << "Fetched new Record batch " << std::endl; }else{ DRILL_LOG(LOG_TRACE) << "No new Record batch found " << std::endl; } if(this->m_pCurrentRecordBatch==NULL || this->m_pCurrentRecordBatch->getNumRecords()==0){ DRILL_LOG(LOG_TRACE) << "No more data." << std::endl; ret = QRY_NO_MORE_DATA; }else if(this->m_pCurrentRecordBatch->hasSchemaChanged()){ ret=QRY_SUCCESS_WITH_INFO; } } }else{ ret=QRY_CANCEL; } return ret; }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OSStatus OALBuffer::AddAudioDataStatic(char* inAudioData, UInt32 inAudioDataSize, ALenum format, ALsizei freq) { #if LOG_VERBOSE DebugMessageN5("OALBuffer::AddAudioDataStatic() - OALBuffer:inAudioData:inAudioDataSize:format:freq = %ld:%p:%ld:%d:%d", (long int) mSelfToken, inAudioData, (long int) inAudioDataSize, format, freq); #endif #if LOG_EXTRAS DebugMessage("AddAudioDataStatic called: Converting Data Now"); #endif CAGuard::Locker bufferLock(mBufferLock); try { if (!IsFormatSupported(format)) throw ((OSStatus) AL_INVALID_VALUE); // this is not a valid buffer token or is an invalid format // don't allow if the buffer is in a queue if (mAttachedSourceList->Size() > 0) { DebugMessage("AddAudioDataStatic ATTACHMENT > 0"); throw ((OSStatus) AL_INVALID_OPERATION); } mPreConvertedDataSize = (UInt32) inAudioDataSize; OSStatus result = noErr; // if this buffer was using memory created by the library, free it now and initialize mData if (!mAppOwnsBufferMemory && (mData != NULL)) { free (mData); mData = NULL; } mData = (UInt8*) inAudioData; mDataSize = (UInt32) inAudioDataSize; result = FillInASBD(mDataFormat, format, freq); THROW_RESULT mPreConvertedDataFormat.SetFrom(mDataFormat); // make sure they are the same so original format info can be returned to caller } catch (OSStatus result) { mData = NULL; mAppOwnsBufferMemory = false; DebugMessageN1("AddAudioDataStatic Failed - err = %ld\n", (long int) result); alSetError(result); } catch (...) { mData = NULL; mAppOwnsBufferMemory = false; DebugMessage("AddAudioDataStatic Failed"); alSetError(AL_INVALID_OPERATION); } mAppOwnsBufferMemory = true; return noErr; }
FieldDefPtr RecordIterator::getColDefs(){ if(m_pQueryResult->hasError()){ return DrillClientQueryResult::s_emptyColDefs; } //NOTE: if query is cancelled, return whatever you have. Client applications job to deal with it. if(this->m_pColDefs==NULL || this->hasSchemaChanged()){ if(this->m_pCurrentRecordBatch==NULL){ this->m_pQueryResult->waitForData(); if(m_pQueryResult->hasError()){ return DrillClientQueryResult::s_emptyColDefs; } } if(this->hasSchemaChanged()){ if(m_pColDefs!=NULL){ for(std::vector<Drill::FieldMetadata*>::iterator it=m_pColDefs->begin(); it!=m_pColDefs->end(); ++it){ delete *it; } m_pColDefs->clear(); //delete m_pColDefs; m_pColDefs=NULL; } } FieldDefPtr pColDefs( new std::vector<Drill::FieldMetadata*>); { //lock after we come out of the wait. boost::lock_guard<boost::mutex> bufferLock(this->m_recordBatchMutex); boost::shared_ptr< std::vector<Drill::FieldMetadata*> > currentColDefs=DrillClientQueryResult::s_emptyColDefs; if(this->m_pCurrentRecordBatch!=NULL){ currentColDefs=this->m_pCurrentRecordBatch->getColumnDefs(); }else{ // This is reached only when the first results have been received but // the getNext call has not been made to retrieve the record batch RecordBatch* pR=this->m_pQueryResult->peekNext(); if(pR!=NULL){ currentColDefs=pR->getColumnDefs(); } } for(std::vector<Drill::FieldMetadata*>::iterator it=currentColDefs->begin(); it!=currentColDefs->end(); ++it){ Drill::FieldMetadata* fmd= new Drill::FieldMetadata; fmd->copy(*(*it));//Yup, that's 2 stars pColDefs->push_back(fmd); } } this->m_pColDefs = pColDefs; } return this->m_pColDefs; }
void* GzippedFileCharacterSource::readAheadThreadMethod(void) { /* Enable immediate cancellation: */ Thread::setCancelState(Thread::CANCEL_ENABLE); Thread::setCancelType(Thread::CANCEL_ASYNCHRONOUS); unsigned int nextBufferHalf=0; while(true) { /* Read into the next buffer half: */ int readSize=gzread(inputFile,buffer+halfBufferSize*nextBufferHalf,halfBufferSize); if(readSize<0) { /* Signal an error and bail out: */ encounteredReadError=nextBufferHalf; break; } dataSizes[nextBufferHalf]=readSize; /* Go to the next buffer half: */ nextBufferHalf=1-nextBufferHalf; { Mutex::Lock bufferLock(bufferMutex); /* Mark the buffer as filled: */ ++numFilledBuffers; if(numFilledBuffers==1) { /* Wake up a potentially waiting reader: */ bufferCond.signal(); } /* Block until there is room in the buffer: */ while(numFilledBuffers==2) bufferCond.wait(bufferMutex); } } return 0; }
void GzippedFileCharacterSource::fillBuffer(void) { /* Check for an error signal from the readahead thread: */ if(encounteredReadError==nextReadBuffer) throw ReadError(); { Mutex::Lock bufferLock(bufferMutex); /* Release the previous buffer if there was one: */ if(haveReadBuffers) { --numFilledBuffers; if(numFilledBuffers==1) { /* Wake up a potentially waiting readahead thread: */ bufferCond.signal(); } } /* Block until there is data in the buffer: */ while(numFilledBuffers==0) bufferCond.wait(bufferMutex); } /* Set up to read from the next buffer half: */ rPtr=buffer+halfBufferSize*nextReadBuffer; bufferEnd=rPtr+dataSizes[nextReadBuffer]; /* Check if the end of file has been reached: */ if(dataSizes[nextReadBuffer]!=halfBufferSize) eofPtr=bufferEnd; /* Go to the next buffer half: */ nextReadBuffer=1-nextReadBuffer; haveReadBuffers=true; }
bool RenderTarget::copyToBuffer( GLenum mode, dp::sg::core::Image::PixelFormat pixelFormat, dp::sg::core::Image::PixelDataType pixelDataType, const dp::sg::core::BufferSharedPtr & buffer ) { // FIXME use C++ object for current/noncurrent for exception safety makeCurrent(); size_t components = 0; size_t bytesPerComponent = 0; // set up alignments glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); glPixelStorei(GL_PACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ROW_LENGTH, 0); glPixelStorei(GL_PACK_SKIP_ROWS, 0); glPixelStorei(GL_PACK_SKIP_PIXELS, 0); // determine OpenGL format GLenum format = ~0; switch (pixelFormat) { case dp::PixelFormat::PF_RGB: format = GL_RGB; components = 3; break; case dp::PixelFormat::PF_RGBA: format = GL_RGBA; components = 4; break; case dp::PixelFormat::PF_BGR: format = GL_BGR; components = 3; break; case dp::PixelFormat::PF_BGRA: format = GL_BGRA; components = 4; break; case dp::PixelFormat::PF_LUMINANCE: format = GL_LUMINANCE; components = 1; break; case dp::PixelFormat::PF_ALPHA: format = GL_ALPHA; components = 1; break; case dp::PixelFormat::PF_LUMINANCE_ALPHA: format = GL_LUMINANCE_ALPHA; components = 2; break; case dp::PixelFormat::PF_DEPTH_COMPONENT: format = GL_DEPTH_COMPONENT; components = 1; break; case dp::PixelFormat::PF_DEPTH_STENCIL: format = GL_DEPTH24_STENCIL8; components = 1; break; default: DP_ASSERT(0 && "unsupported PixelFormat"); }; GLenum dataType = ~0; switch (pixelDataType) { case dp::PixelFormat::PF_BYTE: dataType = GL_BYTE; bytesPerComponent = 1; break; case dp::PixelFormat::PF_UNSIGNED_BYTE: dataType = GL_UNSIGNED_BYTE; bytesPerComponent = 1; break; case dp::PixelFormat::PF_SHORT: dataType = GL_SHORT; bytesPerComponent = 2; break; case dp::PixelFormat::PF_UNSIGNED_SHORT: dataType = GL_UNSIGNED_SHORT; bytesPerComponent = 2; break; case dp::PixelFormat::PF_INT: dataType = GL_INT; bytesPerComponent = 4; break; case dp::PixelFormat::PF_UNSIGNED_INT: dataType = GL_UNSIGNED_INT; bytesPerComponent = 4; break; case dp::PixelFormat::PF_FLOAT32: dataType = GL_FLOAT; bytesPerComponent = 4; break; case dp::PixelFormat::PF_FLOAT16: dataType = GL_HALF_FLOAT; bytesPerComponent = 2; break; default: DP_ASSERT(0 && "unsupported PixelDataType"); } BufferLock(buffer)->setSize(m_width * m_height * components * bytesPerComponent); // read the pixels glWindowPos2i(0,0); glReadBuffer( mode ); bool isBufferGL = buffer.isPtrTo<BufferGL>(); if ( isBufferGL ) { GLint oldPBO; glGetIntegerv(GL_PIXEL_PACK_BUFFER_BINDING, &oldPBO); // FIXME it's necessary to check wheter the buffer object shared data with the current context... BufferGLLock bufferGL( sharedPtr_cast<BufferGL>( buffer ) ); bufferGL->bind( GL_PIXEL_PACK_BUFFER ); glReadPixels(0, 0, m_width, m_height, format, dataType, 0); glBindBuffer(GL_PIXEL_PACK_BUFFER, (GLuint)oldPBO); } else { Buffer::DataWriteLock bufferLock(buffer, Buffer::MAP_WRITE); glReadPixels(0, 0, m_width, m_height, format, dataType, bufferLock.getPtr()); } makeNoncurrent(); return true; }
void ForEachBaselineAction::ReaderFunction::operator()() { Stopwatch watch(true); bool finished = false; size_t threadCount = _action.mathThreadCount(); size_t minRecommendedBufferSize, maxRecommendedBufferSize; MSImageSet *msImageSet = dynamic_cast<MSImageSet*>(_action._artifacts->ImageSet()); if(msImageSet != 0) { minRecommendedBufferSize = msImageSet->Reader()->GetMinRecommendedBufferSize(threadCount); maxRecommendedBufferSize = msImageSet->Reader()->GetMaxRecommendedBufferSize(threadCount) - _action.GetBaselinesInBufferCount(); } else { minRecommendedBufferSize = 1; maxRecommendedBufferSize = 2; } do { watch.Pause(); _action.WaitForBufferAvailable(minRecommendedBufferSize); size_t wantedCount = maxRecommendedBufferSize - _action.GetBaselinesInBufferCount(); size_t requestedCount = 0; boost::mutex::scoped_lock lock(_action._artifacts->IOMutex()); watch.Start(); for(size_t i=0;i<wantedCount;++i) { ImageSetIndex *index = _action.GetNextIndex(); if(index != 0) { _action._artifacts->ImageSet()->AddReadRequest(*index); ++requestedCount; delete index; } else { finished = true; break; } } if(requestedCount > 0) { _action._artifacts->ImageSet()->PerformReadRequests(); watch.Pause(); for(size_t i=0;i<requestedCount;++i) { BaselineData *baseline = _action._artifacts->ImageSet()->GetNextRequested(); boost::mutex::scoped_lock bufferLock(_action._mutex); _action._baselineBuffer.push(baseline); bufferLock.unlock(); } } lock.unlock(); _action._dataAvailable.notify_all(); watch.Start(); } while(!finished); _action.SetFinishedBaselines(); _action._dataAvailable.notify_all(); watch.Pause(); AOLogger::Debug << "Time spent on reading: " << watch.ToString() << '\n'; }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OSStatus OALBuffer::AddAudioData(char* inAudioData, UInt32 inAudioDataSize, ALenum format, ALsizei freq, bool inPreConvertToHalFormat) { #if LOG_VERBOSE DebugMessageN6("OALBuffer::AddAudioData() - OALBuffer:inAudioData:inAudioDataSize:format:freq:inPreConvertToHalFormat = %ld:%p:%ld:%d:%d:%d", (long int) mSelfToken, inAudioData, (long int) inAudioDataSize, format, freq, inPreConvertToHalFormat); #endif // creates memory if needed // reallocs if needed // returns an error if buffer is in use CAGuard::Locker bufferLock(mBufferLock); try { if (!IsFormatSupported(format)) throw ((OSStatus) AL_INVALID_VALUE); // this is not a valid buffer token or is an invalid format #if USE_SOURCE_LIST_MUTEX bool wasLocked = mSourceListGuard.Lock(); #endif // don't allow if the buffer is in a queue UInt32 attachedCount = mAttachedSourceList->Size(); #if USE_SOURCE_LIST_MUTEX if (wasLocked) mSourceListGuard.Unlock(); #endif if (attachedCount > 0) { DebugMessage("WAITING: AddAudioData ---> WaitOneRenderCycle"); // Let a render cycle go by and try again WaitOneRenderCycle(); #if USE_SOURCE_LIST_MUTEX wasLocked = mSourceListGuard.Lock(); #endif attachedCount = mAttachedSourceList->Size(); #if USE_SOURCE_LIST_MUTEX if (wasLocked) mSourceListGuard.Unlock(); #endif if (attachedCount > 0){ DebugMessageN2("OALBuffer::AddAudioData: buffer ATTACHMENT > 0 - mSelfToken:mAttachedSourceList->Size() = %ld:%ld", (long int) mSelfToken, (long int) mAttachedSourceList->Size()); throw ((OSStatus) AL_INVALID_OPERATION); } } if (mAppOwnsBufferMemory) { mData = NULL; // we were using the apps memory before so just initialize mData incase we fail mAppOwnsBufferMemory = false; } mPreConvertedDataSize = (UInt32) inAudioDataSize; // do not pre-convert stereo sounds, let the AC do the deinterleaving OSStatus result = noErr; if (!inPreConvertToHalFormat || ((format == AL_FORMAT_STEREO16) || (format == AL_FORMAT_STEREO8))) { if (mData != NULL) { if (mDataSize != (UInt32) inAudioDataSize) { mDataSize = (UInt32) inAudioDataSize; void *newDataPtr = realloc(mData, mDataSize); mData = (UInt8 *) newDataPtr; } } else { mDataSize = (UInt32) inAudioDataSize; mData = (UInt8 *) malloc (mDataSize); } if (mData) { result = FillInASBD(mDataFormat, format, freq); THROW_RESULT mPreConvertedDataFormat.SetFrom(mDataFormat); // make sure they are the same so original format info can be returned to caller memcpy (mData, inAudioData, mDataSize); } } else { #if LOG_EXTRAS DebugMessage("alBufferData called: Converting Data Now"); #endif result = ConvertDataForBuffer(inAudioData, inAudioDataSize, format, freq); // convert the data to the mixer's format and copy to the buffer THROW_RESULT } } catch (OSStatus result) { DebugMessageN1("OALBuffer::AddAudioData Failed - err = %ld\n", (long int) result); alSetError(result); throw result; } catch (...) { DebugMessage("OALBuffer::AddAudioData Failed"); alSetError(AL_INVALID_OPERATION); throw -1; } return noErr; }
void ForEachBaselineAction::ReaderFunction::operator()() { Stopwatch watch(true); bool finished = false; size_t threadCount = _action.mathThreadCount(); size_t minRecommendedBufferSize, maxRecommendedBufferSize; MSImageSet* msImageSet = dynamic_cast<MSImageSet*>(&_action._artifacts->ImageSet()); if(msImageSet != nullptr) { minRecommendedBufferSize = msImageSet->Reader()->GetMinRecommendedBufferSize(threadCount); maxRecommendedBufferSize = msImageSet->Reader()->GetMaxRecommendedBufferSize(threadCount) - _action.GetBaselinesInBufferCount(); } else { minRecommendedBufferSize = 1; maxRecommendedBufferSize = 2; } do { watch.Pause(); _action.WaitForBufferAvailable(minRecommendedBufferSize); size_t wantedCount = maxRecommendedBufferSize - _action.GetBaselinesInBufferCount(); size_t requestedCount = 0; std::unique_lock<std::mutex> lock(_action._artifacts->IOMutex()); watch.Start(); for(size_t i=0;i<wantedCount;++i) { std::unique_ptr<ImageSetIndex> index = _action.GetNextIndex(); if(index != nullptr) { _action._artifacts->ImageSet().AddReadRequest(*index); ++requestedCount; } else { finished = true; break; } } if(requestedCount > 0) { _action._artifacts->ImageSet().PerformReadRequests(); watch.Pause(); for(size_t i=0;i<requestedCount;++i) { std::unique_ptr<BaselineData> baseline = _action._artifacts->ImageSet().GetNextRequested(); std::lock_guard<std::mutex> bufferLock(_action._mutex); _action._baselineBuffer.emplace(std::move(baseline)); } } lock.unlock(); _action._dataAvailable.notify_all(); watch.Start(); } while(!finished); _action.SetFinishedBaselines(); _action._dataAvailable.notify_all(); watch.Pause(); Logger::Debug << "Time spent on reading: " << watch.ToString() << '\n'; }