main.c 56.4 KB
Newer Older
Austin English's avatar
Austin English committed
1 2 3 4 5
/*
 *
 * vcomp implementation
 *
 * Copyright 2011 Austin English
6
 * Copyright 2012 Dan Kegel
7
 * Copyright 2015-2016 Sebastian Lackner
Austin English's avatar
Austin English committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>
25
#include <assert.h>
Austin English's avatar
Austin English committed
26 27 28

#include "windef.h"
#include "winbase.h"
29
#include "winternl.h"
Austin English's avatar
Austin English committed
30
#include "wine/debug.h"
31
#include "wine/list.h"
32
#include "wine/asm.h"
Austin English's avatar
Austin English committed
33 34 35

WINE_DEFAULT_DEBUG_CHANNEL(vcomp);

36 37
#define MAX_VECT_PARALLEL_CALLBACK_ARGS 128

38 39 40
typedef CRITICAL_SECTION *omp_lock_t;
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 45
static int     vcomp_max_threads;
static int     vcomp_num_threads;
46
static int     vcomp_num_procs;
47
static BOOL    vcomp_nested_fork = FALSE;
48

49 50 51 52 53 54 55 56 57
static RTL_CRITICAL_SECTION vcomp_section;
static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
{
    0, 0, &vcomp_section,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": vcomp_section") }
};
static RTL_CRITICAL_SECTION vcomp_section = { &critsect_debug, -1, 0, 0, 0, 0 };

58 59 60 61 62
#define VCOMP_DYNAMIC_FLAGS_STATIC      0x01
#define VCOMP_DYNAMIC_FLAGS_CHUNKED     0x02
#define VCOMP_DYNAMIC_FLAGS_GUIDED      0x03
#define VCOMP_DYNAMIC_FLAGS_INCREMENT   0x40

63 64
struct vcomp_thread_data
{
65
    struct vcomp_team_data  *team;
66
    struct vcomp_task_data  *task;
67
    int                     thread_num;
68
    BOOL                    parallel;
69
    int                     fork_threads;
70 71 72 73

    /* only used for concurrent tasks */
    struct list             entry;
    CONDITION_VARIABLE      cond;
74

75 76 77
    /* single */
    unsigned int            single;

78 79
    /* section */
    unsigned int            section;
80 81 82 83 84 85

    /* dynamic */
    unsigned int            dynamic;
    unsigned int            dynamic_type;
    unsigned int            dynamic_begin;
    unsigned int            dynamic_end;
86 87 88 89 90 91 92 93 94 95 96
};

struct vcomp_team_data
{
    CONDITION_VARIABLE      cond;
    int                     num_threads;
    int                     finished_threads;

    /* callback arguments */
    int                     nargs;
    void                    *wrapper;
97
    va_list                 valist;
98 99 100 101

    /* barrier */
    unsigned int            barrier;
    int                     barrier_count;
102
};
103

104 105
struct vcomp_task_data
{
106 107 108
    /* single */
    unsigned int            single;

109 110 111 112
    /* section */
    unsigned int            section;
    int                     num_sections;
    int                     section_index;
113 114 115 116 117 118 119 120

    /* dynamic */
    unsigned int            dynamic;
    unsigned int            dynamic_first;
    unsigned int            dynamic_last;
    unsigned int            dynamic_iterations;
    int                     dynamic_step;
    unsigned int            dynamic_chunksize;
121 122
};

123
static void **ptr_from_va_list(va_list valist)
124 125 126 127
{
    return *(void ***)&valist;
}

128
static void copy_va_list_data(void **args, va_list valist, int args_count)
129 130 131 132 133 134 135
{
    unsigned int i;

    for (i = 0; i < args_count; ++i)
        args[i] = va_arg(valist, void *);
}

136 137
#if defined(__i386__)

138
extern void CDECL _vcomp_fork_call_wrapper(void *wrapper, int nargs, void **args);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
__ASM_GLOBAL_FUNC( _vcomp_fork_call_wrapper,
                   "pushl %ebp\n\t"
                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
                   __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
                   "movl %esp,%ebp\n\t"
                   __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
                   "pushl %esi\n\t"
                   __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
                   "pushl %edi\n\t"
                   __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
                   "movl 12(%ebp),%edx\n\t"
                   "movl %esp,%edi\n\t"
                   "shll $2,%edx\n\t"
                   "jz 1f\n\t"
                   "subl %edx,%edi\n\t"
                   "andl $~15,%edi\n\t"
                   "movl %edi,%esp\n\t"
                   "movl 12(%ebp),%ecx\n\t"
                   "movl 16(%ebp),%esi\n\t"
                   "cld\n\t"
                   "rep; movsl\n"
                   "1:\tcall *8(%ebp)\n\t"
                   "leal -8(%ebp),%esp\n\t"
                   "popl %edi\n\t"
                   __ASM_CFI(".cfi_same_value %edi\n\t")
                   "popl %esi\n\t"
                   __ASM_CFI(".cfi_same_value %esi\n\t")
                   "popl %ebp\n\t"
                   __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
                   __ASM_CFI(".cfi_same_value %ebp\n\t")
                   "ret" )

#elif defined(__x86_64__)

173
extern void CDECL _vcomp_fork_call_wrapper(void *wrapper, int nargs, void **args);
174 175
__ASM_GLOBAL_FUNC( _vcomp_fork_call_wrapper,
                   "pushq %rbp\n\t"
176
                   __ASM_SEH(".seh_pushreg %rbp\n\t")
177 178 179
                   __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
                   __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
                   "movq %rsp,%rbp\n\t"
180
                   __ASM_SEH(".seh_setframe %rbp,0\n\t")
181 182
                   __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
                   "pushq %rsi\n\t"
183
                   __ASM_SEH(".seh_pushreg %rsi\n\t")
184 185
                   __ASM_CFI(".cfi_rel_offset %rsi,-8\n\t")
                   "pushq %rdi\n\t"
186 187
                   __ASM_SEH(".seh_pushreg %rdi\n\t")
                   __ASM_SEH(".seh_endprologue\n\t")
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
                   __ASM_CFI(".cfi_rel_offset %rdi,-16\n\t")
                   "movq %rcx,%rax\n\t"
                   "movq $4,%rcx\n\t"
                   "cmp %rcx,%rdx\n\t"
                   "cmovgq %rdx,%rcx\n\t"
                   "leaq 0(,%rcx,8),%rdx\n\t"
                   "subq %rdx,%rsp\n\t"
                   "andq $~15,%rsp\n\t"
                   "movq %rsp,%rdi\n\t"
                   "movq %r8,%rsi\n\t"
                   "rep; movsq\n\t"
                   "movq 0(%rsp),%rcx\n\t"
                   "movq 8(%rsp),%rdx\n\t"
                   "movq 16(%rsp),%r8\n\t"
                   "movq 24(%rsp),%r9\n\t"
                   "callq *%rax\n\t"
                   "leaq -16(%rbp),%rsp\n\t"
                   "popq %rdi\n\t"
                   __ASM_CFI(".cfi_same_value %rdi\n\t")
                   "popq %rsi\n\t"
                   __ASM_CFI(".cfi_same_value %rsi\n\t")
                   __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
                   "popq %rbp\n\t"
                   __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
                   __ASM_CFI(".cfi_same_value %rbp\n\t")
                   "ret")

215 216
#elif defined(__arm__)

217
extern void CDECL _vcomp_fork_call_wrapper(void *wrapper, int nargs, void **args);
218 219 220 221 222 223 224 225 226
__ASM_GLOBAL_FUNC( _vcomp_fork_call_wrapper,
                   "push {r4, r5, LR}\n\t"
                   "mov r4, r0\n\t"
                   "mov r5, SP\n\t"
                   "lsl r3, r1, #2\n\t"
                   "cmp r3, #0\n\t"
                   "beq 5f\n\t"
                   "sub SP, SP, r3\n\t"
                   "tst r1, #1\n\t"
227
                   "it eq\n\t"
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
                   "subeq SP, SP, #4\n\t"
                   "1:\tsub r3, r3, #4\n\t"
                   "ldr r0, [r2, r3]\n\t"
                   "str r0, [SP, r3]\n\t"
                   "cmp r3, #0\n\t"
                   "bgt 1b\n\t"
                   "cmp r1, #1\n\t"
                   "bgt 2f\n\t"
                   "pop {r0}\n\t"
                   "b 5f\n\t"
                   "2:\tcmp r1, #2\n\t"
                   "bgt 3f\n\t"
                   "pop {r0-r1}\n\t"
                   "b 5f\n\t"
                   "3:\tcmp r1, #3\n\t"
                   "bgt 4f\n\t"
                   "pop {r0-r2}\n\t"
                   "b 5f\n\t"
                   "4:\tpop {r0-r3}\n\t"
                   "5:\tblx r4\n\t"
                   "mov SP, r5\n\t"
                   "pop {r4, r5, PC}" )

