summaryrefslogtreecommitdiff
path: root/libhurd-mm/pager.c
blob: 5ac52b3c0ddb45363b49867a1f8e0dc22f6960a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* map.c - Generic map implementation.
   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
   Written by Neal H. Walfield <neal@gnu.org>.

   This file is part of the GNU Hurd.

   GNU Hurd is free software: you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with GNU Hurd.  If not, see
   <http://www.gnu.org/licenses/>.  */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include "pager.h"

extern ss_mutex_t maps_lock;

bool
pager_init (struct pager *pager)
{
  assert (pager->fault);
  assert (pager->length);
  assert ((pager->length & (PAGESIZE - 1)) == 0);

  debug (3, "Creating pager with size %x (%d pages)",
	 pager->length, pager->length / PAGESIZE);

  pager->maps = 0;
  pager->lock = 0;

  return true;
}

void
pager_deinit (struct pager *pager)
{
  maps_lock_lock ();

  /* Destroy all but one references with MAPS_LOCK held.  When we
     destroy the last reference, map_destroy will call the pager's no
     ref call back, which may destroy PAGER.  */

  ss_mutex_lock (&pager->lock);
  assert (pager->maps);
  for (;;)
    {
      struct map *map = pager->maps;
      map_disconnect (map);

      if (! map->map_list_next)
	/* The last reference.  */
	{
	  ss_mutex_unlock (&pager->lock);
	  maps_lock_unlock ();

	  map_destroy (map);

	  return;
	}

      ss_mutex_unlock (&pager->lock);

      /* It is safe to call with with MAPS_LOCK held as we know that
	 this is not the last refence.  */
      map_destroy (map);

      ss_mutex_lock (&pager->lock);
    }
}