OSDN Git Service

* 1aexcept.adb, 1aexcept.ads, 1ic.ads, 1ssecsta.adb,
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-gloloc.adb
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 --          Copyright (C) 1999-2001 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 is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
30 --                                                                          --
31 ------------------------------------------------------------------------------
32
33 with GNAT.Task_Lock;
34
35 package body System.Global_Locks is
36
37    type String_Access is access String;
38
39    package TSL renames GNAT.Task_Lock;
40
41    Dir_Separator : Character;
42    pragma Import (C, Dir_Separator, "__gnat_dir_separator");
43
44    type Lock_File_Entry is record
45       Dir  : String_Access;
46       File : String_Access;
47    end record;
48
49    Last_Lock  : Lock_Type := Null_Lock;
50    Lock_Table : array (Lock_Type range 1 .. 15) of Lock_File_Entry;
51
52    procedure Lock_File
53      (Dir     : String;
54       File    : String;
55       Wait    : Duration := 0.1;
56       Retries : Natural  := Natural'Last);
57    --  Create a lock file File in directory Dir. If the file  cannot be
58    --  locked because someone already owns the lock, this procedure
59    --  waits Wait seconds and retries at most Retries times. If the file
60    --  still cannot be locked, Lock_Error is raised. The default is to try
61    --  every second, almost forever (Natural'Last times).
62
63    ------------------
64    -- Acquire_Lock --
65    ------------------
66
67    procedure Acquire_Lock
68      (Lock : in out Lock_Type) is
69    begin
70       Lock_File
71         (Lock_Table (Lock).Dir.all,
72          Lock_Table (Lock).File.all);
73    end Acquire_Lock;
74
75    -----------------
76    -- Create_Lock --
77    -----------------
78
79    procedure Create_Lock
80      (Lock : out Lock_Type;
81       Name : in String)
82    is
83       L : Lock_Type;
84
85    begin
86       TSL.Lock;
87       Last_Lock := Last_Lock + 1;
88       L := Last_Lock;
89       TSL.Unlock;
90
91       if L > Lock_Table'Last then
92          raise Lock_Error;
93       end if;
94
95       for J in reverse Name'Range loop
96          if Name (J) = Dir_Separator then
97             Lock_Table (L).Dir
98               := new String'(Name (Name'First .. J - 1));
99             Lock_Table (L).File
100               := new String'(Name (J + 1 .. Name'Last));
101             exit;
102          end if;
103       end loop;
104
105       if Lock_Table (L).Dir = null then
106          Lock_Table (L).Dir  := new String'(".");
107          Lock_Table (L).File := new String'(Name);
108       end if;
109
110       Lock := L;
111    end Create_Lock;
112
113    ---------------
114    -- Lock_File --
115    ---------------
116
117    procedure Lock_File
118      (Dir     : String;
119       File    : String;
120       Wait    : Duration := 0.1;
121       Retries : Natural  := Natural'Last)
122    is
123       C_Dir  : aliased String := Dir & ASCII.NUL;
124       C_File : aliased String := File & ASCII.NUL;
125
126       function Try_Lock (Dir, File : System.Address) return Integer;
127       pragma Import (C, Try_Lock, "__gnat_try_lock");
128
129    begin
130       for I in 0 .. Retries loop
131          if Try_Lock (C_Dir'Address, C_File'Address) = 1 then
132             return;
133          end if;
134          exit when I = Retries;
135          delay Wait;
136       end loop;
137       raise Lock_Error;
138    end Lock_File;
139
140    ------------------
141    -- Release_Lock --
142    ------------------
143
144    procedure Release_Lock
145      (Lock : in out Lock_Type)
146    is
147       S : aliased String :=
148         Lock_Table (Lock).Dir.all & Dir_Separator &
149         Lock_Table (Lock).File.all & ASCII.NUL;
150
151       procedure unlink (A : System.Address);
152       pragma Import (C, unlink, "unlink");
153
154    begin
155       unlink (S'Address);
156    end Release_Lock;
157
158 end System.Global_Locks;