Skip to content

Commit

Permalink
Merge pull request #29 from Isetta-Han/master
Browse files Browse the repository at this point in the history
fix: CCaptureStream destruct, show stop Threads, to prevent Threads c…
  • Loading branch information
schellingb authored Nov 27, 2022
2 parents fe461e8 + 2eabf0f commit 7a3278d
Showing 1 changed file with 78 additions and 3 deletions.
81 changes: 78 additions & 3 deletions Source/UnityCaptureFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ class CCaptureStream : CSourceStream, IKsPropertySet, IAMStreamConfig, IAMStream
{
WorkersRunning = 0;
for (size_t i = 0; i != WORKERCOUNT; i++) NewJobSemaphore.Post(); //wake up all threads
for (size_t i = 0; i != WORKERCOUNT; i++)
{
Threads[i].Stop();
}
}

void StartNewJob(ProcessJob NewJob)
Expand All @@ -423,9 +427,80 @@ class CCaptureStream : CSourceStream, IKsPropertySet, IAMStreamConfig, IAMStream

private:
//Wrapper objects for Windows concurrency objects (thread, mutex, semaphore)
struct sThread { typedef DWORD (WINAPI *FUNC_t)(LPVOID); sThread() : h(0) {} sThread(FUNC_t f, void* p = NULL) : h(0) { Start(f, p); } void Start(FUNC_t f, void* p = NULL) { if (h) this->~sThread(); h = CreateThread(0,0,f,p,0,0); } ~sThread() { if (h) { WaitForSingleObject(h, INFINITE); CloseHandle(h); } } private:HANDLE h;sThread(const sThread&);sThread& operator=(const sThread&);};
struct sMutex { sMutex() : h(CreateMutexA(0,0,0)) {} ~sMutex() { CloseHandle(h); } __inline void Lock() { WaitForSingleObject(h,INFINITE); } __inline void Unlock() { ReleaseMutex(h); } private:HANDLE h;sMutex(const sMutex&);sMutex& operator=(const sMutex&);};
struct sSemaphore { sSemaphore() : h(CreateSemaphoreA(0,0,32768,0)) {} ~sSemaphore() { CloseHandle(h); } __inline void Post() { ReleaseSemaphore(h, 1, 0); } __inline bool WaitForPost() { return WaitForSingleObject(h,INFINITE) == WAIT_OBJECT_0; } private:HANDLE h;sSemaphore(const sSemaphore&);sSemaphore& operator=(const sSemaphore&);};
struct sThread
{
typedef DWORD(WINAPI* FUNC_t)(LPVOID);
sThread() : h(0)
{
}
sThread(FUNC_t f, void* p = NULL) : h(0)
{
Start(f, p);
}
void Start(FUNC_t f, void* p = NULL)
{
if (h)
this->~sThread();
h = CreateThread(0, 0, f, p, 0, 0);
}
void Stop()
{
if (h)
{
WaitForSingleObject(h, INFINITE);
CloseHandle(h);
h = 0;
}
}
~sThread()
{
if (h)
{
WaitForSingleObject(h, INFINITE);
CloseHandle(h);
}
}
private:
HANDLE h;
sThread(const sThread&);
sThread& operator=(const sThread&);
};

struct sMutex
{
sMutex() : h(CreateMutexA(0, 0, 0)) {}
~sMutex() { CloseHandle(h); }
__inline void Lock()
{
WaitForSingleObject(h, INFINITE);
}
__inline void Unlock()
{
ReleaseMutex(h);
}
private:
HANDLE h;
sMutex(const sMutex&);
sMutex& operator=(const sMutex&);
};

struct sSemaphore
{
sSemaphore() : h(CreateSemaphoreA(0, 0, 32768, 0)) {}
~sSemaphore() { CloseHandle(h); }
__inline void Post()
{
ReleaseSemaphore(h, 1, 0);
}
__inline bool WaitForPost()
{
return WaitForSingleObject(h, INFINITE) == WAIT_OBJECT_0;
}
private:
HANDLE h;
sSemaphore(const sSemaphore&);
sSemaphore& operator=(const sSemaphore&);
};

enum { WORKERCOUNT = 3 };
sMutex JobsMutex;
Expand Down

0 comments on commit 7a3278d

Please sign in to comment.