summaryrefslogtreecommitdiff
path: root/update.c
blob: 113b25cd1e67f00a56d1022f3ee29ec73beecaed (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Hurd unionfs
   Copyright (C) 2005 Free Software Foundation, Inc.
   Written by Gianluca Guida <glguida@gmail.com>.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or * (at your option) any later version.
 
   This program 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
   General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA.  */

/* Update thread: A clean way to solve locking issues of 
   root node update.  */

#include <errno.h>
#include <string.h>

#include "ncache.h"
#include "node.h"
#include "ulfs.h"

/* Reader lock is used by threads that are going to
   add/remove an ulfs; writer lock is hold by the 
   update thread.  */
static pthread_rwlock_t update_rwlock;
static pthread_cond_t update_wakeup;
static pthread_mutex_t update_lock;

static void *
_root_update_thread (void *arg)
{
  error_t err;
  
  while (1)
    {
      if (pthread_hurd_cond_wait_np (&update_wakeup, &update_lock))
	pthread_mutex_unlock (&update_lock);

      pthread_rwlock_wrlock (&update_rwlock);

      do 
	{
	  ulfs_check();
	  err = node_init_root (netfs_root_node);
	}
      while (err == ENOENT);

      if (err)
	{
	  fprintf (stderr, "update thread: got a %s\n", strerror (err));
	}

      ncache_reset ();

      pthread_rwlock_unlock (&update_rwlock);
    }

  return NULL;
}

void
root_update_schedule ()
{
  pthread_cond_signal (&update_wakeup);
}

void
root_update_disable ()
{
  pthread_rwlock_rdlock (&update_rwlock);
}

void
root_update_enable ()
{
  pthread_rwlock_unlock (&update_rwlock);
}

void
root_update_init()
{
  pthread_t thread;
  error_t err;

  pthread_mutex_init (&update_lock, NULL);
  pthread_rwlock_init (&update_rwlock, NULL);
  pthread_cond_init (&update_wakeup, NULL);

  err = pthread_create (&thread, NULL, _root_update_thread, NULL);
  if (!err)
    pthread_detach (thread);
  else
    perror ("root_update_init/pthread_create");
}