xref: /haiku/src/system/kernel/vm/PageCacheLocker.cpp (revision f2b4344867e97c3f4e742a1b4a15e6879644601a)
1 /*
2  * Copyright 2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "PageCacheLocker.h"
8 
9 #include <vm/VMCache.h>
10 
11 
12 bool
13 PageCacheLocker::_IgnorePage(vm_page* page)
14 {
15 	if (page->busy || page->State() == PAGE_STATE_WIRED
16 		|| page->State() == PAGE_STATE_FREE || page->State() == PAGE_STATE_CLEAR
17 		|| page->State() == PAGE_STATE_UNUSED || page->WiredCount() > 0)
18 		return true;
19 
20 	return false;
21 }
22 
23 
24 bool
25 PageCacheLocker::Lock(vm_page* page, bool dontWait)
26 {
27 	if (_IgnorePage(page))
28 		return false;
29 
30 	// Grab a reference to this cache.
31 	VMCache* cache = vm_cache_acquire_locked_page_cache(page, dontWait);
32 	if (cache == NULL)
33 		return false;
34 
35 	if (_IgnorePage(page)) {
36 		cache->ReleaseRefAndUnlock();
37 		return false;
38 	}
39 
40 	fPage = page;
41 	return true;
42 }
43 
44 
45 void
46 PageCacheLocker::Unlock()
47 {
48 	if (fPage == NULL)
49 		return;
50 
51 	fPage->Cache()->ReleaseRefAndUnlock();
52 
53 	fPage = NULL;
54 }
55