Essentially Ada’s protected objects encapsulate data and operations, and the operations then have automatic mutual exclusion. Guards can also then be placed on these operations for condition synchronization.
They used to be called protected records because the data is like a record structure.
You have protected procedures and protected entries and protected functions. What’s the difference?
The example he gave was a bounded buffer, where he needed to use entries for get and put because it needed to make sure the buffer was not empty/full respectively.
Entries are declared similarly to a procedure, except the start of it looks like:
entry Get (Item : out Data_Item) when Num /= 0 is
Looks a bit like Erlang guards really. Note that the barrier/guard is evaluated under mutual exclusion as well.
Calling a protected object is how you would expect. Something like:
My_Buffer.Put(Some_Data);
Which will now block indefinitely until you can put that data. Wow, this is exactly what the Chute package uses for Get_Ball(). So this is interesting:
If you want a timeout, you can use a select statement:
select
My_Buffer.Put(Some_Data);
or
delay 10.0;
-- do something else
end select;
You can also use else instead of or delay x which is essentially like or delay 0.
There is a 'Count attribute for Entries which maintains the number of tasks queued on the entry.
There are a couple of different examples in the slides of using all this:
Tasks can have private entries; entries that can’t be called from the outside. The Ada ‘requeue’ operation uses them, but we wont deal with that yet.
A family of entries is a way of declaring an array of entries with simple declarations. Sounds a bit odd. The example given was a multicast system (as opposed to the broadcast example) where you have different groups and an array of Booleans that are used as guards. The body of the example is in the slides and isn’t too complicated.
Program_Error. So for example you can’t use a delay statement, or task creation or activation or a select statement or an accept statement. That kind of thing.You may think that because Ada allows parallel reads that the Readers and Writers problem is simple because the runtime system can take care of it. However:
Therefore you have to use the same theory we used with standard monitors last time, which is to define an access control protocol for the read and write operations rather than encapsulate them in the protected object.
Full code detail is in the slides. The implementation basically uses a protected object only for Control, which is essentially a high level way of obtaining and releasing read/write locks by using commands like Control.Start_Read.
You should (exam?) be able to compare these with Java synchronized methods.
Comments
blog comments powered by Disqus