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 | #include "../../shared/mutex.h" |
28 | #include "bsp.h" |
29 | |
30 | #define MAX_THREADS8 8 |
31 | |
32 | threadstate_t threadstate; |
33 | |
34 | |
35 | |
36 | |
37 | static int GetThreadWork (void) |
38 | { |
39 | int r; |
40 | int f; |
41 | |
42 | ThreadLock(); |
43 | |
44 | if (threadstate.workindex == threadstate.workcount) { |
45 | ThreadUnlock(); |
46 | return -1; |
47 | } |
48 | |
49 | |
50 | f = 10 * threadstate.workindex / threadstate.workcount; |
51 | if (f != threadstate.workfrac) { |
52 | threadstate.workfrac = f; |
53 | if (threadstate.progress) { |
54 | fprintf(stdout__stdoutp, "%i...", f); |
55 | fflush(stdout__stdoutp); |
56 | } |
57 | } else if (threadstate.progress && threadstate.workindex % threadstate.worktick == 0) { |
58 | fprintf(stdout__stdoutp, "%c\b", "-\\|/"[(threadstate.workindex / threadstate.worktick) & 3]); |
59 | fflush(stdout__stdoutp); |
60 | } |
61 | |
62 | |
63 | r = threadstate.workindex; |
64 | threadstate.workindex++; |
65 | |
66 | ThreadUnlock(); |
67 | |
68 | return r; |
69 | } |
70 | |
71 | |
72 | |
73 | static void (*WorkFunction) (unsigned int); |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | static int ThreadWork (void *p) |
81 | { |
82 | while (true) { |
83 | int work = GetThreadWork(); |
84 | if (work == -1) |
85 | break; |
86 | WorkFunction(work); |
87 | } |
88 | |
89 | return 0; |
90 | } |
91 | |
92 | |
93 | static threads_mutex_t *lock = NULL__null; |
94 | |
95 | static void ThreadInit (void) |
96 | { |
97 | if (lock != NULL__null) |
98 | Sys_Error("Mutex already created!"); |
99 | |
100 | lock = TH_MutexCreate("ufo2map"); |
101 | } |
102 | |
103 | static void ThreadRelease (void) |
104 | { |
105 | TH_MutexDestroy(lock); |
106 | lock = NULL__null; |
107 | } |
108 | |
109 | |
110 | |
111 | |
112 | void ThreadLock (void) |
113 | { |
114 | if (threadstate.numthreads == 1) { |
115 | |
116 | } else if (lock != NULL__null && TH_MutexLock(lock)_TH_MutexLock((lock), "src/tools/ufo2map/threads.cpp", 116) != -1) { |
117 | |
118 | } else { |
119 | Sys_Error("Couldn't lock mutex (%p)!", (void*)lock); |
120 | } |
121 | } |
122 | |
123 | |
124 | |
125 | |
126 | void ThreadUnlock (void) |
127 | { |
128 | if (threadstate.numthreads == 1) { |
129 | |
130 | } else if (lock != NULL__null && TH_MutexUnlock(lock)_TH_MutexUnlock((lock), "src/tools/ufo2map/threads.cpp", 130) != -1) { |
131 | |
132 | } else { |
133 | Sys_Error("Couldn't unlock mutex (%p)!", (void*)lock); |
134 | } |
135 | } |
136 | |
137 | static void RunThreads (void) |
138 | { |
139 | SDL_Thread *threads[MAX_THREADS8]; |
140 | int i; |
141 | |
142 | if (threadstate.numthreads == 1) { |
| |
143 | ThreadWork(NULL__null); |
144 | return; |
145 | } |
146 | |
147 | ThreadInit(); |
148 | |
149 | for (i = 0; i < threadstate.numthreads; i++) |
| 6 | Loop condition is true. Entering loop body |
|
| 7 | Loop condition is false. Execution continues on line 152 |
|
150 | threads[i] = SDL_CreateThread(ThreadWork, NULL__null); |
151 | |
152 | for (i = 0; i < threadstate.numthreads; i++) |
| 8 | Loop condition is true. Entering loop body |
|
| 9 | Loop condition is true. Entering loop body |
|
153 | SDL_WaitThread(threads[i], NULL__null); |
| 10 | Function call argument is an uninitialized value |
|
154 | |
155 | ThreadRelease(); |
156 | } |
157 | |
158 | |
159 | |
160 | |
161 | |
162 | void RunThreadsOn (void (*func)(unsigned int), int unsigned workcount, bool progress, const char *id) |
163 | { |
164 | int start, end; |
165 | |
166 | if (threadstate.numthreads < 1) |
| |
167 | threadstate.numthreads = 1; |
168 | |
169 | if (threadstate.numthreads > MAX_THREADS8) |
| |
170 | threadstate.numthreads = MAX_THREADS8; |
171 | |
172 | threadstate.workindex = 0; |
173 | threadstate.workcount = workcount; |
174 | threadstate.workfrac = -1; |
175 | threadstate.progress = progress; |
176 | threadstate.worktick = sqrt(workcount) + 1; |
177 | |
178 | WorkFunction = func; |
179 | |
180 | start = time(NULL__null); |
181 | |
182 | if (threadstate.progress) { |
| |
183 | fprintf(stdout__stdoutp, "%10s: ", id); |
184 | fflush(stdout__stdoutp); |
185 | } |
186 | |
187 | RunThreads(); |
| |
188 | |
189 | end = time(NULL__null); |
190 | |
191 | if (threadstate.progress) { |
192 | Verb_Printf(VERB_NORMAL, " (time: %6is, #: %i)\n", end - start, workcount); |
193 | } |
194 | } |
195 | |
196 | |
197 | |
198 | |
199 | void RunSingleThreadOn (void (*func)(unsigned int), int unsigned workcount, bool progress, const char *id) |
200 | { |
201 | int saved_numthreads = threadstate.numthreads; |
202 | |
203 | threadstate.numthreads = 1; |
204 | |
205 | RunThreadsOn(func, workcount, progress, id); |
206 | |
207 | threadstate.numthreads = saved_numthreads; |
208 | } |