251 252
#elif defined(__aarch64__)

253
extern void CDECL _vcomp_fork_call_wrapper(void *wrapper, int nargs, void **args);
254 255 256 257
__ASM_GLOBAL_FUNC( _vcomp_fork_call_wrapper,
                   "stp x29, x30, [SP,#-16]!\n\t"
                   "mov x29, SP\n\t"
                   "mov x9, x0\n\t"
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
                   "cbz w1, 4f\n\t"
                   "lsl w8, w1, #3\n\t"
                   "cmp w8, #64\n\t"
                   "b.ge 1f\n\t"
                   "mov w8, #64\n"
                   "1:\ttbz w8, #3, 2f\n\t"
                   "add w8, w8, #8\n"
                   "2:\tsub x10, x29, x8\n\t"
                   "mov sp, x10\n"
                   "3:\tldr x0, [x2], #8\n\t"
                   "str x0, [x10], #8\n\t"
                   "subs w1, w1, #1\n\t"
                   "b.ne 3b\n\t"
                   "ldp x0, x1, [sp], #16\n\t"
                   "ldp x2, x3, [sp], #16\n\t"
                   "ldp x4, x5, [sp], #16\n\t"
                   "ldp x6, x7, [sp], #16\n"
                   "4:\tblr x9\n\t"
276 277 278 279
                   "mov SP, x29\n\t"
                   "ldp x29, x30, [SP], #16\n\t"
                   "ret\n" )

280 281
#else

282
static void CDECL _vcomp_fork_call_wrapper(void *wrapper, int nargs, void **args)
283 284 285 286 287 288
{
    ERR("Not implemented for this architecture\n");
}

#endif

289 290
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))

291 292 293 294 295 296 297 298
static inline char interlocked_cmpxchg8(char *dest, char xchg, char compare)
{
    char ret;
    __asm__ __volatile__( "lock; cmpxchgb %2,(%1)"
                          : "=a" (ret) : "r" (dest), "q" (xchg), "0" (compare) : "memory" );
    return ret;
}

299 300 301 302 303 304 305 306
static inline short interlocked_cmpxchg16(short *dest, short xchg, short compare)
{
    short ret;
    __asm__ __volatile__( "lock; cmpxchgw %2,(%1)"
                          : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
    return ret;
}

307 308 309 310 311 312 313 314
static inline char interlocked_xchg_add8(char *dest, char incr)
{
    char ret;
    __asm__ __volatile__( "lock; xaddb %0,(%1)"
                          : "=q" (ret) : "r" (dest), "0" (incr) : "memory" );
    return ret;
}

315 316 317 318 319 320 321 322 323 324
static inline short interlocked_xchg_add16(short *dest, short incr)
{
    short ret;
    __asm__ __volatile__( "lock; xaddw %0,(%1)"
                          : "=r" (ret) : "r" (dest), "0" (incr) : "memory" );
    return ret;
}

#else  /* __GNUC__ */

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1
static inline char interlocked_cmpxchg8(char *dest, char xchg, char compare)
{
    return __sync_val_compare_and_swap(dest, compare, xchg);
}

static inline char interlocked_xchg_add8(char *dest, char incr)
{
    return __sync_fetch_and_add(dest, incr);
}
#else
static char interlocked_cmpxchg8(char *dest, char xchg, char compare)
{
    EnterCriticalSection(&vcomp_section);
    if (*dest == compare) *dest = xchg; else compare = *dest;
    LeaveCriticalSection(&vcomp_section);
    return compare;
}

static char interlocked_xchg_add8(char *dest, char incr)
{
    char ret;
    EnterCriticalSection(&vcomp_section);
    ret = *dest; *dest += incr;
    LeaveCriticalSection(&vcomp_section);
    return ret;
}
#endif

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
static inline short interlocked_cmpxchg16(short *dest, short xchg, short compare)
{
    return __sync_val_compare_and_swap(dest, compare, xchg);
}

static inline short interlocked_xchg_add16(short *dest, short incr)
{
    return __sync_fetch_and_add(dest, incr);
}
#else
static short interlocked_cmpxchg16(short *dest, short xchg, short compare)
{
    EnterCriticalSection(&vcomp_section);
    if (*dest == compare) *dest = xchg; else compare = *dest;
    LeaveCriticalSection(&vcomp_section);
    return compare;
}

static short interlocked_xchg_add16(short *dest, short incr)
{
    short ret;
    EnterCriticalSection(&vcomp_section);
    ret = *dest; *dest += incr;
    LeaveCriticalSection(&vcomp_section);
    return ret;
}
#endif

#endif  /* __GNUC__ */

385 386 387 388 389 390 391 392 393 394 395 396 397
static inline struct vcomp_thread_data *vcomp_get_thread_data(void)
{
    return (struct vcomp_thread_data *)TlsGetValue(vcomp_context_tls);
}

static inline void vcomp_set_thread_data(struct vcomp_thread_data *thread_data)
{
    TlsSetValue(vcomp_context_tls, thread_data);
}

static struct vcomp_thread_data *vcomp_init_thread_data(void)
{
    struct vcomp_thread_data *thread_data = vcomp_get_thread_data();
398 399 400 401 402
    struct
    {
        struct vcomp_thread_data thread;
        struct vcomp_task_data   task;
    } *data;
403

404 405
    if (thread_data) return thread_data;
    if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data))))
406 407 408 409 410
    {
        ERR("could not create thread data\n");
        ExitProcess(1);
    }

411
    data->task.single           = 0;
412
    data->task.section          = 0;
413
    data->task.dynamic          = 0;
414 415

    thread_data = &data->thread;
416
    thread_data->team           = NULL;
417
    thread_data->task           = &data->task;
418
    thread_data->thread_num     = 0;
419
    thread_data->parallel       = FALSE;
420
    thread_data->fork_threads   = 0;
421
    thread_data->single         = 1;
422
    thread_data->section        = 1;
423 424
    thread_data->dynamic        = 1;
    thread_data->dynamic_type   = 0;
425 426 427 428 429 430 431 432 433 434 435 436 437 438

    vcomp_set_thread_data(thread_data);
    return thread_data;
}

static void vcomp_free_thread_data(void)
{
    struct vcomp_thread_data *thread_data = vcomp_get_thread_data();
    if (!thread_data) return;

    HeapFree(GetProcessHeap(), 0, thread_data);
    vcomp_set_thread_data(NULL);
}

439 440 441 442 443 444 445 446 447 448 449
void CDECL _vcomp_atomic_add_i1(char *dest, char val)
{
    interlocked_xchg_add8(dest, val);
}

void CDECL _vcomp_atomic_and_i1(char *dest, char val)
{
    char old;
    do old = *dest; while (interlocked_cmpxchg8(dest, old & val, old) != old);
}

450
void CDECL _vcomp_atomic_div_i1(signed char *dest, signed char val)
451
{
452 453
    signed char old;
    do old = *dest; while ((signed char)interlocked_cmpxchg8((char *)dest, old / val, old) != old);
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
}

void CDECL _vcomp_atomic_div_ui1(unsigned char *dest, unsigned char val)
{
    unsigned char old;
    do old = *dest; while ((unsigned char)interlocked_cmpxchg8((char *)dest, old / val, old) != old);
}

void CDECL _vcomp_atomic_mul_i1(char *dest, char val)
{
    char old;
    do old = *dest; while (interlocked_cmpxchg8(dest, old * val, old) != old);
}

void CDECL _vcomp_atomic_or_i1(char *dest, char val)
{
    char old;
    do old = *dest; while (interlocked_cmpxchg8(dest, old | val, old) != old);
}

void CDECL _vcomp_atomic_shl_i1(char *dest, unsigned int val)
{
    char old;
    do old = *dest; while (interlocked_cmpxchg8(dest, old << val, old) != old);
}

480
void CDECL _vcomp_atomic_shr_i1(signed char *dest, unsigned int val)
481
{
482 483
    signed char old;
    do old = *dest; while ((signed char)interlocked_cmpxchg8((char *)dest, old >> val, old) != old);
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
}

