2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de.
4 * Distributed under the terms of the MIT License.
6 #ifndef VM_ADDRESS_SPACE_LOCKING_H
7 #define VM_ADDRESS_SPACE_LOCKING_H
12 #include <vm/VMAddressSpace.h>
15 struct VMAddressSpace
;
20 class AddressSpaceLockerBase
{
22 static VMAddressSpace
* GetAddressSpaceByAreaID(area_id id
);
26 class AddressSpaceReadLocker
: private AddressSpaceLockerBase
{
28 AddressSpaceReadLocker(team_id team
);
29 AddressSpaceReadLocker(VMAddressSpace
* space
,
30 bool getNewReference
);
31 AddressSpaceReadLocker();
32 ~AddressSpaceReadLocker();
34 status_t
SetTo(team_id team
);
35 void SetTo(VMAddressSpace
* space
,
36 bool getNewReference
);
37 status_t
SetFromArea(area_id areaID
, VMArea
*& area
);
39 bool IsLocked() const { return fLocked
; }
45 VMAddressSpace
* AddressSpace() const { return fSpace
; }
48 VMAddressSpace
* fSpace
;
53 class AddressSpaceWriteLocker
: private AddressSpaceLockerBase
{
55 AddressSpaceWriteLocker(team_id team
);
56 AddressSpaceWriteLocker(VMAddressSpace
* space
,
57 bool getNewReference
);
58 AddressSpaceWriteLocker();
59 ~AddressSpaceWriteLocker();
61 status_t
SetTo(team_id team
);
62 void SetTo(VMAddressSpace
* space
,
63 bool getNewReference
);
64 status_t
SetFromArea(area_id areaID
, VMArea
*& area
);
65 status_t
SetFromArea(team_id team
, area_id areaID
,
66 bool allowKernel
, VMArea
*& area
);
67 status_t
SetFromArea(team_id team
, area_id areaID
,
70 bool IsLocked() const { return fLocked
; }
73 void DegradeToReadLock();
76 VMAddressSpace
* AddressSpace() const { return fSpace
; }
79 VMAddressSpace
* fSpace
;
85 class MultiAddressSpaceLocker
: private AddressSpaceLockerBase
{
87 MultiAddressSpaceLocker();
88 ~MultiAddressSpaceLocker();
90 inline status_t
AddTeam(team_id team
, bool writeLock
,
91 VMAddressSpace
** _space
= NULL
);
92 inline status_t
AddArea(area_id area
, bool writeLock
,
93 VMAddressSpace
** _space
= NULL
);
94 inline status_t
AddArea(VMArea
* area
, bool writeLock
,
95 VMAddressSpace
** _space
= NULL
);
97 status_t
AddAreaCacheAndLock(area_id areaID
,
98 bool writeLockThisOne
, bool writeLockOthers
,
99 VMArea
*& _area
, VMCache
** _cache
= NULL
);
103 bool IsLocked() const { return fLocked
; }
109 VMAddressSpace
* space
;
113 bool _ResizeIfNeeded();
114 int32
_IndexOfAddressSpace(VMAddressSpace
* space
)
116 status_t
_AddAddressSpace(VMAddressSpace
* space
,
117 bool writeLock
, VMAddressSpace
** _space
);
119 static int _CompareItems(const void* _a
, const void* _b
);
129 MultiAddressSpaceLocker::AddTeam(team_id team
, bool writeLock
,
130 VMAddressSpace
** _space
)
132 return _AddAddressSpace(VMAddressSpace::Get(team
), writeLock
, _space
);
137 MultiAddressSpaceLocker::AddArea(area_id area
, bool writeLock
,
138 VMAddressSpace
** _space
)
140 return _AddAddressSpace(GetAddressSpaceByAreaID(area
), writeLock
, _space
);
145 MultiAddressSpaceLocker::AddArea(VMArea
* area
, bool writeLock
,
146 VMAddressSpace
** _space
)
148 area
->address_space
->Get();
149 return _AddAddressSpace(area
->address_space
, writeLock
, _space
);
153 #endif // VM_ADDRESS_SPACE_LOCKING_H