AdaPower Logged in as Guest
Ada Tools and Resources

Ada 95 Reference Manual
Ada Source Code Treasury
Bindings and Packages
Ada FAQ


Join >
Articles >
Ada FAQ >
Getting Started >
Home >
Books & Tutorials >
Source Treasury >
Packages for Reuse >
Latest Additions >
Ada Projects >
Press Releases >
Ada Audio / Video >
Home Pages >
Links >
Contact >
About >
Login >
Back
Using semaphores to call functions that are not thread safe and potentialy blocking (Mats Weber)

   protected Semaphore is
      entry Seize;
      entry Release;
   private
      Busy : Boolean := False;
   end Semaphore;

   protected body Semaphore is

      entry Seize when not Busy is
      begin
         Busy := True;
      end;

      entry Release when Busy is
      begin
         Busy := False;
      end;

   end Semaphore;

   function Gethostbyname (Name : in System.Address)
      return System.Address; -- pointer to hostent

   pragma Import(C, Gethostbyname);

   function Protected_Gethostbyname (Name : in System.Address)
      return System.Address is

      Result : System.Address;

   begin
      Semaphore.Seize;
      Result := Gethostbyname(Name);
      Semaphore.Release;
      return Result;
   exception
      when others =>
         Semaphore.Release;
         raise;
   end Protected_Gethostbyname;

it would be nice to put the call to gethostbyname inside a protected procedure (so that you need only one protected call instead of two), but this does not seem to be allowed as gethostbyname could be potentially blocking.


(c) 1998-2004 All Rights Reserved David Botton