-
Notifications
You must be signed in to change notification settings - Fork 1
/
decodeengine.cpp
54 lines (47 loc) · 1.22 KB
/
decodeengine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <pthread.h>
#include "decodeengine.hpp"
#include "decoderjob.hpp"
#include "picture.hpp"
#include "mutexobj.hpp"
static void *job_runner( void *s_threadq )
{
Queue<ReadyThread> *threadq = (Queue<ReadyThread> *)s_threadq;
Queue<DecoderJob> opq( 0 );
ReadyThread me( pthread_self(), &opq );
while ( 1 ) {
threadq->enqueue( &me );
DecoderJob *job = opq.dequeue( true );
job->execute();
delete job;
}
return NULL;
}
void DecodeEngine::dispatch( DecoderJob *job )
{
ReadyThread *thread = threadq.dequeue( false );
if ( thread ) {
thread->opq->enqueue( job );
} else {
pthread_t new_thread;
{
MutexLock x( &mutex );
pthread_create( &new_thread, NULL, job_runner, &threadq );
thread_count++;
}
thread = threadq.dequeue( true );
ahabassert( thread );
thread->opq->enqueue( job );
}
}
DecodeEngine::~DecodeEngine()
{
DecoderJob *shutdown = new DecoderJobShutDown();
for ( int i = 0; i < thread_count; i++ ) {
ReadyThread *thread = threadq.dequeue( true );
pthread_t handle = thread->handle;
thread->opq->enqueue( shutdown );
unixassert( pthread_join( handle, NULL ) );
}
delete shutdown;
unixassert( pthread_mutex_destroy( &mutex ) );
}