Changeset 1386


Ignore:
Timestamp:
Apr 27, 2004, 6:25:53 PM (21 years ago)
Author:
bird
Message:

threads.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/emx/testcase/libc/miscinnotek/tls.c

    • Property cvs2svn:cvs-rev changed from 1.2 to 1.3
    r1385 r1386  
    2525 */
    2626
     27/*******************************************************************************
     28*   Defined Constants And Macros                                               *
     29*******************************************************************************/
     30#define TEST_THREADS 10
     31
    2732
    2833/*******************************************************************************
     
    3035*******************************************************************************/
    3136#include <InnoTekLIBC/thread.h>
    32 
     37#include <process.h>
    3338#include <stdio.h>
    34 
     39#include <386/builtin.h>
     40
     41/*******************************************************************************
     42*   Structures and Typedefs                                                    *
     43*******************************************************************************/
     44/** Generic argument structure for the thread functions. */
     45typedef struct ThreadArg
     46{
     47    int     iTLS;
     48    int     cErrors;
     49    int     tid;
     50    int     fDone;
     51} THREADARG, *PTHREADARG;
     52
     53void thread1(void *pvArg)
     54{
     55    PTHREADARG  pArg = (PTHREADARG)pvArg;
     56    int         i;
     57
     58    if (__libc_TLSGet(pArg->iTLS) != NULL)
     59    {
     60        printf("tls: error: tid %d: initial get failed on index %d\n",
     61               pArg->tid, pArg->iTLS);
     62        pArg->cErrors++;
     63    }
     64
     65    for (i = 0; i < 100; i++)
     66    {
     67        if (__libc_TLSSet(pArg->iTLS, pvArg))
     68        {
     69            printf("tls: error: tid %d: set failed on index %d, i=%d\n",
     70                   pArg->tid, pArg->iTLS, i);
     71            pArg->cErrors++;
     72        }
     73
     74        if (__libc_TLSGet(pArg->iTLS) != pvArg)
     75        {
     76            printf("tls: error: tid %d: get failed on index %d, i=%d\n",
     77                   pArg->tid, pArg->iTLS, i);
     78            pArg->cErrors++;
     79        }
     80        usleep(1000);
     81    }
     82    pArg->fDone = 1;
     83}
     84
     85
     86/** allocations and deallocations thread function. */
     87void thread2(void *pvArg)
     88{
     89    PTHREADARG pArg = (PTHREADARG)pvArg;
     90    int i;
     91
     92    for (i = 0; i < 20000; i++)
     93    {
     94        int iTLS1;
     95        int iTLS2;
     96        iTLS1 = __libc_TLSAlloc();
     97        if (iTLS1 < 0)
     98        {
     99            printf("tls: error: tid %d: TLSAlloc (1) failed, i=%d\n", pArg->tid, i);
     100            pArg->cErrors++;
     101        }
     102
     103        iTLS2 = __libc_TLSAlloc();
     104        if (iTLS2 < 0)
     105        {
     106            printf("tls: error: tid %d: TLSAlloc (2) failed, i=%d\n", pArg->tid, i);
     107            pArg->cErrors++;
     108        }
     109
     110        if (__libc_TLSFree(iTLS1))
     111        {
     112            printf("tls: error: tid %d: TLSFree (1) failed, i=%d, iTLS1=%d\n", pArg->tid, i, iTLS1);
     113            pArg->cErrors++;
     114        }
     115
     116        if (__libc_TLSFree(iTLS2))
     117        {
     118            printf("tls: error: tid %d: TLSFree (2) failed, i=%d, iTLS2=%d\n", pArg->tid, i, iTLS2);
     119            pArg->cErrors++;
     120        }
     121    }
     122    pArg->fDone = 1;
     123}
    35124
    36125
    37126int main()
    38127{
    39     int     cErrors = 0;
    40     int     cTLSes = 0;
    41     int     aiTLSes[1024];
    42     int     i;
     128    int         cErrors = 0;
     129    int         cTLSes = 0;
     130    static int  aiTLSes[1024];
     131    int         i;
     132    static THREADARG    aThreadArgs[1024];
     133    int         iTLS;
    43134
    44135    /*
     
    75166
    76167    /*
     168     * Set,get,set all entries.
     169     */
     170    i = cTLSes;
     171    while (i-- > 0)
     172    {
     173        if (__libc_TLSSet(aiTLSes[i], (void*)(0x80000000 | i)))
     174        {
     175            printf("tls: error: set failed on index %d (i=%d)\n",
     176                   aiTLSes[i], i);
     177            cErrors++;
     178        }
     179
     180        if (__libc_TLSGet(aiTLSes[i]) != (void*)(0x80000000 | i))
     181        {
     182            printf("tls: error: get failed on index %d (i=%d)\n",
     183                   aiTLSes[i], i);
     184            cErrors++;
     185        }
     186
     187        if (__libc_TLSSet(aiTLSes[i], (void*)(0x80004000 | i)))
     188        {
     189            printf("tls: error: set (2) failed on index %d (i=%d)\n",
     190                   aiTLSes[i], i);
     191            cErrors++;
     192        }
     193    }
     194
     195    /*
    77196     * Free the allocated TLSes.
    78197     */
    79198    for (i = 0; i < cTLSes; i++)
    80199    {
     200        if (__libc_TLSGet(aiTLSes[i]) !=  (void*)(0x80004000 | i))
     201        {
     202            printf("tls: error: get (2) failed on index %d (i=%d)\n",
     203                   aiTLSes[i], i);
     204            cErrors++;
     205        }
     206
    81207        if (__libc_TLSFree(aiTLSes[i]))
    82208        {
     
    84210            cErrors++;
    85211        }
    86     }
     212
     213        if (__libc_TLSGet(aiTLSes[i]) != NULL)
     214        {
     215            printf("tls: error: access possible after freed, index %d\n", aiTLSes[i]);
     216            cErrors++;
     217        }
     218
     219    }
     220
     221
     222    /*
     223     * Set & Get threaded.
     224     */
     225    iTLS = __libc_TLSAlloc();
     226    if (iTLS >= 0)
     227    {
     228        /* spawn threads. */
     229        for (i = 0; i < TEST_THREADS; i++)
     230        {
     231            aThreadArgs[i].cErrors  = 0;
     232            aThreadArgs[i].iTLS     = iTLS;
     233            aThreadArgs[i].fDone    = 0;
     234            aThreadArgs[i].tid      = _beginthread(thread1, NULL, 0x10000, &aThreadArgs[i]);
     235            if (aThreadArgs[i].tid <= 0)
     236            {
     237                aThreadArgs[i].fDone    = 1;
     238                printf("tls: error: failed to create thread! i=%d\n", i);
     239                cErrors++;
     240            }
     241        }
     242
     243        /* wait for the threads */
     244        i = 0;
     245        do
     246        {
     247            usleep(64000 + i);
     248            for (i = 0; i < TEST_THREADS; i++)
     249            {
     250                if (!aThreadArgs[i].fDone)
     251                    break;
     252            }
     253        } while (i == TEST_THREADS);
     254
     255        for (i = 0; i < TEST_THREADS; i++)
     256            cErrors += aThreadArgs[i].cErrors;
     257
     258        if (__libc_TLSFree(iTLS))
     259        {
     260            printf("tls: error: failed to free TLS entry %d (set/get)\n", iTLS);
     261            cErrors++;
     262        }
     263    }
     264    else
     265    {
     266        printf("tls: error: failed to allocated TLS entry for set/get threaded.\n");
     267        cErrors++;
     268    }
     269
     270    /*
     271     * Threaded alloc + free.
     272     */
     273    /*
     274     * Set & Get threaded.
     275     */
     276    for (i = 0; i < TEST_THREADS; i++)
     277    {
     278        aThreadArgs[i].cErrors  = 0;
     279        aThreadArgs[i].iTLS     = iTLS;
     280        aThreadArgs[i].fDone    = 0;
     281        aThreadArgs[i].tid      = _beginthread(thread2, NULL, 0x10000, &aThreadArgs[i]);
     282        if (aThreadArgs[i].tid <= 0)
     283        {
     284            aThreadArgs[i].fDone    = 1;
     285            printf("tls: error: failed to create thread! i=%d\n", i);
     286            cErrors++;
     287        }
     288    }
     289
     290    /* wait for the threads */
     291    i = 0;
     292    do
     293    {
     294        usleep(32000 + i);
     295        for (i = 0; i < TEST_THREADS; i++)
     296        {
     297            if (!aThreadArgs[i].fDone)
     298                break;
     299        }
     300    } while (i == TEST_THREADS);
     301
     302    for (i = 0; i < TEST_THREADS; i++)
     303        cErrors += aThreadArgs[i].cErrors;
    87304
    88305
Note: See TracChangeset for help on using the changeset viewer.