1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . G L O B A L _ L O C K S --
9 -- Copyright (C) 1999-2002 Free Software Foundation, Inc. --
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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with System.Soft_Links;
35 -- used for Lock_Task, Unlock_Task
37 package body System.Global_Locks is
39 type String_Access is access String;
41 Dir_Separator : Character;
42 pragma Import (C, Dir_Separator, "__gnat_dir_separator");
44 type Lock_File_Entry is record
49 Last_Lock : Lock_Type := Null_Lock;
50 Lock_Table : array (Lock_Type range 1 .. 15) of Lock_File_Entry;
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).
67 procedure Acquire_Lock (Lock : in out Lock_Type) is
70 (Lock_Table (Lock).Dir.all,
71 Lock_Table (Lock).File.all);
78 procedure Create_Lock (Lock : out Lock_Type; Name : in String) is
82 System.Soft_Links.Lock_Task.all;
83 Last_Lock := Last_Lock + 1;
85 System.Soft_Links.Unlock_Task.all;
87 if L > Lock_Table'Last then
91 for J in reverse Name'Range loop
92 if Name (J) = Dir_Separator then
93 Lock_Table (L).Dir := new String'(Name (Name'First .. J - 1));
94 Lock_Table (L).File := new String'(Name (J + 1 .. Name'Last));
99 if Lock_Table (L).Dir = null then
100 Lock_Table (L).Dir := new String'(".");
101 Lock_Table (L).File := new String'(Name);
114 Wait : Duration := 0.1;
115 Retries : Natural := Natural'Last)
117 C_Dir : aliased String := Dir & ASCII.NUL;
118 C_File : aliased String := File & ASCII.NUL;
120 function Try_Lock (Dir, File : System.Address) return Integer;
121 pragma Import (C, Try_Lock, "__gnat_try_lock");
124 for I in 0 .. Retries loop
125 if Try_Lock (C_Dir'Address, C_File'Address) = 1 then
129 exit when I = Retries;
140 procedure Release_Lock (Lock : in out Lock_Type) is
141 S : aliased String :=
142 Lock_Table (Lock).Dir.all & Dir_Separator &
143 Lock_Table (Lock).File.all & ASCII.NUL;
145 procedure unlink (A : System.Address);
146 pragma Import (C, unlink, "unlink");
152 end System.Global_Locks;