Show
Ignore:
Timestamp:
07/16/07 03:30:29 (17 months ago)
Author:
krobillard
Message:

Atoms should now be thread safe. First thread experiment runs.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/thune/thread_safe/thune.c

    r427 r431  
    1111extern int ur_lessOrEqual( const UCell*, const UCell* ); 
    1212 
    13 #ifdef UR_CONFIG_THREADS 
    14 extern void ur_threadUnlink( UThread* ); 
    15 extern UThread* ur_threadNextReady( UThread* ); 
     13#ifdef UR_CONFIG_TASKS 
     14extern void ur_taskUnlink( UThread* ); 
     15extern UThread* ur_taskNextReady( UThread* ); 
    1616#endif 
    1717 
     
    164164 
    165165#ifdef UR_CONFIG_THREADS 
    166 // (block -- ) 
     166#ifdef _WIN32 
     167static DWORD WINAPI threadRoutine( LPVOID arg ) 
     168#else 
     169static void* threadRoutine( void* arg ) 
     170#endif 
     171{ 
     172    UThread* ut = (UThread*) arg; 
     173    ur_eval( ut, ut->tos[1].series.n, 0 ); 
     174    ur_threadFree( ut ); 
     175    return 0; 
     176} 
     177 
     178/* 
     179static void ur_threadCloneCell( UThread* ut, UCell* cellA, 
     180                                UThread* to, UCell* cellB ) 
     181{ 
     182    UIndex newBlkN; 
     183 
     184    switch( ur_type(cell) ) 
     185    { 
     186        case UT_BLOCK: 
     187        { 
     188            UBlock* blkA; 
     189            UBlock* blkB; 
     190            blkA = ur_blockPtr( blkN ); 
     191            newBlkN = ur_makeBlockT( to, blkA->used, &blkB );  
     192        } 
     193            break; 
     194 
     195        case UT_STRING: 
     196            break; 
     197    } 
     198} 
     199*/ 
     200 
     201 
     202// (code -- ) 
    167203UR_CALL( uc_thread ) 
    168204{ 
     205    if( ur_is(tos, UT_BLOCK) || ur_is(tos, UT_STRING) ) 
     206    { 
     207        UThread* thr = ur_threadMake( ut->env, 64, 128 ); 
     208        if( thr ) 
     209        { 
     210            OSThread pth; 
     211#ifdef _WIN32 
     212            DWORD winId; 
     213#endif 
     214 
     215            if( ur_is(tos, UT_STRING) ) 
     216            { 
     217                char* cpA; 
     218                char* cpB; 
     219                ur_seriesMem( ut, tos, &cpA, &cpB ); 
     220                thr->tos[1].series.n = ur_tokenize( thr, cpA, cpB ); 
     221            } 
     222 
     223#ifdef _WIN32 
     224            pth = CreateThread( NULL, 0, threadRoutine, thr, 0, &winId ); 
     225            if( pth == NULL ) 
     226#else 
     227            if( pthread_create( &pth, 0, threadRoutine, thr ) != 0 ) 
     228#endif 
     229            { 
     230                ur_throwErr( UR_ERR_INTERNAL, "Could not create thread" ); 
     231                return; 
     232            } 
     233 
     234#if 1 
     235            { 
     236            void* status; 
     237            pthread_join( pth, &status ); //KR 
     238            } 
     239#endif 
     240        } 
     241        UR_S_DROP; 
     242    } 
     243    else 
     244    { 
     245        ur_throwErr( UR_ERR_DATATYPE, "thread expected block!/string!" ); 
     246    } 
     247} 
     248#endif 
     249 
     250 
     251#ifdef UR_CONFIG_TASKS 
     252// (block -- ) 
     253UR_CALL( uc_task ) 
     254{ 
    169255    if( ur_is(tos, UT_BLOCK) ) 
    170256    { 
    171257        UThread* thr; 
    172258        UThread* save; 
    173         thr = ur_threadMake( ut->env ); 
     259        thr = ur_threadMake( ut->env, 32, 32 ); 
    174260        if( thr ) 
    175261        { 
     
    307393    UCell* pc; 
    308394    UCell* end; 
    309 #ifdef UR_CONFIG_THREADS 
     395#ifdef UR_CONFIG_TASKS 
    310396    int insCycle = 0; 
    311397#endif 
     
    323409execute: 
    324410 
    325 #ifdef UR_CONFIG_THREADS 
     411#ifdef UR_CONFIG_TASKS 
    326412    if( ++insCycle == 100 ) 
    327413    { 
    328         if( ut->nextThread != ut ) 
    329         { 
    330             ut->state = UR_THREAD_READY; 
     414        if( ut->nextTask != ut ) 
     415        { 
     416            ut->state = UR_TASK_READY; 
    331417switch_thread: 
    332418            PUSHC_EVAL( blkN, start, pc ); 
    333             ut = ur_threadNextReady( ut ); 
     419            ut = ur_taskNextReady( ut ); 
    334420            if( ! ut ) 
    335421                goto error; 
     
    363449        case UT_UNSET: 
    364450            --pc; 
    365 #ifdef UR_CONFIG_THREADS 
    366             if( ut->nextThread != ut ) 
    367             { 
    368                 ut->state = UR_THREAD_BLOCKED; 
     451#ifdef UR_CONFIG_TASKS 
     452            if( ut->nextTask != ut ) 
     453            { 
     454                ut->state = UR_TASK_BLOCKED; 
    369455                goto switch_thread; 
    370456            } 
     
    9561042            goto execute; 
    9571043 
    958 #ifdef UR_CONFIG_THREADS 
     1044#ifdef UR_CONFIG_TASKS 
    9591045        case CC_TERM: 
    960             if( ut->nextThread == ut ) 
     1046            if( ut->nextTask == ut ) 
    9611047                goto finish; 
    962 term_thread: 
     1048term_task: 
    9631049            { 
    9641050            UThread* thr = ut; 
    965             ut->state = UR_THREAD_TERM; 
    966             ut = ur_threadNextReady( ut ); 
     1051            ut->state = UR_TASK_TERM; 
     1052            ut = ur_taskNextReady( ut ); 
    9671053            if( ! ut ) 
    9681054                goto error; 
    969             ur_threadUnlink( thr ); 
     1055            ur_taskUnlink( thr ); 
    9701056            ur_threadFree( thr ); 
    9711057            } 
     
    9741060 
    9751061        case CC_END: 
    976 #ifdef UR_CONFIG_THREADS 
    977             if( ut->nextThread != ut ) 
    978                 goto term_thread; 
     1062#ifdef UR_CONFIG_TASKS 
     1063            if( ut->nextTask != ut ) 
     1064                goto term_task; 
    9791065#endif 
    9801066            goto finish;