1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT LIBRARY COMPONENTS                          --
4--                                                                          --
5--                      G N A T . S E M A P H O R E S                       --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--                     Copyright (C) 2003-2010, AdaCore                     --
10--                                                                          --
11-- GNAT is free software;  you can  redistribute it  and/or modify it under --
12-- terms of the  GNU General Public License as published  by the Free Soft- --
13-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
17--                                                                          --
18-- As a special exception under Section 7 of GPL version 3, you are granted --
19-- additional permissions described in the GCC Runtime Library Exception,   --
20-- version 3.1, as published by the Free Software Foundation.               --
21--                                                                          --
22-- You should have received a copy of the GNU General Public License and    --
23-- a copy of the GCC Runtime Library Exception along with this program;     --
24-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25-- <http://www.gnu.org/licenses/>.                                          --
26--                                                                          --
27-- GNAT was originally developed  by the GNAT team at  New York University. --
28-- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
29--                                                                          --
30------------------------------------------------------------------------------
31
32--  This package provides classic counting semaphores and binary semaphores.
33--  Both types are visibly defined as protected types so that users can make
34--  conditional and timed calls when appropriate.
35
36with System;
37
38package GNAT.Semaphores is
39
40   Default_Ceiling : constant System.Priority := System.Default_Priority;
41   --  A convenient value for the priority discriminants that follow
42
43   ------------------------
44   -- Counting_Semaphore --
45   ------------------------
46
47   protected type Counting_Semaphore
48      (Initial_Value : Natural;
49      --  A counting semaphore contains an internal counter.  The initial
50      --  value of this counter is set by clients via the discriminant.
51
52      Ceiling : System.Priority)
53      --  Users must specify the ceiling priority for the object. If the
54      --  Real-Time Systems Annex is not in use this value is not important.
55   is
56      pragma Priority (Ceiling);
57
58      entry Seize;
59      --  Blocks caller until/unless the semaphore's internal counter is
60      --  greater than zero. Decrements the semaphore's internal counter when
61      --  executed.
62
63      procedure Release;
64      --  Increments the semaphore's internal counter
65
66   private
67      Count : Natural := Initial_Value;
68   end Counting_Semaphore;
69
70   ----------------------
71   -- Binary_Semaphore --
72   ----------------------
73
74   protected type Binary_Semaphore
75     (Initially_Available : Boolean;
76      --  Binary semaphores are either available or not; there is no internal
77      --  count involved. The discriminant value determines whether the
78      --  individual object is initially available.
79
80      Ceiling : System.Priority)
81      --  Users must specify the ceiling priority for the object. If the
82      --  Real-Time Systems Annex is not in use this value is not important.
83   is
84      pragma Priority (Ceiling);
85
86      entry Seize;
87      --  Blocks the caller unless/until semaphore is available. After
88      --  execution the semaphore is no longer available.
89
90      procedure Release;
91      --  Makes the semaphore available
92
93   private
94      Available : Boolean := Initially_Available;
95   end Binary_Semaphore;
96
97end GNAT.Semaphores;
98