comp.lang.ada
 help / color / mirror / Atom feed
* open a file non-blocking ?
@ 2018-02-24 19:42 patrick
  2018-02-25  7:57 ` Dmitry A. Kazakov
  2018-02-26  2:53 ` Robert Eachus
  0 siblings, 2 replies; 11+ messages in thread
From: patrick @ 2018-02-24 19:42 UTC (permalink / raw)


Hi Everyone

I have the options for form strings here:

https://docs.adacore.com/gnat_rm-docs/html/gnat_rm/gnat_rm/the_implementation_of_standard_i_o.html

There does not seem to be an option to open a file non-blocking. Is there a way?

Thanks for reading-Patrick


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: open a file non-blocking ?
  2018-02-24 19:42 open a file non-blocking ? patrick
@ 2018-02-25  7:57 ` Dmitry A. Kazakov
  2018-02-25 12:38   ` patrick
  2018-02-26  2:53 ` Robert Eachus
  1 sibling, 1 reply; 11+ messages in thread
From: Dmitry A. Kazakov @ 2018-02-25  7:57 UTC (permalink / raw)


On 2018-02-24 20:42, patrick@spellingbeewinnars.org wrote:

> I have the options for form strings here:
> 
> https://docs.adacore.com/gnat_rm-docs/html/gnat_rm/gnat_rm/the_implementation_of_standard_i_o.html
> 
> There does not seem to be an option to open a file non-blocking. Is there a way?

You mean asynchronous AKA overlapping I/O? I don't think it were 
possible at this level of abstraction.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: open a file non-blocking ?
  2018-02-25  7:57 ` Dmitry A. Kazakov
@ 2018-02-25 12:38   ` patrick
  0 siblings, 0 replies; 11+ messages in thread
From: patrick @ 2018-02-25 12:38 UTC (permalink / raw)


Thanks yet again Dmitry

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: open a file non-blocking ?
  2018-02-24 19:42 open a file non-blocking ? patrick
  2018-02-25  7:57 ` Dmitry A. Kazakov
@ 2018-02-26  2:53 ` Robert Eachus
  2018-02-26  9:33   ` Dmitry A. Kazakov
  2018-02-26 16:41   ` Jeffrey R. Carter
  1 sibling, 2 replies; 11+ messages in thread
From: Robert Eachus @ 2018-02-26  2:53 UTC (permalink / raw)


On Saturday, February 24, 2018 at 2:42:30 PM UTC-5, pat...@spellingbeewinnars.org wrote:
 
> There does not seem to be an option to open a file non-blocking. Is there a way?

If you really want to play with pathologies, open a file in your main program, then create a dozen tasks which can all write to the file.  (Actually such programs were written and run in the early days of Ada to figure out what minimal behavior you could expect.  Whether your implementation is nice, and sequentializes calls to Text_IO for the same file or device, may depend on the device...

If you want to, for example, log errors and have them appear one per line, it is not too hard to do.  If you want IO without blocking but which is useful?  The hard part there is defining what is useful, and that is for the requirements if you are lucky.

There is a lot of literature on non-blocking I/O which trys to minimize semaphores and mutexes.  That is more about algorithms that avoid or minimize fence instructions (SFENCE, MFENCE, and WFENCE) when you are trying to get task synchronization above a few hundred million times a second.  The easier, practical approach is to allow all of the queued actions in one thread to occur before it gives up the lock.  There may be real-time limits so that only 30 or whatever number can go through.  (Think of a traffic light used to merge lanes of traffic.  You don't want the delay too long, for when the intersection isn't busy, but you also don't want the delay too short.  This is practice syncronization protocols without fence instructions are academic theory.) 

There is no way you can require an Ada library to avoid some blocking (my favorite example is a printer that runs out of paper).  Do you want that to be a blocking event?  What about spinning down a disk drive for whatever reason?

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: open a file non-blocking ?
  2018-02-26  2:53 ` Robert Eachus
@ 2018-02-26  9:33   ` Dmitry A. Kazakov
  2018-02-26 16:41   ` Jeffrey R. Carter
  1 sibling, 0 replies; 11+ messages in thread
From: Dmitry A. Kazakov @ 2018-02-26  9:33 UTC (permalink / raw)


On 26/02/2018 03:53, Robert Eachus wrote:

> There is a lot of literature on non-blocking I/O which trys to minimize semaphores and mutexes.  That is more about algorithms that avoid or minimize fence instructions (SFENCE, MFENCE, and WFENCE) when you are trying to get task synchronization above a few hundred million times a second.  The easier, practical approach is to allow all of the queued actions in one thread to occur before it gives up the lock.  There may be real-time limits so that only 30 or whatever number can go through.  (Think of a traffic light used to merge lanes of traffic.  You don't want the delay too long, for when the intersection isn't busy, but you also don't want the delay too short.  This is practice syncronization protocols without fence instructions are academic theory.)
> 
> There is no way you can require an Ada library to avoid some blocking (my favorite example is a printer that runs out of paper).  Do you want that to be a blocking event?  What about spinning down a disk drive for whatever reason?

True, but I think the question was not about completely avoiding 
blocking, rather about having I/O asynchronous. Ada has a higher-level 
mechanism of timed entry call. Why not to use it for a library interface?

    select
       File.Queue_Write (Data, Write_Request);
    or delay 0.1;
       raise Device_Busy_Error;
    end select;
    ...
    select
       File.Wait_Write (Write_Request);
    or delay 1.0;
       raise Device_Timeout_Error;
    end select;

There are issues with having such interfaces. One is that protected 
types are not proper tagged types. Another is that there is no multiple 
entry calls. Normally you would want to wait for a combination of I/O 
events + termination event + timeout.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: open a file non-blocking ?
  2018-02-26  2:53 ` Robert Eachus
  2018-02-26  9:33   ` Dmitry A. Kazakov
@ 2018-02-26 16:41   ` Jeffrey R. Carter
  2018-02-26 16:55     ` Dmitry A. Kazakov
  1 sibling, 1 reply; 11+ messages in thread
From: Jeffrey R. Carter @ 2018-02-26 16:41 UTC (permalink / raw)


> On Saturday, February 24, 2018 at 2:42:30 PM UTC-5, pat...@spellingbeewinnars.org wrote:
>   
> There does not seem to be an option to open a file non-blocking. Is there a way?

I guess it depends on what you mean by non-blocking. Some of the responses on 
here seem to think it means you can issue a write to the file and it returns 
immediately, without waiting for the write to file to complete.

If that's the case, the normal way to deal with this within Ada is something like

package Write_Queue_Interfaces is new
    Ada.Containers.Synchronized_Queue_Interfaces
       (Element_Type => Ada.Strings.Unbounded.Unbounded_String);

package Write_Queues is new Ada.Containers.Unbounded_Synchornized_Queues
    (Queue_Interfaces => Write_Queue_Interfaces);

Write_Queue : Write_Queues.Queue;

task Writer;

task body Writer is
    File : Ada.Text_IO.File_Type;
    Item : Ada.Strings.Unbounded.Unbounded_String;
begin -- Writer
    Ada.Text_IO.Create (File => File, Name => ...);

    Forever : loop
       Write_Queue.Dequeue (Element => Item);
       Ada.Text_IO.Put_Line
          (File => File, Item => Ada.Strings.Unbounded.To_String (Item) );
    end loop Forever;
end Writer;

Probably in reality you'd want a way to exit the loop and close the file. A task 
wishing to write Msg to the file does

Write_Queue.Enqueue
    (New_Item => Ada.Strings.Unbounded.To_Unbounded_String (Msg) );

-- 
Jeff Carter
"He nevere yet no vileynye ne sayde
In al his lyf unto no maner wight."
Canterbury Tales
156


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: open a file non-blocking ?
  2018-02-26 16:41   ` Jeffrey R. Carter
@ 2018-02-26 16:55     ` Dmitry A. Kazakov
  2018-02-27 17:14       ` patrick
  0 siblings, 1 reply; 11+ messages in thread
From: Dmitry A. Kazakov @ 2018-02-26 16:55 UTC (permalink / raw)


On 2018-02-26 17:41, Jeffrey R. Carter wrote:
>> On Saturday, February 24, 2018 at 2:42:30 PM UTC-5, 
>> pat...@spellingbeewinnars.org wrote:
>> There does not seem to be an option to open a file non-blocking. Is 
>> there a way?
> 
> I guess it depends on what you mean by non-blocking. Some of the 
> responses on here seem to think it means you can issue a write to the 
> file and it returns immediately, without waiting for the write to file 
> to complete.
> 
> If that's the case, the normal way to deal with this within Ada is 
> something like

[...]

No, to my understanding the question was about using OS asynchronous I/O 
facilities rather than mimicking them on top of Ada blocking tasks. 
Apart from wasting resources it is not even close to be asynchronous in 
the sense of I/O. The monitor solution you described does not actually 
multiplex I/O requests. It does single *blocking* request at a time.

As an example consider dealing with several sockets. With a socket 
selector all socket I/O is done concurrently. This is true asynchronous 
I/O. With a monitor task, only one socket can be serviced at a time. The 
difference in terms of data throughout and packet latencies will be 
gigantic.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: open a file non-blocking ?
  2018-02-26 16:55     ` Dmitry A. Kazakov
@ 2018-02-27 17:14       ` patrick
  2018-02-27 17:32         ` Dmitry A. Kazakov
  2018-02-28  1:13         ` Dennis Lee Bieber
  0 siblings, 2 replies; 11+ messages in thread
From: patrick @ 2018-02-27 17:14 UTC (permalink / raw)


Hi Guys

Sorry for my slow response and thanks for posting.

In C I can open with O_NONBLOCK. Is there the same in Ada?

I have different applications but one would be to read from several serial ports. Serial ports are very slow and if opened non-blocking a single task could easily monitor them.

Tasking is nice but I don't think it is a good idea to use it if there is a simple single threaded solution.

thanks again


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: open a file non-blocking ?
  2018-02-27 17:14       ` patrick
@ 2018-02-27 17:32         ` Dmitry A. Kazakov
  2018-02-27 23:33           ` patrick
  2018-02-28  1:13         ` Dennis Lee Bieber
  1 sibling, 1 reply; 11+ messages in thread
From: Dmitry A. Kazakov @ 2018-02-27 17:32 UTC (permalink / raw)


On 2018-02-27 18:14, patrick@spellingbeewinnars.org wrote:

> In C I can open with O_NONBLOCK. Is there the same in Ada?

Sure. If your system has open() you can call it from Ada just like you 
would do from C.

> I have different applications but one would be to read from several serial ports. Serial ports are very slow and if opened non-blocking a single task could easily monitor them.
> 
> Tasking is nice but I don't think it is a good idea to use it if there is a simple single threaded solution.

Ah, you actually want "get immediately" for a COM port. That is already 
done. See GNAT.Serial_Communications package which does what you want. 
You can poll ports from one task if you wanted.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: open a file non-blocking ?
  2018-02-27 17:32         ` Dmitry A. Kazakov
@ 2018-02-27 23:33           ` patrick
  0 siblings, 0 replies; 11+ messages in thread
From: patrick @ 2018-02-27 23:33 UTC (permalink / raw)


Hi Again Dmitry

This is a very interesting post. I new about GNAT.Serial_Communications but I hadn't looked at the sources in a while. I wasn't really impressed when I read it the first time but I am now.

It binds to fcntl and it opens with O_NDELAY, which is similar to O_NONBLOCK.

There is a post here about the difference:
https://mail.python.org/pipermail/python-list/1999-May/013687.html

This serial package also gives code examples that could be used with any device to avoid blocking.

Thanks again! -Patrick





^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: open a file non-blocking ?
  2018-02-27 17:14       ` patrick
  2018-02-27 17:32         ` Dmitry A. Kazakov
@ 2018-02-28  1:13         ` Dennis Lee Bieber
  1 sibling, 0 replies; 11+ messages in thread
From: Dennis Lee Bieber @ 2018-02-28  1:13 UTC (permalink / raw)


On Tue, 27 Feb 2018 09:14:31 -0800 (PST), patrick@spellingbeewinnars.org
declaimed the following:

>Hi Guys
>
>Sorry for my slow response and thanks for posting.
>
>In C I can open with O_NONBLOCK. Is there the same in Ada?
>
	Ada I/O is closer to C stdio file pointer operations -- fopen() --
which does not include such options.

	Not the lower level file descriptor -- open() -- set of operations.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2018-02-28  1:13 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-24 19:42 open a file non-blocking ? patrick
2018-02-25  7:57 ` Dmitry A. Kazakov
2018-02-25 12:38   ` patrick
2018-02-26  2:53 ` Robert Eachus
2018-02-26  9:33   ` Dmitry A. Kazakov
2018-02-26 16:41   ` Jeffrey R. Carter
2018-02-26 16:55     ` Dmitry A. Kazakov
2018-02-27 17:14       ` patrick
2018-02-27 17:32         ` Dmitry A. Kazakov
2018-02-27 23:33           ` patrick
2018-02-28  1:13         ` Dennis Lee Bieber

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox