comp.lang.ada
 help / color / mirror / Atom feed
* how to use "in out" for the "self" parameter in a function in a protected object?
@ 2017-09-16 20:47 Frank Buss
  2017-09-16 22:02 ` Jeffrey R. Carter
  0 siblings, 1 reply; 6+ messages in thread
From: Frank Buss @ 2017-09-16 20:47 UTC (permalink / raw)


I have a function in a protected object, which needs to change a 
variable, but I get this error:

   actual for "Self" must be a variable

I assume the reason is, that the implicit "self" parameter for my 
function is declared as "in" and not "in out". How can I change it to 
"in out"?

Below is the code. The function "Available" for example works, but the 
function "Read" causes the error when compiling. Both are accessing the 
FIFO variable, "Available" only with a call where the variable is used 
as an "in" parameter, but for "Read" I call a function where it is 
passed as an "in out" parameter.


with Interfaces; use Interfaces;
with Ada.Interrupts; use Ada.Interrupts;
with Ada.Interrupts.Names; use Ada.Interrupts.Names;
with STM32.USARTs; use STM32.USARTs;
with Ringbuffers;

package Serial_IO is

    package FIFO_Package is new Ringbuffers(256, Unsigned_8);
    subtype FIFO is FIFO_Package.Ringbuffer;

    protected type Serial_Port_Controller
    is

       procedure Init (Baud_Rate: Baud_Rates);

       function Available return Boolean;

       function Read return Unsigned_8;

       procedure Write (Data : Unsigned_8);

    private
       procedure Interrupt_Handler;
       pragma Attach_Handler (Interrupt_Handler, USART1_Interrupt);

       Input : FIFO;
       Output: FIFO;
       Initialized : Boolean := False;
    end Serial_Port_Controller;

    Serial : Serial_Port_Controller;

end Serial_IO;





-- 
Frank Buss, http://www.frank-buss.de
electronics and more: http://www.youtube.com/user/frankbuss

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

* Re: how to use "in out" for the "self" parameter in a function in a protected object?
  2017-09-16 20:47 how to use "in out" for the "self" parameter in a function in a protected object? Frank Buss
@ 2017-09-16 22:02 ` Jeffrey R. Carter
  2017-09-16 22:40   ` Frank Buss
  0 siblings, 1 reply; 6+ messages in thread
From: Jeffrey R. Carter @ 2017-09-16 22:02 UTC (permalink / raw)


On 09/16/2017 10:47 PM, Frank Buss wrote:
> I have a function in a protected object, which needs to change a variable, but I 
> get this error:
> 
>    actual for "Self" must be a variable

There's no Self in the code you posted, so I can't really say. However, a 
protected function is not allowed to change the state of the protected object.

-- 
Jeff Carter
"When danger reared its ugly head, he bravely
turned his tail and fled."
Monty Python and the Holy Grail
60

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

* Re: how to use "in out" for the "self" parameter in a function in a protected object?
  2017-09-16 22:02 ` Jeffrey R. Carter
@ 2017-09-16 22:40   ` Frank Buss
  2017-09-17  1:35     ` Dennis Lee Bieber
  2017-09-17  8:36     ` Jeffrey R. Carter
  0 siblings, 2 replies; 6+ messages in thread
From: Frank Buss @ 2017-09-16 22:40 UTC (permalink / raw)


On 09/17/2017 12:02 AM, Jeffrey R. Carter wrote:
> On 09/16/2017 10:47 PM, Frank Buss wrote:
>> I have a function in a protected object, which needs to change a
>> variable, but I get this error:
>>
>>    actual for "Self" must be a variable
>
> There's no Self in the code you posted, so I can't really say.

Yes, this is strange. I guess this is the parameter for the call inside 
the function, which is named "Self" and declared as "in out".

 > However, a protected function is not allowed to change the state of
 > the protected object.

What's the rationale for this behavior and where can I find the 
definition, that it is not allowed? Doesn't look useful to me, because I 
can do this with a procedure. In fact now I have written a procedure 
with an "out" parameter and it works. But it is annoying to declare an 
extra temporary variable for it when I call it. Would be nice, if I 
could do this with a function.

There is no such limitation when I use a tagged type, as you can see for 
the "Self" parameter in the Ringbuffer package I wrote earlier, see here:

https://groups.google.com/d/msg/comp.lang.ada/HXrcX2w8qpQ/I7Uw12BaAwAJ

This looks inconsistent to me. At least it should be possible to change 
the behavior, if the default is only "in".

-- 
Frank Buss, http://www.frank-buss.de
electronics and more: http://www.youtube.com/user/frankbuss

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

* Re: how to use "in out" for the "self" parameter in a function in a protected object?
  2017-09-16 22:40   ` Frank Buss
@ 2017-09-17  1:35     ` Dennis Lee Bieber
  2017-09-17  8:36     ` Jeffrey R. Carter
  1 sibling, 0 replies; 6+ messages in thread
