From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM,T_FILL_THIS_FORM_SHORT autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,b59b337045eece60,start X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!m36g2000hse.googlegroups.com!not-for-mail From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Structure of the multitasking server Date: Fri, 19 Sep 2008 05:21:52 -0700 (PDT) Organization: http://groups.google.com Message-ID: <8b4d1170-22e6-40d3-8ed1-096dc0163491@m36g2000hse.googlegroups.com> NNTP-Posting-Host: 128.141.45.232 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1221826912 19890 127.0.0.1 (19 Sep 2008 12:21:52 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 19 Sep 2008 12:21:52 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: m36g2000hse.googlegroups.com; posting-host=128.141.45.232; posting-account=bMuEOQoAAACUUr_ghL3RBIi5neBZ5w_S User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:2044 Date: 2008-09-19T05:21:52-07:00 List-Id: Hi all, Imagine a server with fixed number of worker tasks. There is no queue of jobs and jobs are immediately passed to one of the tasks that is currently idle. There is a separate task (or just the main one) that provides jobs for worker tasks. I am concerned with the proper structure of objects - I mean in the sense of recommended Ada practice. Obviously there is a need for some shared resource where the requesting task will put the job and from where the worker task will pick it up. This is more or less what I came up with, where the "channel" is a single processing pipeline: type Worker_State is (Idle, Ready, Working); protected type Channel_State is procedure Post (J : in Job_Type); entry Get_Job (J : out Job_Type); function Busy return Boolean; private State : Worker_State := Idle; Job_To_Do : Job_Type; end Channel_State; protected body Channel_State is procedure Post (J : in Job_Type) is begin if State /= Idle then raise Program_Error; end if; Job_To_Do := J; State := Ready; end Post; entry Get_Job (J : out Job_Type) when State = Ready is begin J := Job_To_Do; State := Working; end Get_Job; function Busy return Boolean is begin return State /= Idle; end Busy; end Channel_State; type Channel; task type Worker_Task (Ch : access Channel); type Channel is record State : Channel_State; Worker : Worker_Task (Channel'Access); end record; task body Worker_Task is Job : Job_Type; begin loop Ch.all.Get_Job (Job); -- do the job ... end loop; end Worker_Task; Max_Channels : constant := 5; Channels : array (1 .. Max_Channels) of Channel; My question is whether this is what a seasoned Ada programmer would do. Initially I tried to have two separate arrays, one for jobs and one for worker tasks, but I found it difficult to link workers with their respective jobs. After bundling them together in a single record that is referenced from the task it worked and I actually find it structured better. The main task after constructing a job object finds some channel where the worker task is not busy and posts the job to its shared state component: loop Job := ... Found_Worker := False; for I in Channels'Range loop if not Channels (I).State.Busy then Channels (I).State.Post (Job); Found_Worker := True; exit; end if; end loop; if not Found_Worker then -- all pipelines are busy, -- but the overflow handling is not shown... end if; end loop; All this works fine, but my question considers the choice of language constructs and idioms. -- Maciej Sobczak * www.msobczak.com * www.inspirel.com Database Access Library for Ada: www.inspirel.com/soci-ada