void CDECL _vcomp_atomic_shr_ui1(unsigned char *dest, unsigned int val)
{
    unsigned char old;
    do old = *dest; while ((unsigned char)interlocked_cmpxchg8((char *)dest, old >> val, old) != old);
}

void CDECL _vcomp_atomic_sub_i1(char *dest, char val)
{
    interlocked_xchg_add8(dest, -val);
}

void CDECL _vcomp_atomic_xor_i1(char *dest, char val)
{
    char old;
    do old = *dest; while (interlocked_cmpxchg8(dest, old ^ val, old) != old);
}

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
static void CDECL _vcomp_atomic_bool_and_i1(char *dest, char val)
{
    char old;
    do old = *dest; while (interlocked_cmpxchg8(dest, old && val, old) != old);
}

static void CDECL _vcomp_atomic_bool_or_i1(char *dest, char val)
{
    char old;
    do old = *dest; while (interlocked_cmpxchg8(dest, old ? old : (val != 0), old) != old);
}

void CDECL _vcomp_reduction_i1(unsigned int flags, char *dest, char val)
{
    static void (CDECL * const funcs[])(char *, char) =
    {
        _vcomp_atomic_add_i1,
        _vcomp_atomic_add_i1,
        _vcomp_atomic_mul_i1,
        _vcomp_atomic_and_i1,
        _vcomp_atomic_or_i1,
        _vcomp_atomic_xor_i1,
        _vcomp_atomic_bool_and_i1,
        _vcomp_atomic_bool_or_i1,
    };
    unsigned int op = (flags >> 8) & 0xf;
529
    op = min(op, ARRAY_SIZE(funcs) - 1);
530 531 532
    funcs[op](dest, val);
}

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
void CDECL _vcomp_atomic_add_i2(short *dest, short val)
{
    interlocked_xchg_add16(dest, val);
}

void CDECL _vcomp_atomic_and_i2(short *dest, short val)
{
    short old;
    do old = *dest; while (interlocked_cmpxchg16(dest, old & val, old) != old);
}

void CDECL _vcomp_atomic_div_i2(short *dest, short val)
{
    short old;
    do old = *dest; while (interlocked_cmpxchg16(dest, old / val, old) != old);
}

void CDECL _vcomp_atomic_div_ui2(unsigned short *dest, unsigned short val)
{
    unsigned short old;
    do old = *dest; while ((unsigned short)interlocked_cmpxchg16((short *)dest, old / val, old) != old);
}

void CDECL _vcomp_atomic_mul_i2(short *dest, short val)
{
    short old;
    do old = *dest; while (interlocked_cmpxchg16(dest, old * val, old) != old);
}

void CDECL _vcomp_atomic_or_i2(short *dest, short val)
{
    short old;
    do old = *dest; while (interlocked_cmpxchg16(dest, old | val, old) != old);
}

void CDECL _vcomp_atomic_shl_i2(short *dest, unsigned int val)
{
    short old;
    do old = *dest; while (interlocked_cmpxchg16(dest, old << val, old) != old);
}

void CDECL _vcomp_atomic_shr_i2(short *dest, unsigned int val)
{
    short old;
    do old = *dest; while (interlocked_cmpxchg16(dest, old >> val, old) != old);
}

void CDECL _vcomp_atomic_shr_ui2(unsigned short *dest, unsigned int val)
{
    unsigned short old;
    do old = *dest; while ((unsigned short)interlocked_cmpxchg16((short *)dest, old >> val, old) != old);
}

void CDECL _vcomp_atomic_sub_i2(short *dest, short val)
{
    interlocked_xchg_add16(dest, -val);
}

void CDECL _vcomp_atomic_xor_i2(short *dest, short val)
{
    short old;
    do old = *dest; while (interlocked_cmpxchg16(dest, old ^ val, old) != old);
}

597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
static void CDECL _vcomp_atomic_bool_and_i2(short *dest, short val)
{
    short old;
    do old = *dest; while (interlocked_cmpxchg16(dest, old && val, old) != old);
}

static void CDECL _vcomp_atomic_bool_or_i2(short *dest, short val)
{
    short old;
    do old = *dest; while (interlocked_cmpxchg16(dest, old ? old : (val != 0), old) != old);
}

void CDECL _vcomp_reduction_i2(unsigned int flags, short *dest, short val)
{
    static void (CDECL * const funcs[])(short *, short) =
    {
        _vcomp_atomic_add_i2,
        _vcomp_atomic_add_i2,
        _vcomp_atomic_mul_i2,
        _vcomp_atomic_and_i2,
        _vcomp_atomic_or_i2,
        _vcomp_atomic_xor_i2,
        _vcomp_atomic_bool_and_i2,
        _vcomp_atomic_bool_or_i2,
    };
    unsigned int op = (flags >> 8) & 0xf;
623
    op = min(op, ARRAY_SIZE(funcs) - 1);
624 625 626
    funcs[op](dest, val);
}

627 628
void CDECL _vcomp_atomic_add_i4(int *dest, int val)
{
629
    InterlockedExchangeAdd((LONG *)dest, val);
630 631 632 633 634
}

void CDECL _vcomp_atomic_and_i4(int *dest, int val)
{
    int old;
635
    do old = *dest; while (InterlockedCompareExchange((LONG *)dest, old & val, old) != old);
636 637 638 639 640
}

void CDECL _vcomp_atomic_div_i4(int *dest, int val)
{
    int old;
641
    do old = *dest; while (InterlockedCompareExchange((LONG *)dest, old / val, old) != old);
642 643 644 645 646
}

void CDECL _vcomp_atomic_div_ui4(unsigned int *dest, unsigned int val)
{
    unsigned int old;
647
    do old = *dest; while (InterlockedCompareExchange((LONG *)dest, old / val, old) != old);
648 649 650 651 652
}

void CDECL _vcomp_atomic_mul_i4(int *dest, int val)
{
    int old;
653
    do old = *dest; while (InterlockedCompareExchange((LONG *)dest, old * val, old) != old);
654 655 656 657 658
}

void CDECL _vcomp_atomic_or_i4(int *dest, int val)
{
    int old;
659
    do old = *dest; while (InterlockedCompareExchange((LONG *)dest, old | val, old) != old);
660 661 662 663 664
}

void CDECL _vcomp_atomic_shl_i4(int *dest, int val)
{
    int old;
665
    do old = *dest; while (InterlockedCompareExchange((LONG *)dest, old << val, old) != old);
666 667 668 669 670
}

void CDECL _vcomp_atomic_shr_i4(int *dest, int val)
{
    int old;
671
    do old = *dest; while (InterlockedCompareExchange((LONG *)dest, old >> val, old) != old);
672 673 674 675 676
}

void CDECL _vcomp_atomic_shr_ui4(unsigned int *dest, unsigned int val)
{
    unsigned int old;
677
    do old = *dest; while (InterlockedCompareExchange((LONG *)dest, old >> val, old) != old);
678 679 680 681
}

void CDECL _vcomp_atomic_sub_i4(int *dest, int val)
{
682
    InterlockedExchangeAdd((LONG *)dest, -val);
683 684 685 686 687
}

void CDECL _vcomp_atomic_xor_i4(int *dest, int val)
{
    int old;
688
    do old = *dest; while (InterlockedCompareExchange((LONG *)dest, old ^ val, old) != old);
689 690
}

691 692 693
static void CDECL _vcomp_atomic_bool_and_i4(int *dest, int val)
{
    int old;
694
    do old = *dest; while (InterlockedCompareExchange((LONG *)dest, old && val, old) != old);
695 696 697 698 699
}

static void CDECL _vcomp_atomic_bool_or_i4(int *dest, int val)
{
    int old;
700
    do old = *dest; while (InterlockedCompareExchange((LONG *)dest, old ? old : (val != 0), old) != old);
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
}

void CDECL _vcomp_reduction_i4(unsigned int flags, int *dest, int val)
{
    static void (CDECL * const funcs[])(int *, int) =
    {
        _vcomp_atomic_add_i4,
        _vcomp_atomic_add_i4,
        _vcomp_atomic_mul_i4,
        _vcomp_atomic_and_i4,
        _vcomp_atomic_or_i4,
        _vcomp_atomic_xor_i4,
        _vcomp_atomic_bool_and_i4,
        _vcomp_atomic_bool_or_i4,
    };
    unsigned int op = (flags >> 8) & 0xf;
717
    op = min(op, ARRAY_SIZE(funcs) - 1);
718 719 720
    funcs[op](dest, val);
}

