OSDN Git Service

New Language: Ada
[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 --                            $Revision: 1.6 $                               --
10 --                                                                          --
11 --           Copyright (C) 1999-2001 Ada Core Technologies, Inc.            --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- As a special exception,  if other files  instantiate  generics from this --
25 -- unit, or you link  this unit with other files  to produce an executable, --
26 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
27 -- covered  by the  GNU  General  Public  License.  This exception does not --
28 -- however invalidate  any other reasons why  the executable file  might be --
29 -- covered by the  GNU Public License.                                      --
30 --                                                                          --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 with GNAT.Task_Lock;
36
37 package body System.Global_Locks is
38
39    type String_Access is access String;
40
41    package TSL renames GNAT.Task_Lock;
42
43    Dir_Separator : Character;
44    pragma Import (C, Dir_Separator, "__gnat_dir_separator");
45
46    type Lock_File_Entry is
47       record
48          Dir  : String_Access;
49          File : String_Access;
50       end record;
51
52    Last_Lock  : Lock_Type := Null_Lock;
53    Lock_Table : array (Lock_Type range 1 .. 15) of Lock_File_Entry;
54
55    procedure Lock_File
56      (Dir     : String;
57       File    : String;
58       Wait    : Duration := 0.1;
59       Retries : Natural  := Natural'Last);
60    --  Create a lock file File in directory Dir. If the file  cannot be
61    --  locked because someone already owns the lock, this procedure
62    --  waits Wait seconds and retries at most Retries times. If the file
63    --  still cannot be locked, Lock_Error is raised. The default is to try
64    --  every second, almost forever (Natural'Last times).
65
66    ------------------
67    -- Acquire_Lock --
68    ------------------
69
70    procedure Acquire_Lock
71      (Lock : in out Lock_Type)
72    is
73    begin
74       Lock_File
75         (Lock_Table (Lock).Dir.all,
76          Lock_Table (Lock).File.all);
77    end Acquire_Lock;
78
79    -----------------
80    -- Create_Lock --
81    -----------------
82
83    procedure Create_Lock
84      (Lock : out Lock_Type;
85       Name : in String)
86    is
87       L : Lock_Type;
88
89    begin
90       TSL.Lock;
91       Last_Lock := Last_Lock + 1;
92       L := Last_Lock;
93       TSL.Unlock;
94
95       if L > Lock_Table'Last then
96          raise Lock_Error;
97       end if;
98
99       for J in reverse Name'Range loop
100          if Name (J) = Dir_Separator then
101             Lock_Table (L).Dir
102               := new String'(Name (Name'First .. J - 1));
103             Lock_Table (L).File
104               := new String'(Name (J + 1 .. Name'Last));
105             exit;
106          end if;
107       end loop;
108
109       if Lock_Table (L).Dir = null then
110          Lock_Table (L).Dir  := new String'(".");
111          Lock_Table (L).File := new String'(Name);
112       end if;
113
114       Lock := L;
115    end Create_Lock;
116
117    ---------------
118    -- Lock_File --
119    ---------------
120
121    procedure Lock_File
122      (Dir     : String;
123       File    : String;
124       Wait    : Duration := 0.1;
125       Retries : Natural  := Natural'Last)
126    is
127       C_Dir  : aliased String := Dir & ASCII.NUL;
128       C_File : aliased String := File & ASCII.NUL;
129
130       function Try_Lock (Dir, File : System.Address) return Integer;
131       pragma Import (C, Try_Lock, "__gnat_try_lock");
132
133    begin
134       for I in 0 .. Retries loop
135          if Try_Lock (C_Dir'Address, C_File'Address) = 1 then
136             return;
137          end if;
138          exit when I = Retries;
139          delay Wait;
140       end loop;
141       raise Lock_Error;
142    end Lock_File;
143
144    ------------------
145    -- Release_Lock --
146    ------------------
147
148    procedure Release_Lock
149      (Lock : in out Lock_Type)
150    is
151       S : aliased String :=
152         Lock_Table (Lock).Dir.all & Dir_Separator &
153         Lock_Table (Lock).File.all & ASCII.NUL;
154
155       procedure unlink (A : System.Address);
156       pragma Import (C, unlink, "unlink");
157
158    begin
159       unlink (S'Address);
160    end Release_Lock;
161
162 end System.Global_Locks;