OSDN Git Service

PR bootstrap/11932
[pf3gnuchains/gcc-fork.git] / gcc / ada / 4zsytaco.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUNTIME COMPONENTS                          --
4 --                                                                          --
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          --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --            Copyright (C) 1992-2004 Free Software Foundation, Inc.        --
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 2,  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.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 with Interfaces.C;
35
36 package body Ada.Synchronous_Task_Control is
37    use System.OS_Interface;
38    use type Interfaces.C.int;
39
40    -------------------
41    -- Current_State --
42    -------------------
43
44    function Current_State (S : Suspension_Object) return Boolean is
45       St     : STATUS;
46       Result : Boolean := False;
47
48    begin
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.
52
53       St := semTake (S.Sema, NO_WAIT);
54
55       --  If we took the semaphore, reset semaphore state to FULL
56
57       if St = OK then
58          Result := True;
59          St := semGive (S.Sema);
60       end if;
61
62       return Result;
63    end Current_State;
64
65    ---------------
66    -- Set_False --
67    ---------------
68
69    procedure Set_False (S : in out Suspension_Object) is
70       St : STATUS;
71
72    begin
73       --  Need to get the semaphore into the "empty" state.
74       --  On return, this task will have made the semaphore
75       --  empty (St = OK) or have left it empty.
76
77       St := semTake (S.Sema, NO_WAIT);
78       pragma Assert (St = OK);
79    end Set_False;
80
81    --------------
82    -- Set_True --
83    --------------
84
85    procedure Set_True (S : in out Suspension_Object) is
86       St : STATUS;
87       pragma Unreferenced (St);
88    begin
89       St := semGive (S.Sema);
90    end Set_True;
91
92    ------------------------
93    -- Suspend_Until_True --
94    ------------------------
95
96    procedure Suspend_Until_True (S : in out Suspension_Object) is
97       St : STATUS;
98
99    begin
100       --  Determine whether another task is pending on the suspension
101       --  object. Should never be called from an ISR. Therefore semTake can
102       --  be called on the mutex
103
104       St := semTake (S.Mutex, NO_WAIT);
105
106       if St = OK then
107
108          --  Wait for suspension object
109
110          St := semTake (S.Sema, WAIT_FOREVER);
111          St := semGive (S.Mutex);
112
113       else
114          --  Another task is pending on the suspension object
115
116          raise Program_Error;
117       end if;
118    end Suspend_Until_True;
119
120    ----------------
121    -- Initialize --
122    ----------------
123
124    procedure Initialize (S : in out Suspension_Object) is
125    begin
126       S.Sema := semBCreate (SEM_Q_FIFO, SEM_EMPTY);
127
128       --  Use simpler binary semaphore instead of VxWorks
129       --  mutual exclusion semaphore, because we don't need
130       --  the fancier semantics and their overhead.
131
132       S.Mutex := semBCreate (SEM_Q_FIFO, SEM_FULL);
133    end Initialize;
134
135    --------------
136    -- Finalize --
137    --------------
138
139    procedure Finalize (S : in out Suspension_Object) is
140       St : STATUS;
141       pragma Unreferenced (St);
142    begin
143       St := semDelete (S.Sema);
144       St := semDelete (S.Mutex);
145    end Finalize;
146
147 end Ada.Synchronous_Task_Control;