721 722 723
void CDECL _vcomp_atomic_add_i8(LONG64 *dest, LONG64 val)
{
    LONG64 old;
724
    do old = *dest; while (InterlockedCompareExchange64(dest, old + val, old) != old);
725 726 727 728 729
}

void CDECL _vcomp_atomic_and_i8(LONG64 *dest, LONG64 val)
{
    LONG64 old;
730
    do old = *dest; while (InterlockedCompareExchange64(dest, old & val, old) != old);
731 732 733 734 735
}

void CDECL _vcomp_atomic_div_i8(LONG64 *dest, LONG64 val)
{
    LONG64 old;
736
    do old = *dest; while (InterlockedCompareExchange64(dest, old / val, old) != old);
737 738 739 740 741
}

void CDECL _vcomp_atomic_div_ui8(ULONG64 *dest, ULONG64 val)
{
    ULONG64 old;
742
    do old = *dest; while (InterlockedCompareExchange64((LONG64 *)dest, old / val, old) != old);
743 744 745 746 747
}

void CDECL _vcomp_atomic_mul_i8(LONG64 *dest, LONG64 val)
{
    LONG64 old;
748
    do old = *dest; while (InterlockedCompareExchange64(dest, old * val, old) != old);
749 750 751 752 753
}

void CDECL _vcomp_atomic_or_i8(LONG64 *dest, LONG64 val)
{
    LONG64 old;
754
    do old = *dest; while (InterlockedCompareExchange64(dest, old | val, old) != old);
755 756 757 758 759
}

void CDECL _vcomp_atomic_shl_i8(LONG64 *dest, unsigned int val)
{
    LONG64 old;
760
    do old = *dest; while (InterlockedCompareExchange64(dest, old << val, old) != old);
761 762 763 764 765
}

void CDECL _vcomp_atomic_shr_i8(LONG64 *dest, unsigned int val)
{
    LONG64 old;
766
    do old = *dest; while (InterlockedCompareExchange64(dest, old >> val, old) != old);
767 768 769 770 771
}

void CDECL _vcomp_atomic_shr_ui8(ULONG64 *dest, unsigned int val)
{
    ULONG64 old;
772
    do old = *dest; while (InterlockedCompareExchange64((LONG64 *)dest, old >> val, old) != old);
773 774 775 776 777
}

void CDECL _vcomp_atomic_sub_i8(LONG64 *dest, LONG64 val)
{
    LONG64 old;
778
    do old = *dest; while (InterlockedCompareExchange64(dest, old - val, old) != old);
779 780 781 782 783
}

void CDECL _vcomp_atomic_xor_i8(LONG64 *dest, LONG64 val)
{
    LONG64 old;
784
    do old = *dest; while (InterlockedCompareExchange64(dest, old ^ val, old) != old);
785 786
}

787 788 789
static void CDECL _vcomp_atomic_bool_and_i8(LONG64 *dest, LONG64 val)
{
    LONG64 old;
790
    do old = *dest; while (InterlockedCompareExchange64(dest, old && val, old) != old);
791 792 793 794 795
}

static void CDECL _vcomp_atomic_bool_or_i8(LONG64 *dest, LONG64 val)
{
    LONG64 old;
796
    do old = *dest; while (InterlockedCompareExchange64(dest, old ? old : (val != 0), old) != old);
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
}

void CDECL _vcomp_reduction_i8(unsigned int flags, LONG64 *dest, LONG64 val)
{
    static void (CDECL * const funcs[])(LONG64 *, LONG64) =
    {
        _vcomp_atomic_add_i8,
        _vcomp_atomic_add_i8,
        _vcomp_atomic_mul_i8,
        _vcomp_atomic_and_i8,
        _vcomp_atomic_or_i8,
        _vcomp_atomic_xor_i8,
        _vcomp_atomic_bool_and_i8,
        _vcomp_atomic_bool_or_i8,
    };
    unsigned int op = (flags >> 8) & 0xf;
813
    op = min(op, ARRAY_SIZE(funcs) - 1);
814 815 816
    funcs[op](dest, val);
}

817 818 819 820 821 822 823 824
void CDECL _vcomp_atomic_add_r4(float *dest, float val)
{
    int old, new;
    do
    {
        old = *(int *)dest;
        *(float *)&new = *(float *)&old + val;
    }
825
    while (InterlockedCompareExchange((LONG *)dest, new, old) != old);
826 827 828 829 830 831 832 833 834 835
}

void CDECL _vcomp_atomic_div_r4(float *dest, float val)
{
    int old, new;
    do
    {
        old = *(int *)dest;
        *(float *)&new = *(float *)&old / val;
    }
836
    while (InterlockedCompareExchange((LONG *)dest, new, old) != old);
837 838 839 840 841 842 843 844 845 846
}

void CDECL _vcomp_atomic_mul_r4(float *dest, float val)
{
    int old, new;
    do
    {
        old = *(int *)dest;
        *(float *)&new = *(float *)&old * val;
    }
847
    while (InterlockedCompareExchange((LONG *)dest, new, old) != old);
848 849 850 851 852 853 854 855 856 857
}

void CDECL _vcomp_atomic_sub_r4(float *dest, float val)
{
    int old, new;
    do
    {
        old = *(int *)dest;
        *(float *)&new = *(float *)&old - val;
    }
858
    while (InterlockedCompareExchange((LONG *)dest, new, old) != old);
859 860
}

861 862 863 864 865 866 867 868
static void CDECL _vcomp_atomic_bool_and_r4(float *dest, float val)
{
    int old, new;
    do
    {
        old = *(int *)dest;
        *(float *)&new = (*(float *)&old != 0.0) ? (val != 0.0) : 0.0;
    }
869
    while (InterlockedCompareExchange((LONG *)dest, new, old) != old);
870 871 872 873 874 875 876 877 878 879
}

static void CDECL _vcomp_atomic_bool_or_r4(float *dest, float val)
{
    int old, new;
    do
    {
        old = *(int *)dest;
        *(float *)&new = (*(float *)&old != 0.0) ? *(float *)&old : (val != 0.0);
    }
880
    while (InterlockedCompareExchange((LONG *)dest, new, old) != old);
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
}

void CDECL _vcomp_reduction_r4(unsigned int flags, float *dest, float val)
{
    static void (CDECL * const funcs[])(float *, float) =
    {
        _vcomp_atomic_add_r4,
        _vcomp_atomic_add_r4,
        _vcomp_atomic_mul_r4,
        _vcomp_atomic_bool_or_r4,
        _vcomp_atomic_bool_or_r4,
        _vcomp_atomic_bool_or_r4,
        _vcomp_atomic_bool_and_r4,
        _vcomp_atomic_bool_or_r4,
    };
    unsigned int op = (flags >> 8) & 0xf;
897
    op = min(op, ARRAY_SIZE(funcs) - 1);
898 899 900
    funcs[op](dest, val);
}

901 902 903 904 905 906 907 908
void CDECL _vcomp_atomic_add_r8(double *dest, double val)
{
    LONG64 old, new;
    do
    {
        old = *(LONG64 *)dest;
        *(double *)&new = *(double *)&old + val;
    }
909
    while (InterlockedCompareExchange64((LONG64 *)dest, new, old) != old);
910 911 912 913 914 915 916 917 918 919
}

void CDECL _vcomp_atomic_div_r8(double *dest, double val)
{
    LONG64 old, new;
    do
    {
        old = *(LONG64 *)dest;
        *(double *)&new = *(double *)&old / val;
    }
920
    while (InterlockedCompareExchange64((LONG64 *)dest, new, old) != old);
921 922 923 924 925 926 927 928 929 930
}

void CDECL _vcomp_atomic_mul_r8(double *dest, double val)
{
    LONG64 old, new;
    do
    {
        old = *(LONG64 *)dest;
        *(double *)&new = *(double *)&old * val;
    }
931
    while (InterlockedCompareExchange64((LONG64 *)dest, new, old) != old);
932 933 934 935 936 937 938 939 940 941
}

void CDECL _vcomp_atomic_sub_r8(double *dest, double val)
{
    LONG64 old, new;
    do
    {
        old = *(LONG64 *)dest;
        *(double *)&new = *(double *)&old - val;
    }
942
    while (InterlockedCompareExchange64((LONG64 *)dest, new, old) != old);
943 944
}

