% % Implementation by Piotr Hołownia % % Copyright (C) 2006-9 by the HeKatE Project % % PlNXT has been develped by the HeKatE Project, % see http://hekate.ia.agh.edu.pl % % This file is part of PlNXT. % % PlNXT is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % PlNXT is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with PlNXT. If not, see . % :- module(threads,[ trigger_create/3, timer_create/3, do_simultaneously/1 ]). /** Threads Implements triggers, timers and simultaneous actions. @author Piotr Hołownia @license GNU General Public License @tbd Cancelling triggers and timers by ID. */ %% trigger_create(-ID,+Event,+Action). % % Creates trigger. % Action will be fired, when Event is true. % Action can be both predicate and list of predicates. trigger_create(ID,Event,Action) :- thread_create(trigger(Event,Action),ID,[]). fired([Action|Tail]) :- Action, fired(Tail). fired([Action]) :- Action. fired(Action) :- Action. trigger(Event,Action) :- Event, fired(Action). trigger(Event,Action) :- trigger(Event,Action). %% timer_create(-ID,+Time,+Action). % % Creates timer. % Action will be fired after specified Time. % Action can be both predicate and list of predicates. timer_create(ID,Time,Action) :- get_time(Start_time), Finish_time is Start_time+Time, thread_create(timer(Finish_time,Action),ID,[]). timer(Finish_time,Action) :- get_time(Time), Time >= Finish_time, fired(Action). timer(Finish_time,Action) :- timer(Finish_time,Action). %% do_simultaneously(+Action). % % Creats a thread for each action from Action. % Last of them runs in the main thread. % Action should be a list of predicates. % Although Action can be a signle predicate, it makes no sense. do_simultaneously([Action|Tail]) :- thread_create(Action,_,[]), do_simultaneously(Tail). do_simultaneously([Action]) :- Action. do_simultaneously(Action) :- Action.