From: Dennis Lee Bieber @ 2017-09-17  1:35 UTC (permalink / raw)


On Sun, 17 Sep 2017 00:40:12 +0200, Frank Buss <fb@frank-buss.de> declaimed
the following:

>
>What's the rationale for this behavior and where can I find the 
>definition, that it is not allowed? Doesn't look useful to me, because I 
>can do this with a procedure. In fact now I have written a procedure 

	As I recall...

	Functions are considered read-only, and hence don't need the same
locking behavior that is required to safely call into procedures.


	Consolidated Ada Reference Manual -- 2005 Edition (Springer) section
9.5.1
"""
A protected subprogram is a subprogram declared immediately within a
protected_definition. Protected procedures provide exclusive read-write
access to the data of a protected object; protected functions provide
concurrent read-only access to the data.
"""

	... so, multiple tasks can call protected functions without blocking
each other, but a call to a protected procedure blocks all other attempts
to access the object.

	I don't know how 2012's change to allow in/out parameters on functions
applies to protected objects.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/


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

* Re: how to use "in out" for the "self" parameter in a function in a protected object?
  2017-09-16 22:40   ` Frank Buss
  2017-09-17  1:35     ` Dennis Lee Bieber
@ 2017-09-17  8:36     ` Jeffrey R. Carter
  2017-10-03  0:08       ` Randy Brukardt
  1 sibling, 1 reply; 6+ messages in thread
From: Jeffrey R. Carter @ 2017-09-17  8:36 UTC (permalink / raw)


On 09/17/2017 12:40 AM, Frank Buss wrote:
> 
> What's the rationale for this behavior and where can I find the definition, that 
> it is not allowed? Doesn't look useful to me, because I can do this with a 
> procedure. In fact now I have written a procedure with an "out" parameter and it 
> works. But it is annoying to declare an extra temporary variable for it when I 
> call it. Would be nice, if I could do this with a function.

Modifying the state of a protected object requires exclusive access to it (a 
lock of some sort). Simply reading the state does not require a lock, and 
multiple reads may proceed in parallel. The definition of POs in 1995 used 
protected functions for non-exclusive, read-only access, and protected 
procedures and entries for exclusive write access. One can debate whether this 
was the best decision, but that's the way POs are defined.

One way to get a function that modifies the state is to have a function that 
calls a procedure of the PO and returns the value it obtains. Taken to its 
logical conclusion, the PO can be hidden in a pkg body and accessed through 
public subprograms.

-- 
Jeff Carter
"My little plum, I am like Robin Hood. I take from
the rich, and I give to the poor. ... Us poor."
Poppy
96

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

* Re: how to use "in out" for the "self" parameter in a function in a protected object?
  2017-09-17  8:36     ` Jeffrey R. Carter
@ 2017-10-03  0:08       ` Randy Brukardt
  0 siblings, 0 replies; 6+ messages in thread
From: Randy Brukardt @ 2017-10-03  0:08 UTC (permalink / raw)


"Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org> wrote in message 
news:oplc73$jqn$1@dont-email.me...
> On 09/17/2017 12:40 AM, Frank Buss wrote:
>>
>> What's the rationale for this behavior and where can I find the 
>> definition, that it is not allowed? Doesn't look useful to me, because I 
>> can do this with a procedure. In fact now I have written a procedure with 
>> an "out" parameter and it works. But it is annoying to declare an extra 
>> temporary variable for it when I call it. Would be nice, if I could do 
>> this with a function.
>
> Modifying the state of a protected object requires exclusive access to it 
> (a lock of some sort). Simply reading the state does not require a lock, 
> and multiple reads may proceed in parallel. The definition of POs in 1995 
> used protected functions for non-exclusive, read-only access, and 
> protected procedures and entries for exclusive write access. One can 
> debate whether this was the best decision, but that's the way POs are 
> defined.

Note that the Ada 2012 Corrigendum added aspect "Exclusive_Functions", which 
changes the behavior of protected functions to support exclusive read-only 
access. (This allows a PO to be used as a concurrency wrapper around some 
non-concurrent data structure without having to change all of the functions 
of the wrapped data structure into procedures.)

Some of us complained about tying read-only access to the use of protected 
functions long ago, but we got nowhere then and probably wouldn't get 
anywhere now, either. 98% of the time you can use a protected procedure 
instead, but there are cases where a procedure isn't possible and you are 
just stuck.

                                      Randy.



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

end of thread, other threads:[~2017-10-03  0:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-16 20:47 how to use "in out" for the "self" parameter in a function in a protected object? Frank Buss
2017-09-16 22:02 ` Jeffrey R. Carter
2017-09-16 22:40   ` Frank Buss
2017-09-17  1:35     ` Dennis Lee Bieber
2017-09-17  8:36     ` Jeffrey R. Carter
2017-10-03  0:08       ` Randy Brukardt

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