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
25 #include "wine/port.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
=
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
;
70 /* only used for concurrent tasks */
72 CONDITION_VARIABLE cond
;
82 unsigned int dynamic_type
;
83 unsigned int dynamic_begin
;
84 unsigned int dynamic_end
;
87 struct vcomp_team_data
89 CONDITION_VARIABLE cond
;
93 /* callback arguments */
103 struct vcomp_task_data
109 unsigned int section
;
114 unsigned int dynamic
;
115 unsigned int dynamic_first
;
116 unsigned int dynamic_last
;
117 unsigned int dynamic_iterations
;
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
,
127 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
128 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
130 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
132 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
134 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
135 "movl 12(%ebp),%edx\n\t"
142 "movl 12(%ebp),%ecx\n\t"
143 "movl 16(%ebp),%esi\n\t"
146 "1:\tcall *8(%ebp)\n\t"
147 "leal -8(%ebp),%esp\n\t"
149 __ASM_CFI(".cfi_same_value %edi\n\t")
151 __ASM_CFI(".cfi_same_value %esi\n\t")
153 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
154 __ASM_CFI(".cfi_same_value %ebp\n\t")
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
,
162 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
163 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
165 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
167 __ASM_CFI(".cfi_rel_offset %rsi,-8\n\t")
169 __ASM_CFI(".cfi_rel_offset %rdi,-16\n\t")
173 "cmovgq %rdx,%rcx\n\t"
174 "leaq 0(,%rcx,8),%rdx\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"
185 "leaq -16(%rbp),%rsp\n\t"
187 __ASM_CFI(".cfi_same_value %rdi\n\t")
189 __ASM_CFI(".cfi_same_value %rsi\n\t")
190 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
192 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
193 __ASM_CFI(".cfi_same_value %rbp\n\t")
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
,
201 "push {r4, r5, LR}\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"
227 "4:\tpop {r0-r3}\n\t"
234 static void CDECL
_vcomp_fork_call_wrapper(void *wrapper
, int nargs
, __ms_va_list args
)
236 ERR("Not implemented for this architecture\n");
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();
256 struct vcomp_thread_data thread
;
257 struct vcomp_task_data task
;
260 if (thread_data
) return thread_data
;
261 if (!(data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
))))
263 ERR("could not create thread data\n");
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
);
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
)
303 do old
= *dest
; while (interlocked_cmpxchg(dest
, old
& val
, old
) != old
);
306 void CDECL
_vcomp_atomic_div_i4(int *dest
, int val
)
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
)
315 do old
= *dest
; while (interlocked_cmpxchg((int *)dest
, old
/ val
, old
) != old
);
318 void CDECL
_vcomp_atomic_mul_i4(int *dest
, int val
)
321 do old
= *dest
; while (interlocked_cmpxchg(dest
, old
* val
, old
) != old
);
324 void CDECL
_vcomp_atomic_or_i4(int *dest
, int val
)
327 do old
= *dest
; while (interlocked_cmpxchg(dest
, old
| val
, old
) != old
);
330 void CDECL
_vcomp_atomic_shl_i4(int *dest
, int val
)
333 do old
= *dest
; while (interlocked_cmpxchg(dest
, old
<< val
, old
) != old
);
336 void CDECL
_vcomp_atomic_shr_i4(int *dest
, int val
)
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
)
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
)
356 do old
= *dest
; while (interlocked_cmpxchg(dest
, old
^ val
, old
) != old
);
359 void CDECL
_vcomp_atomic_add_r4(float *dest
, float val
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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)
453 int CDECL
omp_get_max_threads(void)
456 return vcomp_max_threads
;
459 int CDECL
omp_get_nested(void)
462 return vcomp_nested_fork
;
465 int CDECL
omp_get_num_procs(void)
471 int CDECL
omp_get_num_threads(void)
473 struct vcomp_team_data
*team_data
= vcomp_init_thread_data()->team
;
475 return team_data
? team_data
->num_threads
: 1;
478 int CDECL
omp_get_thread_num(void)
481 return vcomp_init_thread_data()->thread_num
;
484 int CDECL
_vcomp_get_thread_num(void)
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)
519 void CDECL
_vcomp_barrier(void)
521 struct vcomp_team_data
*team_data
= vcomp_init_thread_data()->team
;
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
);
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)
554 return !vcomp_init_thread_data()->thread_num
;
557 void CDECL
_vcomp_master_end(void)
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
;
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
;
578 LeaveCriticalSection(&vcomp_section
);
583 void CDECL
_vcomp_single_end(void)
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
;
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
;
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
);
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)
646 *end
= increment
? -1 : 1;
651 iterations
= 1 + (last
- first
) / step
;
654 iterations
= 1 + (first
- last
) / step
;
658 per_thread
= iterations
/ num_threads
;
659 remaining
= iterations
- per_thread
* num_threads
;
661 if (thread_num
< remaining
)
664 first
+= remaining
* step
;
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)
700 *loops
= !thread_num
;
718 iterations
= 1 + (last
- first
) / step
;
721 iterations
= 1 + (first
- last
) / step
;
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)
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
);
760 thread_data
->dynamic_type
= 0;
764 if (flags
& VCOMP_DYNAMIC_FLAGS_INCREMENT
)
765 iterations
= 1 + (last
- first
) / step
;
768 iterations
= 1 + (first
- last
) / step
;
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
)
780 first
+= remaining
* step
;
783 thread_data
->dynamic_type
= 0;
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
;
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;
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;
860 int CDECL
omp_in_parallel(void)
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
);
876 struct vcomp_team_data
*team
= thread_data
->team
;
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
)
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);
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
;
915 TRACE("(%d, %d, %p, ...)\n", ifval
, nargs
, wrapper
);
917 if (prev_thread_data
->parallel
&& !vcomp_nested_fork
)
922 else if (prev_thread_data
->fork_threads
)
923 num_threads
= prev_thread_data
->fork_threads
;
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
);
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;
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
;
982 data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
));
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;
993 data
->dynamic_type
= 0;
994 InitializeConditionVariable(&data
->cond
);
996 thread
= CreateThread(NULL
, 0, _vcomp_fork_worker
, data
, 0, NULL
);
999 HeapFree(GetProcessHeap(), 0, data
);
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");
1042 InitializeCriticalSection(critsect
);
1043 critsect
->DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": 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
);
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
))
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
);
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
);
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");
1153 GetSystemInfo(&sysinfo
);
1154 vcomp_module
= instance
;
1155 vcomp_max_threads
= sysinfo
.dwNumberOfProcessors
;
1156 vcomp_num_threads
= sysinfo
.dwNumberOfProcessors
;
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
);
1171 case DLL_THREAD_DETACH
:
1173 vcomp_free_thread_data();