945 946 947 948 949 950 951 952
static void CDECL _vcomp_atomic_bool_and_r8(double *dest, double val)
{
    LONG64 old, new;
    do
    {
        old = *(LONG64 *)dest;
        *(double *)&new = (*(double *)&old != 0.0) ? (val != 0.0) : 0.0;
    }
953
    while (InterlockedCompareExchange64((LONG64 *)dest, new, old) != old);
954 955 956 957 958 959 960 961 962 963
}

static void CDECL _vcomp_atomic_bool_or_r8(double *dest, double val)
{
    LONG64 old, new;
    do
    {
        old = *(LONG64 *)dest;
        *(double *)&new = (*(double *)&old != 0.0) ? *(double *)&old : (val != 0.0);
    }
964
    while (InterlockedCompareExchange64((LONG64 *)dest, new, old) != old);
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
}

void CDECL _vcomp_reduction_r8(unsigned int flags, double *dest, double val)
{
    static void (CDECL * const funcs[])(double *, double) =
    {
        _vcomp_atomic_add_r8,
        _vcomp_atomic_add_r8,
        _vcomp_atomic_mul_r8,
        _vcomp_atomic_bool_or_r8,
        _vcomp_atomic_bool_or_r8,
        _vcomp_atomic_bool_or_r8,
        _vcomp_atomic_bool_and_r8,
        _vcomp_atomic_bool_or_r8,
    };
    unsigned int op = (flags >> 8) & 0xf;
981
    op = min(op, ARRAY_SIZE(funcs) - 1);
982 983 984
    funcs[op](dest, val);
}

985 986 987 988 989 990
int CDECL omp_get_dynamic(void)
{
    TRACE("stub\n");
    return 0;
}

991 992
int CDECL omp_get_max_threads(void)
{
993 994
    TRACE("()\n");
    return vcomp_max_threads;
995 996
}

997 998 999
int CDECL omp_get_nested(void)
{
    TRACE("stub\n");
1000
    return vcomp_nested_fork;
1001 1002
}

1003 1004
int CDECL omp_get_num_procs(void)
{
1005 1006
    TRACE("\n");
    return vcomp_num_procs;
1007 1008
}

1009 1010
int CDECL omp_get_num_threads(void)
{
1011 1012 1013
    struct vcomp_team_data *team_data = vcomp_init_thread_data()->team;
    TRACE("()\n");
    return team_data ? team_data->num_threads : 1;
1014 1015
}

1016 1017
int CDECL omp_get_thread_num(void)
{
1018 1019
    TRACE("()\n");
    return vcomp_init_thread_data()->thread_num;
1020 1021
}

1022 1023 1024 1025 1026 1027
int CDECL _vcomp_get_thread_num(void)
{
    TRACE("()\n");
    return vcomp_init_thread_data()->thread_num;
}

1028 1029 1030 1031 1032 1033
/* Time in seconds since "some time in the past" */
double CDECL omp_get_wtime(void)
{
    return GetTickCount() / 1000.0;
}

1034 1035 1036 1037 1038
void CDECL omp_set_dynamic(int val)
{
    TRACE("(%d): stub\n", val);
}

1039 1040
void CDECL omp_set_nested(int nested)
{
1041 1042
    TRACE("(%d)\n", nested);
    vcomp_nested_fork = (nested != 0);
1043 1044
}

1045 1046
void CDECL omp_set_num_threads(int num_threads)
{
1047 1048 1049
    TRACE("(%d)\n", num_threads);
    if (num_threads >= 1)
        vcomp_num_threads = num_threads;
1050 1051
}

1052 1053 1054 1055 1056
void CDECL _vcomp_flush(void)
{
    TRACE("(): stub\n");
}

1057 1058
void CDECL _vcomp_barrier(void)
{
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
    struct vcomp_team_data *team_data = vcomp_init_thread_data()->team;

    TRACE("()\n");

    if (!team_data)
        return;

    EnterCriticalSection(&vcomp_section);
    if (++team_data->barrier_count >= team_data->num_threads)
    {
        team_data->barrier++;
        team_data->barrier_count = 0;
        WakeAllConditionVariable(&team_data->cond);
    }
    else
    {
        unsigned int barrier = team_data->barrier;
        while (team_data->barrier == barrier)
            SleepConditionVariableCS(&team_data->cond, &vcomp_section, INFINITE);
    }
    LeaveCriticalSection(&vcomp_section);
1080 1081
}

1082 1083
void CDECL _vcomp_set_num_threads(int num_threads)
{
1084 1085 1086
    TRACE("(%d)\n", num_threads);
    if (num_threads >= 1)
        vcomp_init_thread_data()->fork_threads = num_threads;
1087 1088
}

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
int CDECL _vcomp_master_begin(void)
{
    TRACE("()\n");
    return !vcomp_init_thread_data()->thread_num;
}

void CDECL _vcomp_master_end(void)
{
    TRACE("()\n");
    /* nothing to do here */
}

1101 1102
int CDECL _vcomp_single_begin(int flags)
{
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
    struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
    struct vcomp_task_data *task_data = thread_data->task;
    int ret = FALSE;

    TRACE("(%x): semi-stub\n", flags);

    EnterCriticalSection(&vcomp_section);
    thread_data->single++;
    if ((int)(thread_data->single - task_data->single) > 0)
    {
        task_data->single = thread_data->single;
        ret = TRUE;
    }
    LeaveCriticalSection(&vcomp_section);

    return ret;
1119 1120 1121 1122
}

void CDECL _vcomp_single_end(void)
{
1123 1124
    TRACE("()\n");
    /* nothing to do here */
1125 1126
}

1127 1128
void CDECL _vcomp_sections_init(int n)
{
1129 1130
    struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
    struct vcomp_task_data *task_data = thread_data->task;
1131 1132 1133 1134 1135

    TRACE("(%d)\n", n);

    EnterCriticalSection(&vcomp_section);
    thread_data->section++;
1136
    if ((int)(thread_data->section - task_data->section) > 0)
1137
    {
1138 1139 1140
        task_data->section       = thread_data->section;
        task_data->num_sections  = n;
        task_data->section_index = 0;
1141 1142 1143 1144 1145 1146
    }
    LeaveCriticalSection(&vcomp_section);
}

int CDECL _vcomp_sections_next(void)
{
1147 1148
    struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
    struct vcomp_task_data *task_data = thread_data->task;
1149 1150 1151 1152 1153
    int i = -1;

    TRACE("()\n");

    EnterCriticalSection(&vcomp_section);
1154 1155
    if (thread_data->section == task_data->section &&
        task_data->section_index != task_data->num_sections)
1156
    {
1157
        i = task_data->section_index++;
1158 1159 1160 1161 1162
    }
    LeaveCriticalSection(&vcomp_section);
    return i;
}

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
void CDECL _vcomp_for_static_simple_init(unsigned int first, unsigned int last, int step,
                                         BOOL increment, unsigned int *begin, unsigned int *end)
{
    unsigned int iterations, per_thread, remaining;
    struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
    struct vcomp_team_data *team_data = thread_data->team;
    int num_threads = team_data ? team_data->num_threads : 1;
    int thread_num = thread_data->thread_num;

    TRACE("(%u, %u, %d, %u, %p, %p)\n", first, last, step, increment, begin, end);

    if (num_threads == 1)
    {
        *begin = first;
        *end   = last;
        return;
    }

    if (step <= 0)
    {
        *begin = 0;
        *end   = increment ? -1 : 1;
        return;
    }

    if (increment)
        iterations = 1 + (last - first) / step;
    else
    {
        iterations = 1 + (first - last) / step;
        step *= -1;
    }

    per_thread = iterations / num_threads;
    remaining  = iterations - per_thread * num_threads;

    if (thread_num < remaining)
        per_thread++;
    else if (per_thread)
        first += remaining * step;
    else
    {
        *begin = first;
        *end   = first - step;
        return;
    }

    *begin = first + per_thread * thread_num * step;
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
    *end   = *begin + (per_thread - 1) * step;
}

