~ubuntu-branches/ubuntu/vivid/unrar-nonfree/vivid

« back to all changes in this revision

Viewing changes to threadpool.cpp

  • Committer: Package Import Robot
  • Author(s): Martin Meredith
  • Date: 2015-02-03 12:58:01 UTC
  • mfrom: (1.1.18) (5.1.18 sid)
  • Revision ID: package-import@ubuntu.com-20150203125801-od6ev8cqy1er51vz
Tags: 1:5.2.5-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
  if (MaxAllowedThreads==0)
16
16
    MaxAllowedThreads=1;
17
17
 
 
18
  ThreadsCreatedCount=0;
 
19
 
18
20
  // If we have more threads than queue size, we'll hang on pool destroying,
19
21
  // not releasing all waiting threads.
20
22
  if (MaxAllowedThreads>ASIZE(TaskQueue))
22
24
 
23
25
  Closing=false;
24
26
 
25
 
  bool Success;
 
27
  bool Success = CriticalSectionCreate(&CritSection);
26
28
#ifdef _WIN_ALL
27
29
  QueuedTasksCnt=CreateSemaphore(NULL,0,ASIZE(TaskQueue),NULL);
28
30
  NoneActive=CreateEvent(NULL,TRUE,TRUE,NULL);
29
 
  InitializeCriticalSection(&CritSection); 
30
 
  Success=QueuedTasksCnt!=NULL && NoneActive!=NULL;
 
31
  Success=Success && QueuedTasksCnt!=NULL && NoneActive!=NULL;
31
32
#elif defined(_UNIX)
32
33
  AnyActive = false;
33
34
  QueuedTasksCnt = 0;
34
 
  Success=pthread_mutex_init(&CritSection,NULL)==0 &&
35
 
          pthread_cond_init(&AnyActiveCond,NULL)==0 &&
 
35
  Success=Success && pthread_cond_init(&AnyActiveCond,NULL)==0 &&
36
36
          pthread_mutex_init(&AnyActiveMutex,NULL)==0 &&
37
37
          pthread_cond_init(&QueuedTasksCntCond,NULL)==0 &&
38
38
          pthread_mutex_init(&QueuedTasksCntMutex,NULL)==0;
43
43
    ErrHandler.Exit(RARX_FATAL);
44
44
  }
45
45
 
46
 
  for(uint I=0;I<MaxAllowedThreads;I++)
47
 
  {
48
 
    ThreadHandles[I] = ThreadCreate(PoolThread, this);
49
 
#ifdef _WIN_ALL
50
 
    if (ThreadPool::ThreadPriority!=THREAD_PRIORITY_NORMAL)
51
 
      SetThreadPriority(ThreadHandles[I],ThreadPool::ThreadPriority);
52
 
#endif
53
 
  }
54
 
 
55
46
  QueueTop = 0;
56
47
  QueueBottom = 0;
57
48
  ActiveThreads = 0;
75
66
  pthread_cond_broadcast(&QueuedTasksCntCond);
76
67
#endif
77
68
 
78
 
  for(uint I=0;I<MaxAllowedThreads;I++)
 
69
  for(uint I=0;I<ThreadsCreatedCount;I++)
79
70
  {
80
71
#ifdef _WIN_ALL
81
72
    // Waiting until the thread terminates.
82
73
    CWaitForSingleObject(ThreadHandles[I]);
83
74
#endif
84
 
    // Close the thread handle. In Unix it is results in pthread_join call,
 
75
    // Close the thread handle. In Unix it results in pthread_join call,
85
76
    // which also waits for thread termination.
86
77
    ThreadClose(ThreadHandles[I]);
87
78
  }
88
79
 
 
80
  CriticalSectionDelete(&CritSection);
89
81
#ifdef _WIN_ALL
90
 
  DeleteCriticalSection(&CritSection);
91
82
  CloseHandle(QueuedTasksCnt);
92
83
  CloseHandle(NoneActive);
93
84
#elif defined(_UNIX)
94
 
  pthread_mutex_destroy(&CritSection);
95
85
  pthread_cond_destroy(&AnyActiveCond);
96
86
  pthread_mutex_destroy(&AnyActiveMutex);
97
87
  pthread_cond_destroy(&QueuedTasksCntCond);
100
90
}
101
91
 
102
92
 
 
93
void ThreadPool::CreateThreads()
 
94
{
 
95
  for(uint I=0;I<MaxAllowedThreads;I++)
 
96
  {
 
97
    ThreadHandles[I] = ThreadCreate(PoolThread, this);
 
98
    ThreadsCreatedCount++;
 
99
#ifdef _WIN_ALL
 
100
    if (ThreadPool::ThreadPriority!=THREAD_PRIORITY_NORMAL)
 
101
      SetThreadPriority(ThreadHandles[I],ThreadPool::ThreadPriority);
 
102
#endif
 
103
  }
 
104
}
 
105
 
 
106
 
103
107
NATIVE_THREAD_TYPE ThreadPool::PoolThread(void *Param)
104
108
{
105
109
  ((ThreadPool*)Param)->PoolThreadLoop();
159
163
 
160
164
// Add task to queue. We assume that it is always called from main thread,
161
165
// it allows to avoid any locks here. We process collected tasks only
162
 
// when StartWait is called.
 
166
// when WaitDone is called.
163
167
void ThreadPool::AddTask(PTHREAD_PROC Proc,void *Data)
164
168
{
 
169
  if (ThreadsCreatedCount == 0)
 
170
    CreateThreads();
 
171
  
165
172
  // If queue is full, wait until it is empty.
166
173
  if ((QueueTop + 1) % ASIZE(TaskQueue) == QueueBottom)
167
174
    WaitDone();