comparison data/fileio/CodedAudioFileReader.cpp @ 543:7a66b94ef1c0

* Thread safety (fixing a crash)
author Chris Cannam
date Wed, 04 Feb 2009 10:53:38 +0000
parents 59dd6d1bcfb0
children 65d955c4d671
comparison
equal deleted inserted replaced
542:1ddab154fb9a 543:7a66b94ef1c0
331 //!!! check for return value! out of disk space, etc! 331 //!!! check for return value! out of disk space, etc!
332 sf_writef_float(m_cacheFileWritePtr, buffer, sz); 332 sf_writef_float(m_cacheFileWritePtr, buffer, sz);
333 break; 333 break;
334 334
335 case CacheInMemory: 335 case CacheInMemory:
336 m_dataLock.lockForWrite();
336 for (size_t s = 0; s < count; ++s) { 337 for (size_t s = 0; s < count; ++s) {
337 m_data.push_back(buffer[count]); 338 m_data.push_back(buffer[s]);
338 } 339 }
339 MUNLOCK_SAMPLEBLOCK(m_data); 340 MUNLOCK_SAMPLEBLOCK(m_data);
341 m_dataLock.unlock();
340 break; 342 break;
341 } 343 }
342 } 344 }
343 345
344 void 346 void
345 CodedAudioFileReader::getInterleavedFrames(size_t start, size_t count, 347 CodedAudioFileReader::getInterleavedFrames(size_t start, size_t count,
346 SampleBlock &frames) const 348 SampleBlock &frames) const
347 { 349 {
348 //!!! we want to ensure this doesn't require a lock -- at the 350 // Lock is only required in CacheInMemory mode (the cache file
349 // moment it does need one, but it doesn't have one... 351 // reader is expected to be thread safe and manage its own
352 // locking)
350 353
351 if (!m_initialised) { 354 if (!m_initialised) {
352 std::cerr << "CodedAudioFileReader::getInterleavedFrames: not initialised" << std::endl; 355 std::cerr << "CodedAudioFileReader::getInterleavedFrames: not initialised" << std::endl;
353 return; 356 return;
354 } 357 }
364 case CacheInMemory: 367 case CacheInMemory:
365 { 368 {
366 frames.clear(); 369 frames.clear();
367 if (!isOK()) return; 370 if (!isOK()) return;
368 if (count == 0) return; 371 if (count == 0) return;
369 372 frames.reserve(count * m_channelCount);
370 // slownessabounds 373
371 374 size_t idx = start * m_channelCount;
372 for (size_t i = start; i < start + count; ++i) { 375 size_t i = 0;
373 for (size_t ch = 0; ch < m_channelCount; ++ch) { 376
374 size_t index = i * m_channelCount + ch; 377 m_dataLock.lockForRead();
375 if (index >= m_data.size()) return; 378 while (i < count * m_channelCount && idx < m_data.size()) {
376 frames.push_back(m_data[index]); 379 frames.push_back(m_data[idx]);
377 } 380 ++idx;
378 } 381 }
379 } 382 m_dataLock.unlock();
380 } 383 }
381 } 384 }
382 385 }
386