Changeset 4312

Show
Ignore:
Timestamp:
06/14/08 15:12:35 (4 months ago)
Author:
pmoura
Message:

Small optimizations to thread cancellation.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/compiler/logtalk.pl

    r4306 r4312  
    1321413214    (   Error == '$lgt_terminated' -> 
    1321513215        '$lgt_mt_threaded_and_exit'(Ids, Results) 
    13216     ;   '$lgt_mt_threaded_call_cancel'(Ids, Results, Id), 
     13216    ;   '$lgt_mt_threaded_call_cancel'(Ids, Results), 
    1321713217        throw(Error) 
    1321813218    ). 
     
    1322213222    '$lgt_mt_threaded_and_add_result'(Results, Id, TGoal, Continue), 
    1322313223    (   Continue == false -> 
    13224         '$lgt_mt_threaded_call_join'(Ids, Results) 
     13224        '$lgt_mt_threaded_call_join'(Ids) 
    1322513225    ;   '$lgt_mt_threaded_and_exit'(Ids, Results) 
    1322613226    ). 
     
    1322813228'$lgt_mt_threaded_and_exit'(false, Id, Ids, Results) :- 
    1322913229    '$lgt_mt_threaded_record_result'(Results, Id, false), 
    13230     '$lgt_mt_threaded_call_cancel'(Ids, Results, Id), 
     13230    '$lgt_mt_threaded_call_cancel'(Ids, Results), 
    1323113231    fail. 
    1323213232 
     
    1328513285    (   Error == '$lgt_terminated' -> 
    1328613286        '$lgt_mt_threaded_or_exit'(Ids, Results) 
    13287     ;   '$lgt_mt_threaded_call_cancel'(Ids, Results, Id), 
     13287    ;   '$lgt_mt_threaded_call_cancel'(Ids, Results), 
    1328813288        throw(Error) 
    1328913289    ). 
     
    1329213292    thread_get_message('$lgt_result'(Id, TGoal)), 
    1329313293    '$lgt_mt_threaded_record_result'(Results, Id, true), 
    13294     '$lgt_mt_threaded_call_cancel'(Ids, Results, Id), 
     13294    '$lgt_mt_threaded_call_cancel'(Ids, Results), 
    1329513295    '$lgt_mt_threaded_or_exit_unify'(Results, Id, TGoal). 
    1329613296 
     
    1330013300        '$lgt_mt_threaded_or_exit'(Ids, Results) 
    1330113301    ;   % all goals failed 
    13302         '$lgt_mt_threaded_call_join'(Ids, Results), 
     13302        '$lgt_mt_threaded_call_join'(Ids), 
    1330313303        fail 
    1330413304    ). 
     
    1333213332% records a thread goal failure and checks if all other thread goals have failed: 
    1333313333 
    13334 '$lgt_mt_threaded_or_record_failure'([id(Id, _, fail)| Results], Id, Continue) :- 
     13334'$lgt_mt_threaded_or_record_failure'([id(Id, _, false)| Results], Id, Continue) :- 
    1333513335    !, 
    1333613336    (   var(Continue) ->    % we still don't know if there are any pending results 
     
    1333913339    ). 
    1334013340 
    13341 '$lgt_mt_threaded_or_record_failure'([id(_, _, Done)| Results], Id, Continue) :- 
    13342     (   var(Done) -> 
     13341'$lgt_mt_threaded_or_record_failure'([id(_, _, Result)| Results], Id, Continue) :- 
     13342    (   var(Result) -> 
    1334313343        Continue = true     % we found a thread whose result is still pending 
    1334413344    ;   true                % otherwise continue examining the remaining thread results 
     
    1334913349'$lgt_mt_threaded_or_continue'([], false). 
    1335013350 
    13351 '$lgt_mt_threaded_or_continue'([id(_, _, Done)| Results], Continue) :- 
    13352     (   var(Done) -> 
     13351'$lgt_mt_threaded_or_continue'([id(_, _, Result)| Results], Continue) :- 
     13352    (   var(Result) -> 
    1335313353        % we found a thread whose result is still pending 
    1335413354        Continue = true 
     
    1335913359 
    1336013360 
    13361 % '$lgt_mt_threaded_call_cancel'(+list(thread_identifier), +list, +thread_identifier) 
     13361% '$lgt_mt_threaded_call_cancel'(+list(thread_identifier), +list) 
    1336213362% 
    1336313363% aborts a threaded call by aborting and joining all individual threads; 
    1336413364% we must use catch/3 as some threads may already be terminated 
    13365  
    13366 '$lgt_mt_threaded_call_cancel'(Ids, Results, ProtectedId) :- 
    13367     '$lgt_mt_threaded_call_abort'(Ids, ProtectedId), 
    13368     '$lgt_mt_threaded_call_join'(Ids, Results). 
    13369  
    13370  
    13371 '$lgt_mt_threaded_call_abort'([], _). 
    13372  
    13373 '$lgt_mt_threaded_call_abort'([ProtectedId| Ids], ProtectedId) :- 
    13374     !, 
    13375     '$lgt_mt_threaded_call_abort'(Ids, ProtectedId). 
    13376  
    13377 '$lgt_mt_threaded_call_abort'([Id| Ids], ProtectedId) :- 
    13378     (   catch(thread_peek_message(Id, '$lgt_master'), _, fail) -> 
    13379         catch(thread_send_message(Id, '$lgt_status'(_, terminate)), _, true) 
    13380     ;   catch(thread_signal(Id, '$lgt_mt_thread_abort'), _, true) 
    13381     ), 
    13382     '$lgt_mt_threaded_call_abort'(Ids, ProtectedId). 
    13383  
    1338413365 
    1338513366'$lgt_mt_threaded_call_cancel'(Ids, Results) :- 
     
    1340213383'$lgt_mt_threaded_call_join'([Id| Ids], [id(Id, _, Result)| Results]) :- 
    1340313384    (   var(Result) -> 
     13385        % don't leak thread results 
    1340413386        thread_get_message('$lgt_status'(Id, Result)), 
    1340513387        (   Result == true -> 
     
    1341613398    mutex_unlock_all, 
    1341713399    throw('$lgt_terminated'). 
     13400 
     13401 
     13402 
     13403% '$lgt_mt_threaded_call_join'(+list(thread_identifier)) 
     13404% 
     13405% join all individual threads; we must use catch/3 as some threads may no longer exist 
     13406 
     13407'$lgt_mt_threaded_call_join'([]). 
     13408 
     13409'$lgt_mt_threaded_call_join'([Id| Ids]) :- 
     13410    catch(thread_join(Id, _), _, true), 
     13411    '$lgt_mt_threaded_call_join'(Ids). 
    1341813412 
    1341913413