From: Dmitry A. Kazakov <mailbox@dmitry-kazakov.de>
Subject: Re: new_line in a put_line
Date: Tue, 03 Dec 2002 16:01:58 +0100
Date: 2002-12-03T16:01:58+01:00 [thread overview]
Message-ID: <gjgpuu012viiefi3is2skthq7j13vg3969@4ax.com> (raw)
In-Reply-To: u7kerbalq.fsf@FWILSON.i-did-not-set--mail-host-address--so-shoot-me
On 03 Dec 2002 12:33:37 +0100, Fraser Wilson
<newsfraser@blancolioni.org> wrote:
>mheaney@on2.com (Matthew Heaney) writes:
>
>> If you have multiple tasks writing to the same file, then you're going
>> to have to synchronize access to the file through some intermediary,
>> as a task or protected object (the latter requires care).
>
>I've seen a couple of mentions of problems using protected objects for
>atomic file output. What's the specific issue? I.e., why isn't this
>sufficient?
Just do not do I/O from them. Define a mutex as a protected object and
a spin lock as a controlled type (less troubles with releasing a
mutex):
with Ada.Finalization;
package Sync is
protected type Mutex is
entry Seize;
procedure Release;
private
Owned : Boolean := False;
end Mutex;
type Lock (Resource : access Mutex) is new
Ada.Finalization.Limited_Controlled with private;
private
type Lock (Resource : access Mutex) is new
Ada.Finalization.Limited_Controlled with null record;
procedure Initialize (Object : in out Lock);
procedure Finalize (Object : in out Lock);
end Sync;
---------------------------------------
package body Sync is
protected body Mutex is
entry Seize when not Owned is
begin
Owned := True;
end Seize;
procedure Release is
begin
Owned := False;
end Release;
end Mutex;
procedure Initialize (Object : in out Lock) is
begin
Object.Resource.Seize;
end Initialize;
procedure Finalize (Object : in out Lock) is
begin
Object.Resource.Release;
end Finalize;
end Sync;
---------------------------------------
Now your task-safe I/O package could be very sraightforward:
package File_Operation is
procedure Put_Line (Text : String);
end File_Operation;
--------------------------------------------------------
with Ada.Text_IO;
with Sync; use Sync;
package body File_Operation is
Write_Mutex : aliased Mutex;
procedure Put_Line (Text : String) is
Get_It : Lock (Write_Mutex'Access);
begin
Ada.Text_IO.Put_Line (Text);
end Put_Line;
end File_Operation;
>Oh, wait on, is it something to do with 9.5.1(8)?
There could be an implementation of protected objects that would use
one lock for all of them. Now imagine, that you do I/O from a
protected operation, so no other protected operation can start on any
other object until I/O completion. That would be sort of MS Windows!
(:-))
---
Regards,
Dmitry Kazakov
www.dmitry-kazakov.de
next prev parent reply other threads:[~2002-12-03 15:01 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-12-02 5:36 new_line in a put_line Vlad
2002-12-02 9:47 ` Preben Randhol
2002-12-02 17:04 ` Frank J. Lhota
2002-12-05 11:10 ` Preben Randhol
2002-12-02 10:14 ` Dmitry A. Kazakov
2002-12-02 14:57 ` Matthew Heaney
2002-12-03 11:33 ` Fraser Wilson
2002-12-03 15:01 ` Dmitry A. Kazakov [this message]
2002-12-04 9:31 ` Fraser Wilson
2002-12-04 14:10 ` Dmitry A. Kazakov
2002-12-04 15:23 ` Robert A Duff
2002-12-04 16:15 ` Dmitry A. Kazakov
2002-12-04 18:11 ` tmoran
2002-12-04 20:21 ` Simon Wright
2002-12-05 9:36 ` Dmitry A. Kazakov
2002-12-05 22:40 ` tmoran
2002-12-04 15:25 ` Stephen Leake
2002-12-04 16:55 ` Jeffrey Carter
2002-12-04 17:24 ` Stephen Leake
2002-12-04 17:43 ` Warren W. Gay VE3WWG
2002-12-05 1:31 ` Jeffrey Carter
2002-12-05 18:11 ` Warren W. Gay VE3WWG
2002-12-02 15:49 ` Robert A Duff
-- strict thread matches above, loose matches on Subject: below --
2002-12-03 11:52 Grein, Christoph
2002-12-04 9:39 ` Fraser Wilson
2002-12-04 10:03 Grein, Christoph
2002-12-05 6:16 Grein, Christoph
2002-12-05 9:44 ` Dmitry A. Kazakov
2002-12-05 6:28 Grein, Christoph
2002-12-05 14:19 ` Stephen Leake
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox