Changeset 4569 for trunk/compiler

Show
Ignore:
Timestamp:
11/05/08 10:43:48 (2 months ago)
Author:
pmoura
Message:

Simplified the implementation of the threaded/1 multi-threading built-in predicate.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/compiler/logtalk.pl

    r4563 r4569  
    78087808'$lgt_tr_threaded_call'((TGoal; TGoals), catch(ThreadedCall, '$lgt_terminated', fail)) :- 
    78097809    !, 
    7810     '$lgt_tr_threaded_or_call'((TGoal; TGoals), Queue, MTGoals, Ids, Results), 
     7810    '$lgt_tr_threaded_or_call'((TGoal; TGoals), Queue, MTGoals, Results), 
    78117811    ThreadedCall = (    thread_self(Queue), 
    78127812                        thread_send_message(Queue, '$lgt_master'), 
    78137813                        MTGoals, 
    7814                         '$lgt_mt_threaded_or_exit'(Ids, Results) 
     7814                        '$lgt_mt_threaded_or_exit'(Results) 
    78157815                    ). 
    78167816 
    78177817'$lgt_tr_threaded_call'((TGoal, TGoals), catch(ThreadedCall, '$lgt_terminated', fail)) :- 
    78187818    !, 
    7819     '$lgt_tr_threaded_and_call'((TGoal, TGoals), Queue, MTGoals, Ids, Results), 
     7819    '$lgt_tr_threaded_and_call'((TGoal, TGoals), Queue, MTGoals, Results), 
    78207820    ThreadedCall = (    thread_self(Queue), 
    78217821                        thread_send_message(Queue, '$lgt_master'), 
    78227822                        MTGoals, 
    7823                         '$lgt_mt_threaded_and_exit'(Ids, Results) 
     7823                        '$lgt_mt_threaded_and_exit'(Results) 
    78247824                    ). 
    78257825 
     
    78277827 
    78287828 
    7829 '$lgt_tr_threaded_or_call'((TGoal; TGoals), Queue, (MTGoal, MTGoals), [Id| Ids], [Result| Results]) :- 
    7830     !, 
    7831     '$lgt_tr_threaded_individual_goal'(TGoal, Queue, MTGoal, Id, Result), 
    7832     '$lgt_tr_threaded_or_call'(TGoals, Queue, MTGoals, Ids, Results). 
    7833  
    7834 '$lgt_tr_threaded_or_call'(TGoal, Queue, MTGoal, [Id], [Result]) :- 
    7835     '$lgt_tr_threaded_individual_goal'(TGoal, Queue, MTGoal, Id, Result). 
    7836  
    7837  
    7838 '$lgt_tr_threaded_and_call'((TGoal, TGoals), Queue, (MTGoal, MTGoals), [Id| Ids], [Result| Results]) :- 
    7839     !, 
    7840     '$lgt_tr_threaded_individual_goal'(TGoal, Queue, MTGoal, Id, Result), 
    7841     '$lgt_tr_threaded_and_call'(TGoals, Queue, MTGoals, Ids, Results). 
    7842  
    7843 '$lgt_tr_threaded_and_call'(TGoal, Queue, MTGoal, [Id], [Result]) :- 
    7844     '$lgt_tr_threaded_individual_goal'(TGoal, Queue, MTGoal, Id, Result). 
    7845  
    7846  
    7847 '$lgt_tr_threaded_individual_goal'(TGoal, Queue, thread_create('$lgt_mt_threaded_call'(TGoal, Queue), Id, [at_exit('$lgt_mt_exit_handler'(Id, Queue))]), Id, id(Id, TGoal, _)). 
     7829'$lgt_tr_threaded_or_call'((TGoal; TGoals), Queue, (MTGoal, MTGoals), [Result| Results]) :- 
     7830    !, 
     7831    '$lgt_tr_threaded_individual_goal'(TGoal, Queue, MTGoal, Result), 
     7832    '$lgt_tr_threaded_or_call'(TGoals, Queue, MTGoals, Results). 
     7833 
     7834'$lgt_tr_threaded_or_call'(TGoal, Queue, MTGoal, [Result]) :- 
     7835    '$lgt_tr_threaded_individual_goal'(TGoal, Queue, MTGoal, Result). 
     7836 
     7837 
     7838'$lgt_tr_threaded_and_call'((TGoal, TGoals), Queue, (MTGoal, MTGoals), [Result| Results]) :- 
     7839    !, 
     7840    '$lgt_tr_threaded_individual_goal'(TGoal, Queue, MTGoal, Result), 
     7841    '$lgt_tr_threaded_and_call'(TGoals, Queue, MTGoals, Results). 
     7842 
     7843'$lgt_tr_threaded_and_call'(TGoal, Queue, MTGoal, [Result]) :- 
     7844    '$lgt_tr_threaded_individual_goal'(TGoal, Queue, MTGoal, Result). 
     7845 
     7846 
     7847'$lgt_tr_threaded_individual_goal'(TGoal, Queue, thread_create('$lgt_mt_threaded_call'(TGoal, Queue), [at_exit('$lgt_mt_exit_handler'(Id, Queue))]), Id, id(Id, TGoal, _)). 
    78487848 
    78497849 
     
    1365013650 
    1365113651 
    13652 % '$lgt_mt_threaded_and_exit'(+list(thread_identifier), +list) 
     13652% '$lgt_mt_threaded_and_exit'(+list) 
    1365313653% 
    1365413654% retrieves the result of proving a conjunction of goals using a threaded/1 predicate call 
    1365513655% by collecting the individual thread results posted to the master thread message queue 
    1365613656 
    13657 '$lgt_mt_threaded_and_exit'(Ids, Results) :- 
     13657'$lgt_mt_threaded_and_exit'(Results) :- 
    1365813658    thread_get_message('$lgt_result'(Id, Result)), 
    13659     '$lgt_mt_threaded_and_exit'(Result, Id, Ids, Results). 
    13660  
    13661  
    13662 '$lgt_mt_threaded_and_exit'(terminate, _, Ids, Results) :- 
    13663     '$lgt_mt_threaded_call_cancel'(Ids, Results), 
     13659    '$lgt_mt_threaded_and_exit'(Result, Id, Results). 
     13660 
     13661 
     13662'$lgt_mt_threaded_and_exit'(terminate, _, Results) :- 
     13663    '$lgt_mt_threaded_call_cancel'(Results), 
    1366413664    throw('$lgt_terminated'). 
    1366513665 
     
    1366713667% thread result that lead to the termination of the other threads 
    1366813668 
    13669 '$lgt_mt_threaded_and_exit'(exception(Error), Id, Ids, Results) :- 
     13669'$lgt_mt_threaded_and_exit'(exception(Error), Id, Results) :- 
    1367013670    '$lgt_mt_threaded_record_result'(Results, Id, exception(Error)), 
    1367113671    (   Error == '$lgt_terminated' -> 
    13672         '$lgt_mt_threaded_and_exit'(Ids, Results) 
    13673     ;   '$lgt_mt_threaded_call_cancel'(Ids, Results), 
     13672        '$lgt_mt_threaded_and_exit'(Results) 
     13673    ;   '$lgt_mt_threaded_call_cancel'(Results), 
    1367413674        throw(Error) 
    1367513675    ). 
    1367613676 
    13677 '$lgt_mt_threaded_and_exit'(true(TGoal), Id, Ids, Results) :- 
     13677'$lgt_mt_threaded_and_exit'(true(TGoal), Id, Results) :- 
    1367813678    '$lgt_mt_threaded_and_add_result'(Results, Id, TGoal, Continue), 
    1367913679    (   Continue == false -> 
    13680         '$lgt_mt_threaded_call_join'(Ids) 
    13681     ;   '$lgt_mt_threaded_and_exit'(Ids, Results) 
    13682     ). 
    13683  
    13684 '$lgt_mt_threaded_and_exit'(false, Id, Ids, Results) :- 
     13680        '$lgt_mt_threaded_call_join'(Results) 
     13681    ;   '$lgt_mt_threaded_and_exit'(Results) 
     13682    ). 
     13683 
     13684'$lgt_mt_threaded_and_exit'(false, Id, Results) :- 
    1368513685    '$lgt_mt_threaded_record_result'(Results, Id, false), 
    13686     '$lgt_mt_threaded_call_cancel'(Ids, Results), 
     13686    '$lgt_mt_threaded_call_cancel'(Results), 
    1368713687    fail. 
    1368813688 
     
    1372013720 
    1372113721 
    13722 % '$lgt_mt_threaded_or_exit'(+list(thread_identifier), +list) 
     13722% '$lgt_mt_threaded_or_exit'(+list) 
    1372313723% 
    1372413724% retrieves the result of proving a disjunction of goals using a threaded/1 predicate 
    1372513725% call by collecting the individual thread results posted to the call message queue 
    1372613726 
    13727 '$lgt_mt_threaded_or_exit'(Ids, Results) :- 
     13727'$lgt_mt_threaded_or_exit'(Results) :- 
    1372813728    thread_get_message('$lgt_result'(Id, Result)), 
    13729     '$lgt_mt_threaded_or_exit'(Result, Id, Ids, Results). 
    13730  
    13731  
    13732 '$lgt_mt_threaded_or_exit'(terminate, _, Ids, Results) :- 
    13733     '$lgt_mt_threaded_call_cancel'(Ids, Results), 
     13729    '$lgt_mt_threaded_or_exit'(Result, Id, Results). 
     13730 
     13731 
     13732'$lgt_mt_threaded_or_exit'(terminate, _, Results) :- 
     13733    '$lgt_mt_threaded_call_cancel'(Results), 
    1373413734    throw('$lgt_terminated'). 
    1373513735 
     
    1373713737% thread result that lead to the termination of the other threads 
    1373813738 
    13739 '$lgt_mt_threaded_or_exit'(exception(Error), Id, Ids, Results) :- 
     13739'$lgt_mt_threaded_or_exit'(exception(Error), Id, Results) :- 
    1374013740    '$lgt_mt_threaded_record_result'(Results, Id, exception(Error)), 
    1374113741    (   Error == '$lgt_terminated' -> 
    13742         '$lgt_mt_threaded_or_exit'(Ids, Results) 
    13743     ;   '$lgt_mt_threaded_call_cancel'(Ids, Results), 
     13742        '$lgt_mt_threaded_or_exit'(Results) 
     13743    ;   '$lgt_mt_threaded_call_cancel'(Results), 
    1374413744        throw(Error) 
    1374513745    ). 
    1374613746 
    13747 '$lgt_mt_threaded_or_exit'(true(TGoal), Id, Ids, Results) :- 
     13747'$lgt_mt_threaded_or_exit'(true(TGoal), Id, Results) :- 
    1374813748    '$lgt_mt_threaded_or_exit_unify'(Results, Id, TGoal), 
    13749     '$lgt_mt_threaded_call_cancel'(Ids, Results). 
    13750  
    13751 '$lgt_mt_threaded_or_exit'(false, Id, Ids, Results) :- 
     13749    '$lgt_mt_threaded_call_cancel'(Results). 
     13750 
     13751'$lgt_mt_threaded_or_exit'(false, Id, Results) :- 
    1375213752    '$lgt_mt_threaded_or_record_failure'(Results, Id, Continue), 
    1375313753    (   Continue == true -> 
    13754         '$lgt_mt_threaded_or_exit'(Ids, Results) 
     13754        '$lgt_mt_threaded_or_exit'(Results) 
    1375513755    ;   % all goals failed 
    13756         '$lgt_mt_threaded_call_join'(Ids), 
     13756        '$lgt_mt_threaded_call_join'(Results), 
    1375713757        fail 
    1375813758    ). 
     
    1381313813 
    1381413814 
    13815 % '$lgt_mt_threaded_call_cancel'(+list(thread_identifier), +list) 
     13815% '$lgt_mt_threaded_call_cancel'(+list) 
    1381613816% 
    1381713817% aborts a threaded call by aborting and joining all individual threads; 
    1381813818% we must use catch/3 as some threads may already be terminated 
    1381913819 
    13820 '$lgt_mt_threaded_call_cancel'(Ids, Results) :- 
    13821     '$lgt_mt_threaded_call_abort'(Ids), 
    13822     '$lgt_mt_threaded_call_join'(Ids, Results). 
     13820'$lgt_mt_threaded_call_cancel'(Results) :- 
     13821    '$lgt_mt_threaded_call_abort'(Results), 
     13822    '$lgt_mt_threaded_call_join'(Results). 
    1382313823 
    1382413824 
    1382513825'$lgt_mt_threaded_call_abort'([]). 
    1382613826 
    13827 '$lgt_mt_threaded_call_abort'([Id| Ids]) :- 
     13827'$lgt_mt_threaded_call_abort'([id(Id, _, _)| Ids]) :- 
    1382813828    catch(thread_signal(Id, '$lgt_mt_abort_thread'(Id)), _, true), 
    1382913829    '$lgt_mt_threaded_call_abort'(Ids). 
     
    1383713837 
    1383813838 
    13839 '$lgt_mt_threaded_call_join'([], []). 
    13840  
    13841 '$lgt_mt_threaded_call_join'([Id| Ids], [id(Id, _, Result)| Results]) :- 
     13839 
     13840% '$lgt_mt_threaded_call_join'(+list) 
     13841% 
     13842% join all individual threads; we must use catch/3 as some threads may no longer exist 
     13843 
     13844'$lgt_mt_threaded_call_join'([]). 
     13845 
     13846'$lgt_mt_threaded_call_join'([id(Id, _, Result)| Results]) :- 
    1384213847    (   var(Result) -> 
    1384313848        thread_get_message('$lgt_result'(Id, _))    % don't leak thread results 
     
    1384513850    ), 
    1384613851    catch(thread_join(Id, _), _, true), 
    13847     '$lgt_mt_threaded_call_join'(Ids, Results). 
    13848  
    13849  
    13850  
    13851 % '$lgt_mt_threaded_call_join'(+list(thread_identifier)) 
    13852 % 
    13853 % join all individual threads; we must use catch/3 as some threads may no longer exist 
    13854  
    13855 '$lgt_mt_threaded_call_join'([]). 
    13856  
    13857 '$lgt_mt_threaded_call_join'([Id| Ids]) :- 
    13858     catch(thread_join(Id, _), _, true), 
    13859     '$lgt_mt_threaded_call_join'(Ids). 
     13852    '$lgt_mt_threaded_call_join'(Results). 
    1386013853 
    1386113854