1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
------------------------------------------------------------------------------
-- --
-- FLORIST (FSU Implementation of POSIX.5) COMPONENTS --
-- --
-- P O S I X . S E M A P H O R E S --
-- --
-- B o d y --
-- --
-- --
-- Copyright (C) 1996-1997 Florida State University --
-- Copyright (C) 1998-2010, AdaCore --
-- --
-- This file is a component of FLORIST, an implementation of an Ada API --
-- for the POSIX OS services, for use with the GNAT Ada compiler and --
-- the FSU Gnu Ada Runtime Library (GNARL). The interface is intended --
-- to be close to that specified in IEEE STD 1003.5: 1990 and IEEE STD --
-- 1003.5b: 1996. --
-- --
-- FLORIST is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the --
-- Free Software Foundation; either version 2, or (at your option) any --
-- later version. FLORIST is distributed in the hope that it will be --
-- useful, but WITHOUT ANY WARRANTY; without even the implied warranty --
-- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. You should have received a --
-- copy of the GNU General Public License distributed with GNARL; see --
-- file COPYING. If not, write to the Free Software Foundation, 59 --
-- Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
-- --
-- --
-- --
-- --
-- --
-- --
-- --
------------------------------------------------------------------------------
with POSIX.Implementation,
POSIX.Permissions.Implementation,
Unchecked_Conversion;
package body POSIX.Semaphores is
use POSIX.C,
POSIX.Implementation,
POSIX.Permissions.Implementation;
function To_int is new Unchecked_Conversion (Bits, int);
function To_int is
new Unchecked_Conversion (Semaphore_Descriptor, ptr_as_int);
procedure Check_And_Restore_Signals
(Result : Semaphore_Descriptor;
Masked_Signals : Signal_Masking;
Old_Mask : Signal_Mask_Access);
pragma Inline (Check_And_Restore_Signals);
procedure Check_And_Restore_Signals
(Result : Semaphore_Descriptor;
Masked_Signals : Signal_Masking;
Old_Mask : Signal_Mask_Access) is
begin
if To_int (Result) = -1 then
Restore_Signals_And_Raise_POSIX_Error
(Masked_Signals, Old_Mask);
else
Restore_Signals (Masked_Signals, Old_Mask);
end if;
end Check_And_Restore_Signals;
procedure Validate (Sem : Semaphore_Descriptor);
pragma Inline (Validate);
procedure Validate (Sem : Semaphore_Descriptor) is
begin
if Sem = null then
Raise_POSIX_Error (Invalid_Argument);
end if;
end Validate;
---------------------------------
-- Initialize --
---------------------------------
function sem_init
(s : Semaphore_Descriptor;
pshared : int;
value : unsigned) return int;
pragma Import (C, sem_init, sem_init_LINKNAME);
procedure Initialize
(Sem : in out Semaphore;
Value : Natural;
Is_Shared : Boolean := False) is
begin
Check (sem_init (Sem.Sem'Unchecked_Access,
Boolean'Pos (Is_Shared), unsigned (Value)));
end Initialize;
---------------------------------
-- Descriptor_Of --
---------------------------------
function Descriptor_Of (Sem : Semaphore) return Semaphore_Descriptor is
begin
return Sem.Sem'Unchecked_Access;
end Descriptor_Of;
---------------------------------
-- Finalize --
---------------------------------
function sem_destroy (sem : Semaphore_Descriptor) return int;
pragma Import (C, sem_destroy, sem_destroy_LINKNAME);
procedure Finalize (Sem : in out Semaphore) is
begin
Check (sem_destroy (Sem.Sem'Unchecked_Access));
end Finalize;
---------------------------------
-- Open --
---------------------------------
function sem_open
(name : char_ptr;
oflag : int;
mode : mode_t;
value : unsigned) return Semaphore_Descriptor;
function sem_open
(name : char_ptr;
oflag : int) return Semaphore_Descriptor;
pragma Import (C, sem_open, sem_open_LINKNAME);
function Open
(Name : POSIX.POSIX_String;
Masked_Signals : POSIX.Signal_Masking := POSIX.RTS_Signals)
return Semaphore_Descriptor is
Result : Semaphore_Descriptor;
Name_With_NUL : POSIX_String := Name & NUL;
Old_Mask : aliased Signal_Mask;
begin
Mask_Signals (Masked_Signals, Old_Mask'Unchecked_Access);
Result := sem_open
(Name_With_NUL (Name_With_NUL'First)'Unchecked_Access, 0);
Check_And_Restore_Signals
(Result, Masked_Signals, Old_Mask'Unchecked_Access);
return Result;
end Open;
---------------------------------
-- Open_Or_Create --
---------------------------------
function Open_Or_Create
(Name : POSIX.POSIX_String;
Permissions : POSIX.Permissions.Permission_Set;
Value : Natural;
Options : POSIX.IO.Open_Option_Set := POSIX.IO.Empty_Set;
Masked_Signals : POSIX.Signal_Masking := POSIX.RTS_Signals)
return Semaphore_Descriptor is
Result : Semaphore_Descriptor;
Name_With_NUL : POSIX_String := Name & NUL;
Old_Mask : aliased Signal_Mask;
begin
Mask_Signals (Masked_Signals, Old_Mask'Unchecked_Access);
Result := sem_open
(Name_With_NUL (Name_With_NUL'First)'Unchecked_Access,
To_int (Option_Set (Options).Option or O_CREAT),
Form_C_Permission (Permissions),
unsigned (Value));
Check_And_Restore_Signals
(Result, Masked_Signals, Old_Mask'Unchecked_Access);
return Result;
end Open_Or_Create;
---------------------------------
-- Close --
---------------------------------
function sem_close (sem : Semaphore_Descriptor) return int;
pragma Import (C, sem_close, sem_close_LINKNAME);
procedure Close (Sem : in out Semaphore_Descriptor) is
begin
Check (sem_close (Sem));
end Close;
---------------------------------
-- Unlink_Semaphore --
---------------------------------
function sem_unlink (name : char_ptr) return int;
pragma Import (C, sem_unlink, sem_unlink_LINKNAME);
procedure Unlink_Semaphore (Name : POSIX.POSIX_String) is
Name_With_NUL : POSIX_String := Name & NUL;
begin
Check (sem_unlink
(Name_With_NUL (Name_With_NUL'First)'Unchecked_Access));
end Unlink_Semaphore;
---------------------------------
-- Wait --
---------------------------------
function sem_wait (sem : Semaphore_Descriptor) return int;
pragma Import (C, sem_wait, sem_wait_LINKNAME);
procedure Wait
(Sem : Semaphore_Descriptor;
Masked_Signals : POSIX.Signal_Masking := POSIX.RTS_Signals) is
Result : int;
Old_Mask : aliased Signal_Mask;
begin
Validate (Sem);
Mask_Signals (Masked_Signals, Old_Mask'Unchecked_Access);
Result := sem_wait (Sem);
Check_NNeg_And_Restore_Signals
(Result, Masked_Signals, Old_Mask'Unchecked_Access);
end Wait;
---------------------------------
-- Try_Wait --
---------------------------------
function sem_trywait (sem : Semaphore_Descriptor) return int;
pragma Import (C, sem_trywait, sem_trywait_LINKNAME);
function Try_Wait (Sem : Semaphore_Descriptor) return Boolean is
Result : int;
begin
Validate (Sem);
Result := sem_trywait (Sem);
if Result = 0 then
return True;
elsif Fetch_Errno = EAGAIN then
return False;
else
Raise_POSIX_Error;
-- return statement to suppress compiler warning message
return False;
end if;
end Try_Wait;
---------------------------------
-- Post --
---------------------------------
function sem_post (sem : Semaphore_Descriptor) return int;
pragma Import (C, sem_post, sem_post_LINKNAME);
procedure Post (Sem : Semaphore_Descriptor) is
begin
Validate (Sem);
Check (sem_post (Sem));
end Post;
---------------------------------
-- Get_Value --
---------------------------------
function sem_getvalue
(sem : Semaphore_Descriptor;
sval : access int) return int;
pragma Import (C, sem_getvalue, sem_getvalue_LINKNAME);
function Get_Value (Sem : Semaphore_Descriptor) return Integer is
Value : aliased int;
begin
Validate (Sem);
Check (sem_getvalue (Sem, Value'Unchecked_Access));
return Integer (Value);
end Get_Value;
end POSIX.Semaphores;
|