void CDECL _vcomp_for_static_simple_init_i8(ULONG64 first, ULONG64 last, LONG64 step,
                                         BOOL increment, ULONG64 *begin, ULONG64 *end)
{
    ULONG64 iterations, per_thread, remaining;
    struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
    struct vcomp_team_data *team_data = thread_data->team;
    int num_threads = team_data ? team_data->num_threads : 1;
    int thread_num = thread_data->thread_num;

    TRACE("(%s, %s, %s, %x, %p, %p)\n", wine_dbgstr_longlong(first), wine_dbgstr_longlong(last),
            wine_dbgstr_longlong(step), increment, begin, end);

    if (num_threads == 1)
    {
        *begin = first;
        *end   = last;
        return;
    }

    if (step <= 0)
    {
        *begin = 0;
        *end   = increment ? -1 : 1;
        return;
    }

    if (increment)
        iterations = 1 + (last - first) / step;
    else
    {
        iterations = 1 + (first - last) / step;
        step *= -1;
    }

    per_thread = iterations / num_threads;
    remaining  = iterations - per_thread * num_threads;

    if (thread_num < remaining)
        per_thread++;
    else if (per_thread)
        first += remaining * step;
    else
    {
        *begin = first;
        *end   = first - step;
        return;
    }

    *begin = first + per_thread * thread_num * step;
1263 1264 1265
    *end   = *begin + (per_thread - 1) * step;
}

1266 1267 1268 1269 1270 1271 1272 1273
void CDECL _vcomp_for_static_init(int first, int last, int step, int chunksize, unsigned int *loops,
                                  int *begin, int *end, int *next, int *lastchunk)
{
    unsigned int iterations, num_chunks, per_thread, remaining;
    struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
    struct vcomp_team_data *team_data = thread_data->team;
    int num_threads = team_data ? team_data->num_threads : 1;
    int thread_num = thread_data->thread_num;
1274
    int no_begin, no_lastchunk;
1275 1276 1277 1278

    TRACE("(%d, %d, %d, %d, %p, %p, %p, %p, %p)\n",
          first, last, step, chunksize, loops, begin, end, next, lastchunk);

1279 1280 1281 1282 1283 1284
    if (!begin)
    {
        begin = &no_begin;
        lastchunk = &no_lastchunk;
    }

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
    if (num_threads == 1 && chunksize != 1)
    {
        *loops      = 1;
        *begin      = first;
        *end        = last;
        *next       = 0;
        *lastchunk  = first;
        return;
    }

    if (first == last)
    {
        *loops = !thread_num;
        if (!thread_num)
        {
            *begin      = first;
            *end        = last;
            *next       = 0;
            *lastchunk  = first;
        }
        return;
    }

    if (step <= 0)
    {
        *loops = 0;
        return;
    }

    if (first < last)
        iterations = 1 + (last - first) / step;
    else
    {
        iterations = 1 + (first - last) / step;
        step *= -1;
    }

    if (chunksize < 1)
        chunksize = 1;

    num_chunks  = ((DWORD64)iterations + chunksize - 1) / chunksize;
    per_thread  = num_chunks / num_threads;
    remaining   = num_chunks - per_thread * num_threads;

1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
    *loops      = per_thread + (thread_num < remaining);
    *begin      = first + thread_num * chunksize * step;
    *end        = *begin + (chunksize - 1) * step;
    *next       = chunksize * num_threads * step;
    *lastchunk  = first + (num_chunks - 1) * chunksize * step;
}

void CDECL _vcomp_for_static_init_i8(LONG64 first, LONG64 last, LONG64 step, LONG64 chunksize, ULONG64 *loops,
                                     LONG64 *begin, LONG64 *end, LONG64 *next, LONG64 *lastchunk)
{
    ULONG64 iterations, num_chunks, per_thread, remaining;
    struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
    struct vcomp_team_data *team_data = thread_data->team;
    int num_threads = team_data ? team_data->num_threads : 1;
    int thread_num = thread_data->thread_num;
    LONG64 no_begin, no_lastchunk;

    TRACE("(%s, %s, %s, %s, %p, %p, %p, %p, %p)\n",
          wine_dbgstr_longlong(first), wine_dbgstr_longlong(last),
          wine_dbgstr_longlong(step), wine_dbgstr_longlong(chunksize),
          loops, begin, end, next, lastchunk);

    if (!begin)
    {
        begin = &no_begin;
        lastchunk = &no_lastchunk;
    }

    if (num_threads == 1 && chunksize != 1)
    {
        *loops      = 1;
        *begin      = first;
        *end        = last;
        *next       = 0;
        *lastchunk  = first;
        return;
    }

    if (first == last)
    {
        *loops = !thread_num;
        if (!thread_num)
        {
            *begin      = first;
            *end        = last;
            *next       = 0;
            *lastchunk  = first;
        }
        return;
    }

    if (step <= 0)
    {
        *loops = 0;
        return;
    }

    if (first < last)
        iterations = 1 + (last - first) / step;
    else
    {
        iterations = 1 + (first - last) / step;
        step *= -1;
    }

    if (chunksize < 1)
        chunksize = 1;

    num_chunks  = iterations / chunksize;
    if (iterations % chunksize) num_chunks++;
    per_thread  = num_chunks / num_threads;
    remaining   = num_chunks - per_thread * num_threads;

1402 1403 1404 1405 1406 1407 1408
    *loops      = per_thread + (thread_num < remaining);
    *begin      = first + thread_num * chunksize * step;
    *end        = *begin + (chunksize - 1) * step;
    *next       = chunksize * num_threads * step;
    *lastchunk  = first + (num_chunks - 1) * chunksize * step;
}

1409 1410 1411 1412 1413 1414
void CDECL _vcomp_for_static_end(void)
{
    TRACE("()\n");
    /* nothing to do here */
}

1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
void CDECL _vcomp_for_dynamic_init(unsigned int flags, unsigned int first, unsigned int last,
                                   int step, unsigned int chunksize)
{
    unsigned int iterations, per_thread, remaining;
    struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
    struct vcomp_team_data *team_data = thread_data->team;
    struct vcomp_task_data *task_data = thread_data->task;
    int num_threads = team_data ? team_data->num_threads : 1;
    int thread_num = thread_data->thread_num;
    unsigned int type = flags & ~VCOMP_DYNAMIC_FLAGS_INCREMENT;

    TRACE("(%u, %u, %u, %d, %u)\n", flags, first, last, step, chunksize);

    if (step <= 0)
    {
        thread_data->dynamic_type = 0;
        return;
    }

    if (flags & VCOMP_DYNAMIC_FLAGS_INCREMENT)
        iterations = 1 + (last - first) / step;
    else
    {
        iterations = 1 + (first - last) / step;
        step *= -1;
    }

    if (type == VCOMP_DYNAMIC_FLAGS_STATIC)
    {
        per_thread = iterations / num_threads;
        remaining  = iterations - per_thread * num_threads;

        if (thread_num < remaining)
            per_thread++;
        else if (per_thread)
            first += remaining * step;
        else
        {
            thread_data->dynamic_type = 0;
            return;
        }

        thread_data->dynamic_type   = VCOMP_DYNAMIC_FLAGS_STATIC;
        thread_data->dynamic_begin  = first + per_thread * thread_num * step;
        thread_data->dynamic_end    = thread_data->dynamic_begin + (per_thread - 1) * step;
    }
    else
    {
        if (type != VCOMP_DYNAMIC_FLAGS_CHUNKED &&
            type != VCOMP_DYNAMIC_FLAGS_GUIDED)
        {
            FIXME("unsupported flags %u\n", flags);
            type = VCOMP_DYNAMIC_FLAGS_GUIDED;
        }

        EnterCriticalSection(&vcomp_section);
        thread_data->dynamic++;
        thread_data->dynamic_type = type;
        if ((int)(thread_data->dynamic - task_data->dynamic) > 0)
        {
            task_data->dynamic              = thread_data->dynamic;
            task_data->dynamic_first        = first;
            task_data->dynamic_last         = last;
            task_data->dynamic_iterations   = iterations;
            task_data->dynamic_step         = step;
            task_data->dynamic_chunksize    = chunksize;
        }
        LeaveCriticalSection(&vcomp_section);
    }
}

