* arm.c (arm_compute_initial_elimination_offset): If optimizing for
[official-gcc.git] / gcc / ada / s-gloloc.adb
blobb1fe1b3138bc449f5c6e4ca068e29b19f9100d50
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . G L O B A L _ L O C K S --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1999-2001 Free Software Foundation, Inc. --
11 -- --
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. --
22 -- --
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. --
29 -- --
30 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
31 -- --
32 ------------------------------------------------------------------------------
34 with GNAT.Task_Lock;
36 package body System.Global_Locks is
38 type String_Access is access String;
40 package TSL renames GNAT.Task_Lock;
42 Dir_Separator : Character;
43 pragma Import (C, Dir_Separator, "__gnat_dir_separator");
45 type Lock_File_Entry is record
46 Dir : String_Access;
47 File : String_Access;
48 end record;
50 Last_Lock : Lock_Type := Null_Lock;
51 Lock_Table : array (Lock_Type range 1 .. 15) of Lock_File_Entry;
53 procedure Lock_File
54 (Dir : String;
55 File : String;
56 Wait : Duration := 0.1;
57 Retries : Natural := Natural'Last);
58 -- Create a lock file File in directory Dir. If the file cannot be
59 -- locked because someone already owns the lock, this procedure
60 -- waits Wait seconds and retries at most Retries times. If the file
61 -- still cannot be locked, Lock_Error is raised. The default is to try
62 -- every second, almost forever (Natural'Last times).
64 ------------------
65 -- Acquire_Lock --
66 ------------------
68 procedure Acquire_Lock
69 (Lock : in out Lock_Type) is
70 begin
71 Lock_File
72 (Lock_Table (Lock).Dir.all,
73 Lock_Table (Lock).File.all);
74 end Acquire_Lock;
76 -----------------
77 -- Create_Lock --
78 -----------------
80 procedure Create_Lock
81 (Lock : out Lock_Type;
82 Name : in String)
84 L : Lock_Type;
86 begin
87 TSL.Lock;
88 Last_Lock := Last_Lock + 1;
89 L := Last_Lock;
90 TSL.Unlock;
92 if L > Lock_Table'Last then
93 raise Lock_Error;
94 end if;
96 for J in reverse Name'Range loop
97 if Name (J) = Dir_Separator then
98 Lock_Table (L).Dir
99 := new String'(Name (Name'First .. J - 1));
100 Lock_Table (L).File
101 := new String'(Name (J + 1 .. Name'Last));
102 exit;
103 end if;
104 end loop;
106 if Lock_Table (L).Dir = null then
107 Lock_Table (L).Dir := new String'(".");
108 Lock_Table (L).File := new String'(Name);
109 end if;
111 Lock := L;
112 end Create_Lock;
114 ---------------
115 -- Lock_File --
116 ---------------
118 procedure Lock_File
119 (Dir : String;
120 File : String;
121 Wait : Duration := 0.1;
122 Retries : Natural := Natural'Last)
124 C_Dir : aliased String := Dir & ASCII.NUL;
125 C_File : aliased String := File & ASCII.NUL;
127 function Try_Lock (Dir, File : System.Address) return Integer;
128 pragma Import (C, Try_Lock, "__gnat_try_lock");
130 begin
131 for I in 0 .. Retries loop
132 if Try_Lock (C_Dir'Address, C_File'Address) = 1 then
133 return;
134 end if;
135 exit when I = Retries;
136 delay Wait;
137 end loop;
138 raise Lock_Error;
139 end Lock_File;
141 ------------------
142 -- Release_Lock --
143 ------------------
145 procedure Release_Lock
146 (Lock : in out Lock_Type)
148 S : aliased String :=
149 Lock_Table (Lock).Dir.all & Dir_Separator &
150 Lock_Table (Lock).File.all & ASCII.NUL;
152 procedure unlink (A : System.Address);
153 pragma Import (C, unlink, "unlink");
155 begin
156 unlink (S'Address);
157 end Release_Lock;
159 end System.Global_Locks;