void MtSync_Destruct(CMtSync *p) { if (Thread_WasCreated(&p->thread)) { MtSync_StopWriting(p); p->exit = True; if (p->needStart) Event_Set(&p->canStart); Thread_Wait(&p->thread); Thread_Close(&p->thread); } if (p->csWasInitialized) { CriticalSection_Delete(&p->cs); p->csWasInitialized = False; } Event_Close(&p->canStart); Event_Close(&p->wasStarted); Event_Close(&p->wasStopped); Semaphore_Close(&p->freeSemaphore); Semaphore_Close(&p->filledSemaphore); p->wasCreated = False; }
void MtSync_StopWriting(CMtSync *p) { UInt32 myNumBlocks = p->numProcessedBlocks; if (!Thread_WasCreated(&p->thread) || p->needStart) return; EnterGlobalLock(); TraceObjectSync("MtSync_StopWriting:stop", p); p->stopWriting = True; TraceObjectSync("MtSync_StopWriting:stop", p); LeaveGlobalLock(); if (p->csWasEntered) { CriticalSection_Leave(&p->cs); p->csWasEntered = False; } Semaphore_Release1(&p->freeSemaphore); Event_Wait(&p->wasStopped); while (myNumBlocks++ != p->numProcessedBlocks) { Semaphore_Wait(&p->filledSemaphore); Semaphore_Release1(&p->freeSemaphore); } p->needStart = True; }
SRes MtCoder_Code(CMtCoder *p) { unsigned i, numThreads = p->numThreads; SRes res = SZ_OK; p->res = SZ_OK; MtProgress_Init(&p->mtProgress, p->progress); for (i = 0; i < numThreads; i++) { RINOK(CMtThread_Prepare(&p->threads[i])); } for (i = 0; i < numThreads; i++) { CMtThread *t = &p->threads[i]; CLoopThread *lt = &t->thread; if (!Thread_WasCreated(<->thread)) { lt->func = ThreadFunc; lt->param = t; if (LoopThread_Create(lt) != SZ_OK) { res = SZ_ERROR_THREAD; break; } } } if (res == SZ_OK) { unsigned j; for (i = 0; i < numThreads; i++) { CMtThread *t = &p->threads[i]; if (LoopThread_StartSubThread(&t->thread) != SZ_OK) { res = SZ_ERROR_THREAD; p->threads[0].stopReading = True; break; } } Event_Set(&p->threads[0].canWrite); Event_Set(&p->threads[0].canRead); for (j = 0; j < i; j++) LoopThread_WaitSubThread(&p->threads[j].thread); } for (i = 0; i < numThreads; i++) CMtThread_CloseEvents(&p->threads[i]); return (res == SZ_OK) ? p->res : res; }
static void CMtThread_Destruct(CMtThread *p) { CMtThread_CloseEvents(p); if (Thread_WasCreated(&p->thread.thread)) { LoopThread_StopAndWait(&p->thread); LoopThread_Close(&p->thread); } if (p->mtCoder->alloc) IAlloc_Free(p->mtCoder->alloc, p->outBuf); p->outBuf = 0; if (p->mtCoder->alloc) IAlloc_Free(p->mtCoder->alloc, p->inBuf); p->inBuf = 0; }