int CDECL _vcomp_for_dynamic_next(unsigned int *begin, unsigned int *end)
{
    struct vcomp_thread_data *thread_data = vcomp_init_thread_data();
    struct vcomp_task_data *task_data = thread_data->task;
    struct vcomp_team_data *team_data = thread_data->team;
    int num_threads = team_data ? team_data->num_threads : 1;

    TRACE("(%p, %p)\n", begin, end);

    if (thread_data->dynamic_type == VCOMP_DYNAMIC_FLAGS_STATIC)
    {
        *begin = thread_data->dynamic_begin;
        *end   = thread_data->dynamic_end;
        thread_data->dynamic_type = 0;
        return 1;
    }
    else if (thread_data->dynamic_type == VCOMP_DYNAMIC_FLAGS_CHUNKED ||
             thread_data->dynamic_type == VCOMP_DYNAMIC_FLAGS_GUIDED)
    {
        unsigned int iterations = 0;
        EnterCriticalSection(&vcomp_section);
        if (thread_data->dynamic == task_data->dynamic &&
            task_data->dynamic_iterations != 0)
        {
            iterations = min(task_data->dynamic_iterations, task_data->dynamic_chunksize);
            if (thread_data->dynamic_type == VCOMP_DYNAMIC_FLAGS_GUIDED &&
                task_data->dynamic_iterations > num_threads * task_data->dynamic_chunksize)
            {
                iterations = (task_data->dynamic_iterations + num_threads - 1) / num_threads;
            }
            *begin = task_data->dynamic_first;
            *end   = task_data->dynamic_first + (iterations - 1) * task_data->dynamic_step;
            task_data->dynamic_iterations -= iterations;
            task_data->dynamic_first      += iterations * task_data->dynamic_step;
            if (!task_data->dynamic_iterations)
                *end = task_data->dynamic_last;
        }
        LeaveCriticalSection(&vcomp_section);
        return iterations != 0;
    }

    return 0;
}

1530 1531 1532 1533 1534 1535
int CDECL omp_in_parallel(void)
{
    TRACE("()\n");
    return vcomp_init_thread_data()->parallel;
}

1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
static DWORD WINAPI _vcomp_fork_worker(void *param)
{
    struct vcomp_thread_data *thread_data = param;
    vcomp_set_thread_data(thread_data);

    TRACE("starting worker thread for %p\n", thread_data);

    EnterCriticalSection(&vcomp_section);
    for (;;)
    {
        struct vcomp_team_data *team = thread_data->team;
        if (team != NULL)
        {
            LeaveCriticalSection(&vcomp_section);
1550
            _vcomp_fork_call_wrapper(team->wrapper, team->nargs, ptr_from_va_list(team->valist));
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
            EnterCriticalSection(&vcomp_section);

            thread_data->team = NULL;
            list_remove(&thread_data->entry);
            list_add_tail(&vcomp_idle_threads, &thread_data->entry);
            if (++team->finished_threads >= team->num_threads)
                WakeAllConditionVariable(&team->cond);
        }

        if (!SleepConditionVariableCS(&thread_data->cond, &vcomp_section, 5000) &&
            GetLastError() == ERROR_TIMEOUT && !thread_data->team)
        {
            break;
        }
    }
    list_remove(&thread_data->entry);
    LeaveCriticalSection(&vcomp_section);

    TRACE("terminating worker thread for %p\n", thread_data);

    HeapFree(GetProcessHeap(), 0, thread_data);
    vcomp_set_thread_data(NULL);
    FreeLibraryAndExitThread(vcomp_module, 0);
    return 0;
}

1577 1578
void WINAPIV _vcomp_fork(BOOL ifval, int nargs, void *wrapper, ...)
{
1579 1580 1581
    struct vcomp_thread_data *prev_thread_data = vcomp_init_thread_data();
    struct vcomp_thread_data thread_data;
    struct vcomp_team_data team_data;
1582
    struct vcomp_task_data task_data;
1583
    int num_threads;
1584 1585 1586

    TRACE("(%d, %d, %p, ...)\n", ifval, nargs, wrapper);

1587 1588 1589
    if (prev_thread_data->parallel && !vcomp_nested_fork)
        ifval = FALSE;

1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
    if (!ifval)
        num_threads = 1;
    else if (prev_thread_data->fork_threads)
        num_threads = prev_thread_data->fork_threads;
    else
        num_threads = vcomp_num_threads;

    InitializeConditionVariable(&team_data.cond);
    team_data.num_threads       = 1;
    team_data.finished_threads  = 0;
    team_data.nargs             = nargs;
    team_data.wrapper           = wrapper;
1602
    va_start(team_data.valist, wrapper);
1603 1604
    team_data.barrier           = 0;
    team_data.barrier_count     = 0;
1605

1606
    task_data.single            = 0;
1607
    task_data.section           = 0;
1608
    task_data.dynamic           = 0;
1609 1610

    thread_data.team            = &team_data;
1611
    thread_data.task            = &task_data;
1612
    thread_data.thread_num      = 0;
1613
    thread_data.parallel        = ifval || prev_thread_data->parallel;
1614
    thread_data.fork_threads    = 0;
1615
    thread_data.single          = 1;
1616
    thread_data.section         = 1;
1617
    thread_data.dynamic         = 1;
1618
    thread_data.dynamic_type    = 0;
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
    list_init(&thread_data.entry);
    InitializeConditionVariable(&thread_data.cond);

    if (num_threads > 1)
    {
        struct list *ptr;
        EnterCriticalSection(&vcomp_section);

        /* reuse existing threads (if any) */
        while (team_data.num_threads < num_threads && (ptr = list_head(&vcomp_idle_threads)))
        {
            struct vcomp_thread_data *data = LIST_ENTRY(ptr, struct vcomp_thread_data, entry);
            data->team          = &team_data;
1632
            data->task          = &task_data;
1633
            data->thread_num    = team_data.num_threads++;
1634
            data->parallel      = thread_data.parallel;
1635
            data->fork_threads  = 0;
1636
            data->single        = 1;
1637
            data->section       = 1;
1638
            data->dynamic       = 1;
1639
            data->dynamic_type  = 0;
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
            list_remove(&data->entry);
            list_add_tail(&thread_data.entry, &data->entry);
            WakeAllConditionVariable(&data->cond);
        }

        /* spawn additional threads */
        while (team_data.num_threads < num_threads)
        {
            struct vcomp_thread_data *data;
            HMODULE module;
            HANDLE thread;

            data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
            if (!data) break;

            data->team          = &team_data;
1656
            data->task          = &task_data;
1657
            data->thread_num    = team_data.num_threads;
1658
            data->parallel      = thread_data.parallel;
1659
            data->fork_threads  = 0;
1660
            data->single        = 1;
1661
            data->section       = 1;
1662
            data->dynamic       = 1;
1663
            data->dynamic_type  = 0;
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
            InitializeConditionVariable(&data->cond);

            thread = CreateThread(NULL, 0, _vcomp_fork_worker, data, 0, NULL);
            if (!thread)
            {
                HeapFree(GetProcessHeap(), 0, data);
                break;
            }

            GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
                               (const WCHAR *)vcomp_module, &module);
            team_data.num_threads++;
            list_add_tail(&thread_data.entry, &data->entry);
            CloseHandle(thread);
        }

        LeaveCriticalSection(&vcomp_section);
    }

    vcomp_set_thread_data(&thread_data);
1684
    _vcomp_fork_call_wrapper(team_data.wrapper, team_data.nargs, ptr_from_va_list(team_data.valist));
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
    vcomp_set_thread_data(prev_thread_data);
    prev_thread_data->fork_threads = 0;

    if (team_data.num_threads > 1)
    {
        EnterCriticalSection(&vcomp_section);

        team_data.finished_threads++;
        while (team_data.finished_threads < team_data.num_threads)
            SleepConditionVariableCS(&team_data.cond, &vcomp_section, INFINITE);

        LeaveCriticalSection(&vcomp_section);
        assert(list_empty(&thread_data.entry));
    }

1700
    va_end(team_data.valist);
1701 1702
}

1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
static CRITICAL_SECTION *alloc_critsect(void)
{
    CRITICAL_SECTION *critsect;
    if (!(critsect = HeapAlloc(GetProcessHeap(), 0, sizeof(*critsect))))
    {
        ERR("could not allocate critical section\n");
        ExitProcess(1);
    }

    InitializeCriticalSection(critsect);
    critsect->DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": critsect");
    return critsect;
}

static void destroy_critsect(CRITICAL_SECTION *critsect)
{
    if (!critsect) return;
    critsect->DebugInfo->Spare[0] = 0;
    DeleteCriticalSection(critsect);
    HeapFree(GetProcessHeap(), 0, critsect);
}

void CDECL omp_init_lock(omp_lock_t *lock)
{
    TRACE("(%p)\n", lock);
    *lock = alloc_critsect();
}

void CDECL omp_destroy_lock(omp_lock_t *lock)
{
    TRACE("(%p)\n", lock);
    destroy_critsect(*lock);
}

1737 1738 1739 1740
void CDECL omp_set_lock(omp_lock_t *lock)
{
    TRACE("(%p)\n", lock);

1741
    if (RtlIsCriticalSectionLockedByThread(*lock))
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
    {
        ERR("omp_set_lock called while holding lock %p\n", *lock);
        ExitProcess(1);
    }

    EnterCriticalSection(*lock);
}

