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 Ada Core Technologies, 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 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--  This implementation is specific to NT.
35
36with GNAT.Task_Lock;
37
38with Interfaces.C.Strings;
39with System.OS_Interface;
40
41package body System.Global_Locks is
42
43   package TSL renames GNAT.Task_Lock;
44   package OSI renames System.OS_Interface;
45   package ICS renames Interfaces.C.Strings;
46
47   subtype Lock_File_Entry is OSI.HANDLE;
48
49   Last_Lock  : Lock_Type := Null_Lock;
50   Lock_Table : array (Lock_Type range 1 .. 15) of Lock_File_Entry;
51
52   -----------------
53   -- Create_Lock --
54   -----------------
55
56   procedure Create_Lock
57     (Lock : out Lock_Type;
58      Name : in String)
59   is
60      L : Lock_Type;
61
62   begin
63      TSL.Lock;
64      Last_Lock := Last_Lock + 1;
65      L := Last_Lock;
66      TSL.Unlock;
67
68      if L > Lock_Table'Last then
69         raise Lock_Error;
70      end if;
71
72      Lock_Table (L) :=
73        OSI.CreateMutex (null, OSI.BOOL (False), ICS.New_String (Name));
74      Lock := L;
75   end Create_Lock;
76
77   ------------------
78   -- Acquire_Lock --
79   ------------------
80
81   procedure Acquire_Lock
82     (Lock : in out Lock_Type)
83   is
84      use type OSI.DWORD;
85
86      Res : OSI.DWORD;
87   begin
88      Res := OSI.WaitForSingleObject (Lock_Table (Lock), OSI.Wait_Infinite);
89
90      if Res = OSI.WAIT_FAILED then
91         raise Lock_Error;
92      end if;
93   end Acquire_Lock;
94
95   ------------------
96   -- Release_Lock --
97   ------------------
98
99   procedure Release_Lock
100     (Lock : in out Lock_Type)
101   is
102      use type OSI.BOOL;
103
104      Res : OSI.BOOL;
105   begin
106      Res := OSI.ReleaseMutex (Lock_Table (Lock));
107
108      if Res = OSI.False then
109         raise Lock_Error;
110      end if;
111   end Release_Lock;
112
113end System.Global_Locks;
114