OSDN Git Service

2008-03-26 Robert Dewar <dewar@adacore.com>
[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-2008, 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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, 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 System.Soft_Links;
35
36 package body System.Global_Locks is
37
38    type String_Access is access String;
39
40    Dir_Separator : Character;
41    pragma Import (C, Dir_Separator, "__gnat_dir_separator");
42
43    type Lock_File_Entry is record
44       Dir  : String_Access;
45       File : String_Access;
46    end record;
47
48    Last_Lock  : Lock_Type := Null_Lock;
49    Lock_Table : array (Lock_Type range 1 .. 15) of Lock_File_Entry;
50
51    procedure Lock_File
52      (Dir     : String;
53       File    : String;
54       Wait    : Duration := 0.1;
55       Retries : Natural  := Natural'Last);
56    --  Create a lock file File in directory Dir. If the file  cannot be
57    --  locked because someone already owns the lock, this procedure
58    --  waits Wait seconds and retries at most Retries times. If the file
59    --  still cannot be locked, Lock_Error is raised. The default is to try
60    --  every second, almost forever (Natural'Last times).
61
62    ------------------
63    -- Acquire_Lock --
64    ------------------
65
66    procedure Acquire_Lock (Lock : in out Lock_Type) is
67    begin
68       Lock_File
69         (Lock_Table (Lock).Dir.all,
70          Lock_Table (Lock).File.all);
71    end Acquire_Lock;
72
73    -----------------
74    -- Create_Lock --
75    -----------------
76
77    procedure Create_Lock (Lock : out Lock_Type; Name : String) is
78       L : Lock_Type;
79
80    begin
81       System.Soft_Links.Lock_Task.all;
82       Last_Lock := Last_Lock + 1;
83       L := Last_Lock;
84       System.Soft_Links.Unlock_Task.all;
85
86       if L > Lock_Table'Last then
87          raise Lock_Error;
88       end if;
89
90       for J in reverse Name'Range loop
91          if Name (J) = Dir_Separator then
92             Lock_Table (L).Dir := new String'(Name (Name'First .. J - 1));
93             Lock_Table (L).File := new String'(Name (J + 1 .. Name'Last));
94             exit;
95          end if;
96       end loop;
97
98       if Lock_Table (L).Dir = null then
99          Lock_Table (L).Dir  := new String'(".");
100          Lock_Table (L).File := new String'(Name);
101       end if;
102
103       Lock := L;
104    end Create_Lock;
105
106    ---------------
107    -- Lock_File --
108    ---------------
109
110    procedure Lock_File
111      (Dir     : String;
112       File    : String;
113       Wait    : Duration := 0.1;
114       Retries : Natural  := Natural'Last)
115    is
116       C_Dir  : aliased String := Dir & ASCII.NUL;
117       C_File : aliased String := File & ASCII.NUL;
118
119       function Try_Lock (Dir, File : System.Address) return Integer;
120       pragma Import (C, Try_Lock, "__gnat_try_lock");
121
122    begin
123       for I in 0 .. Retries loop
124          if Try_Lock (C_Dir'Address, C_File'Address) = 1 then
125             return;
126          end if;
127
128          exit when I = Retries;
129          delay Wait;
130       end loop;
131
132       raise Lock_Error;
133    end Lock_File;
134
135    ------------------
136    -- Release_Lock --
137    ------------------
138
139    procedure Release_Lock (Lock : in out Lock_Type) is
140       S : aliased String :=
141             Lock_Table (Lock).Dir.all & Dir_Separator &
142             Lock_Table (Lock).File.all & ASCII.NUL;
143
144       procedure unlink (A : System.Address);
145       pragma Import (C, unlink, "unlink");
146
147    begin
148       unlink (S'Address);
149    end Release_Lock;
150
151 end System.Global_Locks;