vcomp: Use RtlIsCriticalSectionLockedByThread to check lock owner.
[wine/multimedia.git] / dlls / vcomp / main.c
blob12fd2a531907ec36bfcdf222dda4efff58fbea98
1 /*
3 * vcomp implementation
5 * Copyright 2011 Austin English
6 * Copyright 2012 Dan Kegel
7 * Copyright 2015 Sebastian Lackner
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
28 #include <assert.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winternl.h"
33 #include "wine/debug.h"
34 #include "wine/list.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(vcomp);
38 typedef CRITICAL_SECTION *omp_lock_t;
39 typedef CRITICAL_SECTION *omp_nest_lock_t;
41 static struct list vcomp_idle_threads = LIST_INIT(vcomp_idle_threads);
42 static DWORD vcomp_context_tls = TLS_OUT_OF_INDEXES;
43 static HMODULE vcomp_module;
44 static int vcomp_max_threads;
45 static int vcomp_num_threads;
46 static BOOL vcomp_nested_fork = FALSE;
48 static RTL_CRITICAL_SECTION vcomp_section;
49 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
51 0, 0, &vcomp_section,
52 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
53 0, 0, { (DWORD_PTR)(__FILE__ ": vcomp_section") }
55 static RTL_CRITICAL_SECTION vcomp_section = { &critsect_debug, -1, 0, 0, 0, 0 };
57 #define VCOMP_DYNAMIC_FLAGS_STATIC 0x01
58 #define VCOMP_DYNAMIC_FLAGS_CHUNKED 0x02
59 #define VCOMP_DYNAMIC_FLAGS_GUIDED 0x03
60 #define VCOMP_DYNAMIC_FLAGS_INCREMENT 0x40
62 struct vcomp_thread_data
64 struct vcomp_team_data *team;
65 struct vcomp_task_data *task;
66 int thread_num;
67 BOOL parallel;
68 int fork_threads;
70 /* only used for concurrent tasks */
71 struct list entry;
72 CONDITION_VARIABLE cond;
74 /* single */
75 unsigned int single;
77 /* section */
78 unsigned int section;
80 /* dynamic */
81 unsigned int dynamic;
82 unsigned int dynamic_type;
83 unsigned int dynamic_begin;
84 unsigned int dynamic_end;
87 struct vcomp_team_data
89 CONDITION_VARIABLE cond;
90 int num_threads;
91 int finished_threads;
93 /* callback arguments */
94 int nargs;
95 void *wrapper;
96 __ms_va_list valist;
98 /* barrier */
99 unsigned int barrier;
100 int barrier_count;
103 struct vcomp_task_data
105 /* single */
106 unsigned int single;
108 /* section */
109 unsigned int section;
110 int num_sections;
111 int section_index;
113 /* dynamic */
114 unsigned int dynamic;
115 unsigned int dynamic_first;
116 unsigned int dynamic_last;
117 unsigned int dynamic_iterations;
118 int dynamic_step;
119 unsigned int dynamic_chunksize;
122 #if defined(__i386__)
124 extern void CDECL _vcomp_fork_call_wrapper(void *wrapper, int nargs, __ms_va_list args);
125 __ASM_GLOBAL_FUNC( _vcomp_fork_call_wrapper,
126 "pushl %ebp\n\t"
127 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
128 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
129 "movl %esp,%ebp\n\t"
130 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
131 "pushl %esi\n\t"
132 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
133 "pushl %edi\n\t"
134 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
135 "movl 12(%ebp),%edx\n\t"
136 "movl %esp,%edi\n\t"
137 "shll $2,%edx\n\t"
138 "jz 1f\n\t"
139 "subl %edx,%edi\n\t"
140 "andl $~15,%edi\n\t"
141 "movl %edi,%esp\n\t"
142 "movl 12(%ebp),%ecx\n\t"
143 "movl 16(%ebp),%esi\n\t"
144 "cld\n\t"
145 "rep; movsl\n"
146 "1:\tcall *8(%ebp)\n\t"
147 "leal -8(%ebp),%esp\n\t"
148 "popl %edi\n\t"
149 __ASM_CFI(".cfi_same_value %edi\n\t")
150 "popl %esi\n\t"
151 __ASM_CFI(".cfi_same_value %esi\n\t")
152 "popl %ebp\n\t"
153 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
154 __ASM_CFI(".cfi_same_value %ebp\n\t")
155 "ret" )
157 #elif defined(__x86_64__)
159 extern void CDECL _vcomp_fork_call_wrapper(void *wrapper, int nargs, __ms_va_list args);
160 __ASM_GLOBAL_FUNC( _vcomp_fork_call_wrapper,
161 "pushq %rbp\n\t"
162 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
163 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
164 "movq %rsp,%rbp\n\t"
165 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
166 "pushq %rsi\n\t"
167 __ASM_CFI(".cfi_rel_offset %rsi,-8\n\t")
168 "pushq %rdi\n\t"
169 __ASM_CFI(".cfi_rel_offset %rdi,-16\n\t")
170 "movq %rcx,%rax\n\t"
171 "movq $4,%rcx\n\t"
172 "cmp %rcx,%rdx\n\t"
173 "cmovgq %rdx,%rcx\n\t"
174 "leaq 0(,%rcx,8),%rdx\n\t"
175 "subq %rdx,%rsp\n\t"
176 "andq $~15,%rsp\n\t"
177 "movq %rsp,%rdi\n\t"
178 "movq %r8,%rsi\n\t"
179 "rep; movsq\n\t"
180 "movq 0(%rsp),%rcx\n\t"
181 "movq 8(%rsp),%rdx\n\t"
182 "movq 16(%rsp),%r8\n\t"
183 "movq 24(%rsp),%r9\n\t"
184 "callq *%rax\n\t"
185 "leaq -16(%rbp),%rsp\n\t"
186 "popq %rdi\n\t"
187 __ASM_CFI(".cfi_same_value %rdi\n\t")
188 "popq %rsi\n\t"
189 __ASM_CFI(".cfi_same_value %rsi\n\t")
190 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
191 "popq %rbp\n\t"
192 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
193 __ASM_CFI(".cfi_same_value %rbp\n\t")
194 "ret")
196 #elif defined(__arm__)
198 extern void CDECL _vcomp_fork_call_wrapper(void *wrapper, int nargs, __ms_va_list args);
199 __ASM_GLOBAL_FUNC( _vcomp_fork_call_wrapper,
200 ".arm\n\t"
201 "push {r4, r5, LR}\n\t"
202 "mov r4, r0\n\t"
203 "mov r5, SP\n\t"
204 "lsl r3, r1, #2\n\t"
205 "cmp r3, #0\n\t"
206 "beq 5f\n\t"
207 "sub SP, SP, r3\n\t"
208 "tst r1, #1\n\t"
209 "subeq SP, SP, #4\n\t"
210 "1:\tsub r3, r3, #4\n\t"
211 "ldr r0, [r2, r3]\n\t"
212 "str r0, [SP, r3]\n\t"
213 "cmp r3, #0\n\t"
214 "bgt 1b\n\t"
215 "cmp r1, #1\n\t"
216 "bgt 2f\n\t"
217 "pop {r0}\n\t"
218 "b 5f\n\t"
219 "2:\tcmp r1, #2\n\t"
220 "bgt 3f\n\t"
221 "pop {r0-r1}\n\t"
222 "b 5f\n\t"
223 "3:\tcmp r1, #3\n\t"
224 "bgt 4f\n\t"
225 "pop {r0-r2}\n\t"
226 "b 5f\n\t"
227 "4:\tpop {r0-r3}\n\t"
228 "5:\tblx r4\n\t"
229 "mov SP, r5\n\t"
230 "pop {r4, r5, PC}" )
232 #else
234 static void CDECL _vcomp_fork_call_wrapper(void *wrapper, int nargs, __ms_va_list args)
236 ERR("Not implemented for this architecture\n");
239 #endif
241 static inline struct vcomp_thread_data *vcomp_get_thread_data(void)
243 return (struct vcomp_thread_data *)TlsGetValue(vcomp_context_tls);
246 static inline void vcomp_set_thread_data(struct vcomp_thread_data *thread_data)
248 TlsSetValue(vcomp_context_tls, thread_data);
251 static struct vcomp_thread_data *vcomp_init_thread_data(void)
253 struct vcomp_thread_data *thread_data = vcomp_get_thread_data();
254 struct
256 struct vcomp_thread_data thread;
257 struct vcomp_task_data task;
258 } *data;
260 if (thread_data) return thread_data;
261 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data))))
263 ERR("could not create thread data\n");
264 ExitProcess(1);
267 data->task.single = 0;
268 data->task.section = 0;
269 data->task.dynamic = 0;
271 thread_data = &data->thread;
272 thread_data->team = NULL;
273 thread_data->task = &data->task;
274 thread_data->thread_num = 0;
275 thread_data->parallel = FALSE;
276 thread_data->fork_threads = 0;
277 thread_data->single = 1;
278 thread_data->section = 1;
279 thread_data->dynamic = 1;
280 thread_data->dynamic_type = 0;
282 vcomp_set_thread_data(thread_data);
283 return thread_data;
286 static void vcomp_free_thread_data(void)
288 struct vcomp_thread_data *thread_data = vcomp_get_thread_data();
289 if (!thread_data) return;
291 HeapFree(GetProcessHeap(), 0, thread_data);
292 vcomp_set_thread_data(NULL);
295 void CDECL _vcomp_atomic_add_i4(int *dest, int val)
297 interlocked_xchg_add(dest, val);
300 void CDECL _vcomp_atomic_and_i4(int *dest, int val)
302 int old;
303 do old = *dest; while (interlocked_cmpxchg(dest, old & val, old) != old);
306 void CDECL _vcomp_atomic_div_i4(int *dest, int val)
308 int old;
309 do old = *dest; while (interlocked_cmpxchg(dest, old / val, old) != old);
312 void CDECL _vcomp_atomic_div_ui4(unsigned int *dest, unsigned int val)
314 unsigned int old;
315 do old = *dest; while (interlocked_cmpxchg((int *)dest, old / val, old) != old);
318 void CDECL _vcomp_atomic_mul_i4(int *dest, int val)
320 int old;
321 do old = *dest; while (interlocked_cmpxchg(dest, old * val, old) != old);
324 void CDECL _vcomp_atomic_or_i4(int *dest, int val)
326 int old;
327 do old = *dest; while (interlocked_cmpxchg(dest, old | val, old) != old);
330 void CDECL _vcomp_atomic_shl_i4(int *dest, int val)
332 int old;
333 do old = *dest; while (interlocked_cmpxchg(dest, old << val, old) != old);
336 void CDECL _vcomp_atomic_shr_i4(int *dest, int val)
338 int old;
339 do old = *dest; while (interlocked_cmpxchg(dest, old >> val, old) != old);
342 void CDECL _vcomp_atomic_shr_ui4(unsigned int *dest, unsigned int val)
344 unsigned int old;
345 do old = *dest; while (interlocked_cmpxchg((int *)dest, old >> val, old) != old);
348 void CDECL _vcomp_atomic_sub_i4(int *dest, int val)
350 interlocked_xchg_add(dest, -val);
353 void CDECL _vcomp_atomic_xor_i4(int *dest, int val)
355 int old;
356 do old = *dest; while (interlocked_cmpxchg(dest, old ^ val, old) != old);
359 void CDECL _vcomp_atomic_add_r4(float *dest, float val)
361 int old, new;
364 old = *(int *)dest;
365 *(float *)&new = *(float *)&old + val;
367 while (interlocked_cmpxchg((int *)dest, new, old) != old);
370 void CDECL _vcomp_atomic_div_r4(float *dest, float val)
372 int old, new;
375 old = *(int *)dest;
376 *(float *)&new = *(float *)&old / val;
378 while (interlocked_cmpxchg((int *)dest, new, old) != old);
381 void CDECL _vcomp_atomic_mul_r4(float *dest, float val)
383 int old, new;
386 old = *(int *)dest;
387 *(float *)&new = *(float *)&old * val;
389 while (interlocked_cmpxchg((int *)dest, new, old) != old);
392 void CDECL _vcomp_atomic_sub_r4(float *dest, float val)
394 int old, new;
397 old = *(int *)dest;
398 *(float *)&new = *(float *)&old - val;
400 while (interlocked_cmpxchg((int *)dest, new, old) != old);
403 void CDECL _vcomp_atomic_add_r8(double *dest, double val)
405 LONG64 old, new;
408 old = *(LONG64 *)dest;
409 *(double *)&new = *(double *)&old + val;
411 while (interlocked_cmpxchg64((LONG64 *)dest, new, old) != old);
414 void CDECL _vcomp_atomic_div_r8(double *dest, double val)
416 LONG64 old, new;
419 old = *(LONG64 *)dest;
420 *(double *)&new = *(double *)&old / val;
422 while (interlocked_cmpxchg64((LONG64 *)dest, new, old) != old);
425 void CDECL _vcomp_atomic_mul_r8(double *dest, double val)
427 LONG64 old, new;
430 old = *(LONG64 *)dest;
431 *(double *)&new = *(double *)&old * val;
433 while (interlocked_cmpxchg64((LONG64 *)dest, new, old) != old);
436 void CDECL _vcomp_atomic_sub_r8(double *dest, double val)
438 LONG64 old, new;
441 old = *(LONG64 *)dest;
442 *(double *)&new = *(double *)&old - val;
444 while (interlocked_cmpxchg64((LONG64 *)dest, new, old) != old);
447 int CDECL omp_get_dynamic(void)
449 TRACE("stub\n");
450 return 0;
453 int CDECL omp_get_max_threads(void)
455 TRACE("()\n");
456 return vcomp_max_threads;
459 int CDECL omp_get_nested(void)
461 TRACE("stub\n");
462 return vcomp_nested_fork;
465 int CDECL omp_get_num_procs(void)
467 TRACE("stub\n");
468 return 1;
471 int CDECL omp_get_num_threads(void)
473 struct vcomp_team_data *team_data = vcomp_init_thread_data()->team;
474 TRACE("()\n");
475 return team_data ? team_data->num_threads : 1;
478 int CDECL omp_get_thread_num(void)
480 TRACE("()\n");
481 return vcomp_init_thread_data()->thread_num;
484 int CDECL _vcomp_get_thread_num(void)
486 TRACE("()\n");
487 return vcomp_init_thread_data()->thread_num;
490 /* Time in seconds since "some time in the past" */
491 double CDECL omp_get_wtime(void)
493 return GetTickCount() / 1000.0;
496 void CDECL omp_set_dynamic(int val)
498 TRACE("(%d): stub\n", val);
501 void CDECL omp_set_nested(int nested)
503 TRACE("(%d)\n", nested);
504 vcomp_nested_fork = (nested != 0);
507 void CDECL omp_set_num_threads(int num_threads)
509 TRACE("(%d)\n", num_threads);
510 if (num_threads >= 1)
511 vcomp_num_threads = num_threads;
514 void CDECL _vcomp_flush(void)
516 TRACE("(): stub\n");
519 void CDECL _vcomp_barrier(void)
521 struct vcomp_team_data *team_data = vcomp_init_thread_data()->team;
523 TRACE("()\n");
525 if (!team_data)
526 return;
528 EnterCriticalSection(&vcomp_section);
529 if (++team_data->barrier_count >= team_data->num_threads)
531 team_data->barrier++;
532 team_data->barrier_count = 0;
533 WakeAllConditionVariable(&team_data->cond);
535 else
537 unsigned int barrier = team_data->barrier;
538 while (team_data->barrier == barrier)
539 SleepConditionVariableCS(&team_data->cond, &vcomp_section, INFINITE);
541 LeaveCriticalSection(&vcomp_section);
544 void CDECL _vcomp_set_num_threads(int num_threads)
546 TRACE("(%d)\n", num_threads);
547 if (num_threads >= 1)
548 vcomp_init_thread_data()->fork_threads = num_threads;
551 int CDECL _vcomp_master_begin(void)
553 TRACE("()\n");
554 return !vcomp_init_thread_data()->thread_num;
557 void CDECL _vcomp_master_end(void)
559 TRACE("()\n");
560 /* nothing to do here */
563 int CDECL _vcomp_single_begin(int flags)
565 struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
566 struct vcomp_task_data *task_data = thread_data->task;
567 int ret = FALSE;
569 TRACE("(%x): semi-stub\n", flags);
571 EnterCriticalSection(&vcomp_section);
572 thread_data->single++;
573 if ((int)(thread_data->single - task_data->single) > 0)
575 task_data->single = thread_data->single;
576 ret = TRUE;
578 LeaveCriticalSection(&vcomp_section);
580 return ret;
583 void CDECL _vcomp_single_end(void)
585 TRACE("()\n");
586 /* nothing to do here */
589 void CDECL _vcomp_sections_init(int n)
591 struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
592 struct vcomp_task_data *task_data = thread_data->task;
594 TRACE("(%d)\n", n);
596 EnterCriticalSection(&vcomp_section);
597 thread_data->section++;
598 if ((int)(thread_data->section - task_data->section) > 0)
600 task_data->section = thread_data->section;
601 task_data->num_sections = n;
602 task_data->section_index = 0;
604 LeaveCriticalSection(&vcomp_section);
607 int CDECL _vcomp_sections_next(void)
609 struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
610 struct vcomp_task_data *task_data = thread_data->task;
611 int i = -1;
613 TRACE("()\n");
615 EnterCriticalSection(&vcomp_section);
616 if (thread_data->section == task_data->section &&
617 task_data->section_index != task_data->num_sections)
619 i = task_data->section_index++;
621 LeaveCriticalSection(&vcomp_section);
622 return i;
625 void CDECL _vcomp_for_static_simple_init(unsigned int first, unsigned int last, int step,
626 BOOL increment, unsigned int *begin, unsigned int *end)
628 unsigned int iterations, per_thread, remaining;
629 struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
630 struct vcomp_team_data *team_data = thread_data->team;
631 int num_threads = team_data ? team_data->num_threads : 1;
632 int thread_num = thread_data->thread_num;
634 TRACE("(%u, %u, %d, %u, %p, %p)\n", first, last, step, increment, begin, end);
636 if (num_threads == 1)
638 *begin = first;
639 *end = last;
640 return;
643 if (step <= 0)
645 *begin = 0;
646 *end = increment ? -1 : 1;
647 return;
650 if (increment)
651 iterations = 1 + (last - first) / step;
652 else
654 iterations = 1 + (first - last) / step;
655 step *= -1;
658 per_thread = iterations / num_threads;
659 remaining = iterations - per_thread * num_threads;
661 if (thread_num < remaining)
662 per_thread++;
663 else if (per_thread)
664 first += remaining * step;
665 else
667 *begin = first;
668 *end = first - step;
669 return;
672 *begin = first + per_thread * thread_num * step;
673 *end = *begin + (per_thread - 1) * step;
676 void CDECL _vcomp_for_static_init(int first, int last, int step, int chunksize, unsigned int *loops,
677 int *begin, int *end, int *next, int *lastchunk)
679 unsigned int iterations, num_chunks, per_thread, remaining;
680 struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
681 struct vcomp_team_data *team_data = thread_data->team;
682 int num_threads = team_data ? team_data->num_threads : 1;
683 int thread_num = thread_data->thread_num;
685 TRACE("(%d, %d, %d, %d, %p, %p, %p, %p, %p)\n",
686 first, last, step, chunksize, loops, begin, end, next, lastchunk);
688 if (num_threads == 1 && chunksize != 1)
690 *loops = 1;
691 *begin = first;
692 *end = last;
693 *next = 0;
694 *lastchunk = first;
695 return;
698 if (first == last)
700 *loops = !thread_num;
701 if (!thread_num)
703 *begin = first;
704 *end = last;
705 *next = 0;
706 *lastchunk = first;
708 return;
711 if (step <= 0)
713 *loops = 0;
714 return;
717 if (first < last)
718 iterations = 1 + (last - first) / step;
719 else
721 iterations = 1 + (first - last) / step;
722 step *= -1;
725 if (chunksize < 1)
726 chunksize = 1;
728 num_chunks = ((DWORD64)iterations + chunksize - 1) / chunksize;
729 per_thread = num_chunks / num_threads;
730 remaining = num_chunks - per_thread * num_threads;
732 *loops = per_thread + (thread_num < remaining);
733 *begin = first + thread_num * chunksize * step;
734 *end = *begin + (chunksize - 1) * step;
735 *next = chunksize * num_threads * step;
736 *lastchunk = first + (num_chunks - 1) * chunksize * step;
739 void CDECL _vcomp_for_static_end(void)
741 TRACE("()\n");
742 /* nothing to do here */
745 void CDECL _vcomp_for_dynamic_init(unsigned int flags, unsigned int first, unsigned int last,
746 int step, unsigned int chunksize)
748 unsigned int iterations, per_thread, remaining;
749 struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
750 struct vcomp_team_data *team_data = thread_data->team;
751 struct vcomp_task_data *task_data = thread_data->task;
752 int num_threads = team_data ? team_data->num_threads : 1;
753 int thread_num = thread_data->thread_num;
754 unsigned int type = flags & ~VCOMP_DYNAMIC_FLAGS_INCREMENT;
756 TRACE("(%u, %u, %u, %d, %u)\n", flags, first, last, step, chunksize);
758 if (step <= 0)
760 thread_data->dynamic_type = 0;
761 return;
764 if (flags & VCOMP_DYNAMIC_FLAGS_INCREMENT)
765 iterations = 1 + (last - first) / step;
766 else
768 iterations = 1 + (first - last) / step;
769 step *= -1;
772 if (type == VCOMP_DYNAMIC_FLAGS_STATIC)
774 per_thread = iterations / num_threads;
775 remaining = iterations - per_thread * num_threads;
777 if (thread_num < remaining)
778 per_thread++;
779 else if (per_thread)
780 first += remaining * step;
781 else
783 thread_data->dynamic_type = 0;
784 return;
787 thread_data->dynamic_type = VCOMP_DYNAMIC_FLAGS_STATIC;
788 thread_data->dynamic_begin = first + per_thread * thread_num * step;
789 thread_data->dynamic_end = thread_data->dynamic_begin + (per_thread - 1) * step;
791 else
793 if (type != VCOMP_DYNAMIC_FLAGS_CHUNKED &&
794 type != VCOMP_DYNAMIC_FLAGS_GUIDED)
796 FIXME("unsupported flags %u\n", flags);
797 type = VCOMP_DYNAMIC_FLAGS_GUIDED;
800 EnterCriticalSection(&vcomp_section);
801 thread_data->dynamic++;
802 thread_data->dynamic_type = type;
803 if ((int)(thread_data->dynamic - task_data->dynamic) > 0)
805 task_data->dynamic = thread_data->dynamic;
806 task_data->dynamic_first = first;
807 task_data->dynamic_last = last;
808 task_data->dynamic_iterations = iterations;
809 task_data->dynamic_step = step;
810 task_data->dynamic_chunksize = chunksize;
812 LeaveCriticalSection(&vcomp_section);
816 int CDECL _vcomp_for_dynamic_next(unsigned int *begin, unsigned int *end)
818 struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
819 struct vcomp_task_data *task_data = thread_data->task;
820 struct vcomp_team_data *team_data = thread_data->team;
821 int num_threads = team_data ? team_data->num_threads : 1;
823 TRACE("(%p, %p)\n", begin, end);
825 if (thread_data->dynamic_type == VCOMP_DYNAMIC_FLAGS_STATIC)
827 *begin = thread_data->dynamic_begin;
828 *end = thread_data->dynamic_end;
829 thread_data->dynamic_type = 0;
830 return 1;
832 else if (thread_data->dynamic_type == VCOMP_DYNAMIC_FLAGS_CHUNKED ||
833 thread_data->dynamic_type == VCOMP_DYNAMIC_FLAGS_GUIDED)
835 unsigned int iterations = 0;
836 EnterCriticalSection(&vcomp_section);
837 if (thread_data->dynamic == task_data->dynamic &&
838 task_data->dynamic_iterations != 0)
840 iterations = min(task_data->dynamic_iterations, task_data->dynamic_chunksize);
841 if (thread_data->dynamic_type == VCOMP_DYNAMIC_FLAGS_GUIDED &&
842 task_data->dynamic_iterations > num_threads * task_data->dynamic_chunksize)
844 iterations = (task_data->dynamic_iterations + num_threads - 1) / num_threads;
846 *begin = task_data->dynamic_first;
847 *end = task_data->dynamic_first + (iterations - 1) * task_data->dynamic_step;
848 task_data->dynamic_iterations -= iterations;
849 task_data->dynamic_first += iterations * task_data->dynamic_step;
850 if (!task_data->dynamic_iterations)
851 *end = task_data->dynamic_last;
853 LeaveCriticalSection(&vcomp_section);
854 return iterations != 0;
857 return 0;
860 int CDECL omp_in_parallel(void)
862 TRACE("()\n");
863 return vcomp_init_thread_data()->parallel;
866 static DWORD WINAPI _vcomp_fork_worker(void *param)
868 struct vcomp_thread_data *thread_data = param;
869 vcomp_set_thread_data(thread_data);
871 TRACE("starting worker thread for %p\n", thread_data);
873 EnterCriticalSection(&vcomp_section);
874 for (;;)
876 struct vcomp_team_data *team = thread_data->team;
877 if (team != NULL)
879 LeaveCriticalSection(&vcomp_section);
880 _vcomp_fork_call_wrapper(team->wrapper, team->nargs, team->valist);
881 EnterCriticalSection(&vcomp_section);
883 thread_data->team = NULL;
884 list_remove(&thread_data->entry);
885 list_add_tail(&vcomp_idle_threads, &thread_data->entry);
886 if (++team->finished_threads >= team->num_threads)
887 WakeAllConditionVariable(&team->cond);
890 if (!SleepConditionVariableCS(&thread_data->cond, &vcomp_section, 5000) &&
891 GetLastError() == ERROR_TIMEOUT && !thread_data->team)
893 break;
896 list_remove(&thread_data->entry);
897 LeaveCriticalSection(&vcomp_section);
899 TRACE("terminating worker thread for %p\n", thread_data);
901 HeapFree(GetProcessHeap(), 0, thread_data);
902 vcomp_set_thread_data(NULL);
903 FreeLibraryAndExitThread(vcomp_module, 0);
904 return 0;
907 void WINAPIV _vcomp_fork(BOOL ifval, int nargs, void *wrapper, ...)
909 struct vcomp_thread_data *prev_thread_data = vcomp_init_thread_data();
910 struct vcomp_thread_data thread_data;
911 struct vcomp_team_data team_data;
912 struct vcomp_task_data task_data;
913 int num_threads;
915 TRACE("(%d, %d, %p, ...)\n", ifval, nargs, wrapper);
917 if (prev_thread_data->parallel && !vcomp_nested_fork)
918 ifval = FALSE;
920 if (!ifval)
921 num_threads = 1;
922 else if (prev_thread_data->fork_threads)
923 num_threads = prev_thread_data->fork_threads;
924 else
925 num_threads = vcomp_num_threads;
927 InitializeConditionVariable(&team_data.cond);
928 team_data.num_threads = 1;
929 team_data.finished_threads = 0;
930 team_data.nargs = nargs;
931 team_data.wrapper = wrapper;
932 __ms_va_start(team_data.valist, wrapper);
933 team_data.barrier = 0;
934 team_data.barrier_count = 0;
936 task_data.single = 0;
937 task_data.section = 0;
938 task_data.dynamic = 0;
940 thread_data.team = &team_data;
941 thread_data.task = &task_data;
942 thread_data.thread_num = 0;
943 thread_data.parallel = ifval || prev_thread_data->parallel;
944 thread_data.fork_threads = 0;
945 thread_data.single = 1;
946 thread_data.section = 1;
947 thread_data.dynamic = 1;
948 thread_data.dynamic_type = 0;
949 list_init(&thread_data.entry);
950 InitializeConditionVariable(&thread_data.cond);
952 if (num_threads > 1)
954 struct list *ptr;
955 EnterCriticalSection(&vcomp_section);
957 /* reuse existing threads (if any) */
958 while (team_data.num_threads < num_threads && (ptr = list_head(&vcomp_idle_threads)))
960 struct vcomp_thread_data *data = LIST_ENTRY(ptr, struct vcomp_thread_data, entry);
961 data->team = &team_data;
962 data->task = &task_data;
963 data->thread_num = team_data.num_threads++;
964 data->parallel = thread_data.parallel;
965 data->fork_threads = 0;
966 data->single = 1;
967 data->section = 1;
968 data->dynamic = 1;
969 data->dynamic_type = 0;
970 list_remove(&data->entry);
971 list_add_tail(&thread_data.entry, &data->entry);
972 WakeAllConditionVariable(&data->cond);
975 /* spawn additional threads */
976 while (team_data.num_threads < num_threads)
978 struct vcomp_thread_data *data;
979 HMODULE module;
980 HANDLE thread;
982 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
983 if (!data) break;
985 data->team = &team_data;
986 data->task = &task_data;
987 data->thread_num = team_data.num_threads;
988 data->parallel = thread_data.parallel;
989 data->fork_threads = 0;
990 data->single = 1;
991 data->section = 1;
992 data->dynamic = 1;
993 data->dynamic_type = 0;
994 InitializeConditionVariable(&data->cond);
996 thread = CreateThread(NULL, 0, _vcomp_fork_worker, data, 0, NULL);
997 if (!thread)
999 HeapFree(GetProcessHeap(), 0, data);
1000 break;
1003 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
1004 (const WCHAR *)vcomp_module, &module);
1005 team_data.num_threads++;
1006 list_add_tail(&thread_data.entry, &data->entry);
1007 CloseHandle(thread);
1010 LeaveCriticalSection(&vcomp_section);
1013 vcomp_set_thread_data(&thread_data);
1014 _vcomp_fork_call_wrapper(team_data.wrapper, team_data.nargs, team_data.valist);
1015 vcomp_set_thread_data(prev_thread_data);
1016 prev_thread_data->fork_threads = 0;
1018 if (team_data.num_threads > 1)
1020 EnterCriticalSection(&vcomp_section);
1022 team_data.finished_threads++;
1023 while (team_data.finished_threads < team_data.num_threads)
1024 SleepConditionVariableCS(&team_data.cond, &vcomp_section, INFINITE);
1026 LeaveCriticalSection(&vcomp_section);
1027 assert(list_empty(&thread_data.entry));
1030 __ms_va_end(team_data.valist);
1033 static CRITICAL_SECTION *alloc_critsect(void)
1035 CRITICAL_SECTION *critsect;
1036 if (!(critsect = HeapAlloc(GetProcessHeap(), 0, sizeof(*critsect))))
1038 ERR("could not allocate critical section\n");
1039 ExitProcess(1);
1042 InitializeCriticalSection(critsect);
1043 critsect->DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": critsect");
1044 return critsect;
1047 static void destroy_critsect(CRITICAL_SECTION *critsect)
1049 if (!critsect) return;
1050 critsect->DebugInfo->Spare[0] = 0;
1051 DeleteCriticalSection(critsect);
1052 HeapFree(GetProcessHeap(), 0, critsect);
1055 void CDECL omp_init_lock(omp_lock_t *lock)
1057 TRACE("(%p)\n", lock);
1058 *lock = alloc_critsect();
1061 void CDECL omp_destroy_lock(omp_lock_t *lock)
1063 TRACE("(%p)\n", lock);
1064 destroy_critsect(*lock);
1067 void CDECL omp_set_lock(omp_lock_t *lock)
1069 TRACE("(%p)\n", lock);
1071 if (RtlIsCriticalSectionLockedByThread(*lock))
1073 ERR("omp_set_lock called while holding lock %p\n", *lock);
1074 ExitProcess(1);
1077 EnterCriticalSection(*lock);
1080 void CDECL omp_unset_lock(omp_lock_t *lock)
1082 TRACE("(%p)\n", lock);
1083 LeaveCriticalSection(*lock);
1086 int CDECL omp_test_lock(omp_lock_t *lock)
1088 TRACE("(%p)\n", lock);
1090 if (RtlIsCriticalSectionLockedByThread(*lock))
1091 return 0;
1093 return TryEnterCriticalSection(*lock);
1096 void CDECL omp_set_nest_lock(omp_nest_lock_t *lock)
1098 TRACE("(%p)\n", lock);
1099 EnterCriticalSection(*lock);
1102 void CDECL omp_unset_nest_lock(omp_nest_lock_t *lock)
1104 TRACE("(%p)\n", lock);
1105 LeaveCriticalSection(*lock);
1108 int CDECL omp_test_nest_lock(omp_nest_lock_t *lock)
1110 TRACE("(%p)\n", lock);
1111 return TryEnterCriticalSection(*lock) ? (*lock)->RecursionCount : 0;
1114 void CDECL _vcomp_enter_critsect(CRITICAL_SECTION **critsect)
1116 TRACE("(%p)\n", critsect);
1118 if (!*critsect)
1120 CRITICAL_SECTION *new_critsect = alloc_critsect();
1121 if (interlocked_cmpxchg_ptr((void **)critsect, new_critsect, NULL) != NULL)
1122 destroy_critsect(new_critsect); /* someone beat us to it */
1125 EnterCriticalSection(*critsect);
1128 void CDECL _vcomp_leave_critsect(CRITICAL_SECTION *critsect)
1130 TRACE("(%p)\n", critsect);
1131 LeaveCriticalSection(critsect);
1134 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
1136 TRACE("(%p, %d, %p)\n", instance, reason, reserved);
1138 switch (reason)
1140 case DLL_WINE_PREATTACH:
1141 return FALSE; /* prefer native version */
1143 case DLL_PROCESS_ATTACH:
1145 SYSTEM_INFO sysinfo;
1147 if ((vcomp_context_tls = TlsAlloc()) == TLS_OUT_OF_INDEXES)
1149 ERR("Failed to allocate TLS index\n");
1150 return FALSE;
1153 GetSystemInfo(&sysinfo);
1154 vcomp_module = instance;
1155 vcomp_max_threads = sysinfo.dwNumberOfProcessors;
1156 vcomp_num_threads = sysinfo.dwNumberOfProcessors;
1157 break;
1160 case DLL_PROCESS_DETACH:
1162 if (reserved) break;
1163 if (vcomp_context_tls != TLS_OUT_OF_INDEXES)
1165 vcomp_free_thread_data();
1166 TlsFree(vcomp_context_tls);
1168 break;
1171 case DLL_THREAD_DETACH:
1173 vcomp_free_thread_data();
1174 break;
1178 return TRUE;