1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . S Y N C H R O N O U S _ T A S K _ C O N T R O L --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
36 package body Ada.Synchronous_Task_Control is
37 use System.OS_Interface;
38 use type Interfaces.C.int;
44 function Current_State (S : Suspension_Object) return Boolean is
46 Result : Boolean := False;
49 -- Determine state by attempting to take the semaphore with
50 -- a 0 timeout value. Status = OK indicates the semaphore was
51 -- full, so reset it to the full state.
53 St := semTake (S.Sema, NO_WAIT);
56 -- Took the semaphore. Reset semaphore state to FULL
58 St := semGive (S.Sema);
68 procedure Set_False (S : in out Suspension_Object) is
71 -- Need to get the semaphore into the "empty" state.
72 -- On return, this task will have made the semaphore
73 -- empty (St = OK) or have left it empty.
74 St := semTake (S.Sema, NO_WAIT);
81 procedure Set_True (S : in out Suspension_Object) is
84 St := semGive (S.Sema);
87 ------------------------
88 -- Suspend_Until_True --
89 ------------------------
91 procedure Suspend_Until_True (S : in out Suspension_Object) is
94 -- Declare local exception so the mutex can still be reset
95 -- to full if Program_Error is raised
97 Task_Already_Pending : exception;
99 -- Determine whether another task is pending on the suspension
100 -- object. Should never be called from an ISR. Therefore semTake can
101 -- be called on the mutex
102 St := semTake (S.Mutex, NO_WAIT);
105 -- Wait for suspension object
107 St := semTake (S.Sema, WAIT_FOREVER);
108 St := semGive (S.Mutex);
111 -- Another task is pending on the suspension object
113 raise Task_Already_Pending;
116 when Task_Already_Pending =>
119 St := semGive (S.Mutex);
121 end Suspend_Until_True;
123 procedure Initialize (S : in out Suspension_Object) is
125 S.Sema := semBCreate (SEM_Q_FIFO, SEM_EMPTY);
127 -- Use simpler binary semaphore instead of VxWorks
128 -- mutual exclusion semaphore, because we don't need
129 -- the fancier semantics and their overhead.
131 S.Mutex := semBCreate (SEM_Q_FIFO, SEM_FULL);
134 procedure Finalize (S : in out Suspension_Object) is
137 St := semDelete (S.Sema);
138 St := semDelete (S.Mutex);
141 end Ada.Synchronous_Task_Control;