nsprpub/pr/tests/yield.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/yield.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,56 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include <stdio.h>
    1.10 +#include "prthread.h"
    1.11 +#include "prinit.h"
    1.12 +#ifndef XP_OS2
    1.13 +#include "private/pprmisc.h"
    1.14 +#include <windows.h>
    1.15 +#else
    1.16 +#include "primpl.h"
    1.17 +#include <os2.h>
    1.18 +#endif
    1.19 +
    1.20 +#define THREADS 10
    1.21 +
    1.22 +
    1.23 +void 
    1.24 +threadmain(void *_id)
    1.25 +{
    1.26 +    int id = (int)_id;
    1.27 +    int index;
    1.28 +
    1.29 +    printf("thread %d alive\n", id);
    1.30 +    for (index=0; index<10; index++) {
    1.31 +        printf("thread %d yielding\n", id);
    1.32 +        PR_Sleep(0);
    1.33 +        printf("thread %d awake\n", id);
    1.34 +    }
    1.35 +    printf("thread %d dead\n", id);
    1.36 +
    1.37 +}
    1.38 +
    1.39 +int main(int argc, char **argv)
    1.40 +{
    1.41 +    int index;
    1.42 +    PRThread *a[THREADS];
    1.43 +
    1.44 +    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 5);
    1.45 +    PR_STDIO_INIT();
    1.46 +
    1.47 +    for (index=0; index<THREADS; index++) {
    1.48 +        a[index] = PR_CreateThread(PR_USER_THREAD,
    1.49 +                              threadmain,
    1.50 +                              (void *)index,
    1.51 +                              PR_PRIORITY_NORMAL,
    1.52 +                              index%2?PR_LOCAL_THREAD:PR_GLOBAL_THREAD,
    1.53 +                              PR_JOINABLE_THREAD,
    1.54 +                              0);
    1.55 +    }
    1.56 +    for(index=0; index<THREADS; index++)
    1.57 +        PR_JoinThread(a[index]);
    1.58 +    printf("main dying\n");
    1.59 +}

mercurial