void CDECL omp_unset_lock(omp_lock_t *lock)
{
    TRACE("(%p)\n", lock);
    LeaveCriticalSection(*lock);
}

int CDECL omp_test_lock(omp_lock_t *lock)
{
    TRACE("(%p)\n", lock);

1760
    if (RtlIsCriticalSectionLockedByThread(*lock))
1761 1762 1763 1764 1765
        return 0;

    return TryEnterCriticalSection(*lock);
}

1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
void CDECL omp_set_nest_lock(omp_nest_lock_t *lock)
{
    TRACE("(%p)\n", lock);
    EnterCriticalSection(*lock);
}

void CDECL omp_unset_nest_lock(omp_nest_lock_t *lock)
{
    TRACE("(%p)\n", lock);
    LeaveCriticalSection(*lock);
}

int CDECL omp_test_nest_lock(omp_nest_lock_t *lock)
{
    TRACE("(%p)\n", lock);
    return TryEnterCriticalSection(*lock) ? (*lock)->RecursionCount : 0;
}

1784 1785 1786 1787 1788 1789
void CDECL _vcomp_enter_critsect(CRITICAL_SECTION **critsect)
{
    TRACE("(%p)\n", critsect);

    if (!*critsect)
    {
1790
        CRITICAL_SECTION *new_critsect = alloc_critsect();
1791
        if (InterlockedCompareExchangePointer((void **)critsect, new_critsect, NULL) != NULL)
1792
            destroy_critsect(new_critsect);  /* someone beat us to it */
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
    }

    EnterCriticalSection(*critsect);
}

void CDECL _vcomp_leave_critsect(CRITICAL_SECTION *critsect)
{
    TRACE("(%p)\n", critsect);
    LeaveCriticalSection(critsect);
}

1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
static unsigned int get_step_count(int start, int end, int range_offset, int step)
{
    int range = end - start + step - range_offset;

    if (step < 0)
        return (unsigned)-range / -step;
    else
        return (unsigned)range / step;
}

static void CDECL c2vectparallel_wrapper(int start, int end, int step, int end_included, BOOL dynamic_distribution,
1815
        int volatile *dynamic_start, void *function, int nargs, va_list valist)
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
{
    void *wrapper_args[MAX_VECT_PARALLEL_CALLBACK_ARGS];
    unsigned int step_count, steps_per_call, remainder;
    int thread_count = omp_get_num_threads();
    int curr_start, curr_end, range_offset;
    int thread = _vcomp_get_thread_num();
    int step_sign;

    copy_va_list_data(&wrapper_args[2], valist, nargs - 2);

    step_sign = step > 0 ? 1 : -1;
    range_offset = step_sign * !end_included;

    if (dynamic_distribution)
    {
        int next_start, new_start, end_value;

        start = *dynamic_start;
        end_value = end + !!end_included * step;
        while (start != end_value)
        {
            step_count = get_step_count(start, end, range_offset, step);

            curr_end = start + (step_count + thread_count - 1) / thread_count * step
                    + range_offset;

            if ((curr_end - end) * step_sign > 0)
            {
                next_start = end_value;
                curr_end = end;
            }
            else
            {
                next_start = curr_end - range_offset;
                curr_end -= step;
            }

1853
            if ((new_start = InterlockedCompareExchange((LONG volatile*)dynamic_start, next_start, start)) != start)
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
            {
                start = new_start;
                continue;
            }

            wrapper_args[0] = (void *)(ULONG_PTR)start;
            wrapper_args[1] = (void *)(ULONG_PTR)curr_end;
            _vcomp_fork_call_wrapper(function, nargs, wrapper_args);
            start = *dynamic_start;
        }
        return;
    }

    step_count = get_step_count(start, end, range_offset, step);

    /* According to the tests native vcomp still makes extra calls
     * with empty range from excessive threads under certain conditions
     * for unclear reason. */
    if (thread >= step_count && (end_included || (step != 1 && step != -1)))
        return;

    steps_per_call = step_count / thread_count;
    remainder = step_count % thread_count;

    if (thread < remainder)
    {
        curr_start = thread * (steps_per_call + 1);
        curr_end = curr_start + steps_per_call + 1;
    }
    else if (thread < step_count)
    {
        curr_start = remainder + steps_per_call * thread;
        curr_end = curr_start + steps_per_call;
    }
    else
    {
        curr_start = curr_end = 0;
    }

    curr_start = start + curr_start * step;
    curr_end = start + (curr_end - 1) * step + range_offset;

    wrapper_args[0] = (void *)(ULONG_PTR)curr_start;
    wrapper_args[1] = (void *)(ULONG_PTR)curr_end;
    _vcomp_fork_call_wrapper(function, nargs, wrapper_args);
}

1901
void WINAPIV C2VectParallel(int start, int end, int step, BOOL end_included, int thread_count,
1902 1903 1904 1905 1906
        BOOL dynamic_distribution, void *function, int nargs, ...)
{
    struct vcomp_thread_data *thread_data;
    int volatile dynamic_start;
    int prev_thread_count;
1907
    va_list valist;
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920

    TRACE("start %d, end %d, step %d, end_included %d, thread_count %d, dynamic_distribution %#x,"
            " function %p, nargs %d.\n", start, end, step, end_included, thread_count,
            dynamic_distribution, function, nargs);

    if (nargs > MAX_VECT_PARALLEL_CALLBACK_ARGS)
    {
        FIXME("Number of arguments %u exceeds supported maximum %u"
                " (not calling the loop code, expect problems).\n",
                nargs, MAX_VECT_PARALLEL_CALLBACK_ARGS);
        return;
    }

1921
    va_start(valist, nargs);
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934

    /* This expression can result in integer overflow. According to the tests,
     * native vcomp runs the function as a single thread both for empty range
     * and (end - start) not fitting the integer range. */
    if ((step > 0 && end < start) || (step < 0 && end > start)
            || (end - start) / step < 2 || thread_count < 0)
    {
        void *wrapper_args[MAX_VECT_PARALLEL_CALLBACK_ARGS];

        wrapper_args[0] = (void *)(ULONG_PTR)start;
        wrapper_args[1] = (void *)(ULONG_PTR)end;
        copy_va_list_data(&wrapper_args[2], valist, nargs - 2);
        _vcomp_fork_call_wrapper(function, nargs, wrapper_args);
1935
        va_end(valist);
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
        return;
    }

    thread_data = vcomp_init_thread_data();
    prev_thread_count = thread_data->fork_threads;
    thread_data->fork_threads = thread_count;

    dynamic_start = start;

    _vcomp_fork(TRUE, 9, c2vectparallel_wrapper, start, end, step, end_included, dynamic_distribution,
            &dynamic_start, function, nargs, valist);

    thread_data->fork_threads = prev_thread_count;
1949
    va_end(valist);
1950 1951
}

1952
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
Austin English's avatar
Austin English committed
1953
{
1954
    TRACE("(%p, %ld, %p)\n", instance, reason, reserved);
Austin English's avatar
Austin English committed
1955

1956
    switch (reason)
Austin English's avatar
Austin English committed
1957 1958
    {
        case DLL_PROCESS_ATTACH:
1959 1960
        {
            SYSTEM_INFO sysinfo;
1961 1962 1963 1964 1965 1966

            if ((vcomp_context_tls = TlsAlloc()) == TLS_OUT_OF_INDEXES)
            {
                ERR("Failed to allocate TLS index\n");
                return FALSE;
            }
1967 1968

            GetSystemInfo(&sysinfo);
1969
            vcomp_module      = instance;
1970 1971
            vcomp_max_threads = sysinfo.dwNumberOfProcessors;
            vcomp_num_threads = sysinfo.dwNumberOfProcessors;
1972
            vcomp_num_procs   = sysinfo.dwNumberOfProcessors;
Austin English's avatar
Austin English committed
1973
            break;
1974
        }
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991

        case DLL_PROCESS_DETACH:
        {
            if (reserved) break;
            if (vcomp_context_tls != TLS_OUT_OF_INDEXES)
            {
                vcomp_free_thread_data();
                TlsFree(vcomp_context_tls);
            }
            break;
        }

        case DLL_THREAD_DETACH:
        {
            vcomp_free_thread_data();
            break;
        }
Austin English's avatar
Austin English committed
1992 1993 1994 1995
    }

    return TRUE;
}