summaryrefslogtreecommitdiff
path: root/libdiskfs
diff options
context:
space:
mode:
authorJustus Winter <4winter@informatik.uni-hamburg.de>2014-05-14 11:19:35 +0200
committerJustus Winter <4winter@informatik.uni-hamburg.de>2015-04-17 14:06:46 +0200
commitc16eed2cb64089bf7d958db0fe85352f4ceefb4d (patch)
tree3240f6e025fa4c4fb47d7d270f285a7f0f9612e4 /libdiskfs
parent8c050fb080c6e1981dc8e5a97a2313cd24e9b4b4 (diff)
libdiskfs: lock-less reference counting of nodes
* libdiskfs/diskfs.h (struct node): Use refcounts_t for reference counting. (diskfs_node_refcnt_lock): Remove. (diskfs_node_norefs,diskfs_drop_node): Change comments accordingly. * libdiskfs/init-init.c: Adjust accordingly. * libdiskfs/node-drop.c: Likewise. * libdiskfs/node-make.c: Likewise. * libdiskfs/node-nput.c: Likewise. * libdiskfs/node-nputl.c: Likewise. * libdiskfs/node-nref.c: Likewise. * libdiskfs/node-nrefl.c: Likewise. * libdiskfs/node-nrele.c: Likewise. * libdiskfs/node-nrelel.c: Likewise. * ext2fs/inode.c: Likewise. * fatfs/inode.c: Likewise. * isofs/inode.c: Likewise. * tmpfs/node.c: Likewise. * doc/hurd.texi: Likewise.
Diffstat (limited to 'libdiskfs')
-rw-r--r--libdiskfs/diskfs.h15
-rw-r--r--libdiskfs/init-init.c2
-rw-r--r--libdiskfs/node-drop.c11
-rw-r--r--libdiskfs/node-make.c3
-rw-r--r--libdiskfs/node-nput.c52
-rw-r--r--libdiskfs/node-nputl.c12
-rw-r--r--libdiskfs/node-nref.c9
-rw-r--r--libdiskfs/node-nrefl.c4
-rw-r--r--libdiskfs/node-nrele.c48
-rw-r--r--libdiskfs/node-nrelel.c9
10 files changed, 66 insertions, 99 deletions
diff --git a/libdiskfs/diskfs.h b/libdiskfs/diskfs.h
index 28182254..535fb399 100644
--- a/libdiskfs/diskfs.h
+++ b/libdiskfs/diskfs.h
@@ -96,8 +96,7 @@ struct node
pthread_mutex_t lock;
- int references; /* hard references */
- int light_references; /* light references */
+ refcounts_t refcounts;
mach_port_t sockaddr; /* address for S_IFSOCK shortcut */
@@ -198,8 +197,6 @@ extern volatile struct mapped_time_value *diskfs_mtime;
be done by format independent code. */
extern int diskfs_synchronous;
-extern pthread_spinlock_t diskfs_node_refcnt_lock;
-
extern int pager_port_type;
/* Whether the filesystem is currently writable or not. */
@@ -448,14 +445,15 @@ error_t diskfs_alloc_node (struct node *dp, mode_t mode, struct node **np);
void diskfs_free_node (struct node *np, mode_t mode);
/* Node NP has no more references; free local state, including *NP
- if it isn't to be retained. diskfs_node_refcnt_lock is held. */
+ if it isn't to be retained. */
void diskfs_node_norefs (struct node *np);
/* The user must define this function. Node NP has some light
references, but has just lost its last hard references. Take steps
so that if any light references can be freed, they are. NP is locked
as is the pager refcount lock. This function will be called after
- diskfs_lost_hardrefs. */
+ diskfs_lost_hardrefs. An additional light reference is acquired by
+ libdiskfs across calls to this function. */
void diskfs_try_dropping_softrefs (struct node *np);
/* The user must define this funcction. Node NP has some light
@@ -611,9 +609,8 @@ void diskfs_spawn_first_thread (ports_demuxer_type demuxer);
diskfs_init_completed once it has a valid proc and auth port. */
void diskfs_start_bootstrap ();
-/* Node NP now has no more references; clean all state. The
- _diskfs_node_refcnt_lock must be held, and will be released
- upon return. NP must be locked. */
+/* Node NP now has no more references; clean all state. NP must be
+ locked. */
void diskfs_drop_node (struct node *np);
/* Set on disk fields from NP->dn_stat; update ctime, atime, and mtime
diff --git a/libdiskfs/init-init.c b/libdiskfs/init-init.c
index ffb99e04..357960b8 100644
--- a/libdiskfs/init-init.c
+++ b/libdiskfs/init-init.c
@@ -41,8 +41,6 @@ int _diskfs_noatime;
struct hurd_port _diskfs_exec_portcell;
-pthread_spinlock_t diskfs_node_refcnt_lock = PTHREAD_SPINLOCK_INITIALIZER;
-
pthread_spinlock_t _diskfs_control_lock = PTHREAD_SPINLOCK_INITIALIZER;
int _diskfs_ncontrol_ports;
diff --git a/libdiskfs/node-drop.c b/libdiskfs/node-drop.c
index 83eb5909..455031bc 100644
--- a/libdiskfs/node-drop.c
+++ b/libdiskfs/node-drop.c
@@ -31,9 +31,8 @@ free_modreqs (struct modreq *mr)
}
-/* Node NP now has no more references; clean all state. The
- diskfs_node_refcnt_lock must be held, and will be released
- upon return. NP must be locked. */
+/* Node NP now has no more references; clean all state. NP must be
+ locked. */
void
diskfs_drop_node (struct node *np)
{
@@ -60,8 +59,7 @@ diskfs_drop_node (struct node *np)
and an nput. The next time through, this routine
will notice that the size is zero, and not have to
do anything. */
- np->references++;
- pthread_spin_unlock (&diskfs_node_refcnt_lock);
+ refcounts_unsafe_ref (&np->refcounts, NULL);
diskfs_truncate (np, 0);
/* Force allocsize to zero; if truncate consistently fails this
@@ -93,6 +91,7 @@ diskfs_drop_node (struct node *np)
assert (!np->sockaddr);
+ pthread_mutex_unlock(&np->lock);
+ pthread_mutex_destroy(&np->lock);
diskfs_node_norefs (np);
- pthread_spin_unlock (&diskfs_node_refcnt_lock);
}
diff --git a/libdiskfs/node-make.c b/libdiskfs/node-make.c
index ff0cc0d4..c7ca3b04 100644
--- a/libdiskfs/node-make.c
+++ b/libdiskfs/node-make.c
@@ -29,8 +29,7 @@ init_node (struct node *np, struct disknode *dn)
np->dn_stat_dirty = 0;
pthread_mutex_init (&np->lock, NULL);
- np->references = 1;
- np->light_references = 0;
+ refcounts_init (&np->refcounts, 1, 0);
np->owner = 0;
np->sockaddr = MACH_PORT_NULL;
diff --git a/libdiskfs/node-nput.c b/libdiskfs/node-nput.c
index 5043ad1a..d23c1037 100644
--- a/libdiskfs/node-nput.c
+++ b/libdiskfs/node-nput.c
@@ -26,56 +26,44 @@
void
diskfs_nput (struct node *np)
{
- int tried_drop_softrefs = 0;
+ struct references result;
- loop:
- pthread_spin_lock (&diskfs_node_refcnt_lock);
- assert (np->references);
- np->references--;
- if (np->references + np->light_references == 0)
- diskfs_drop_node (np);
- else if (np->references == 0 && !tried_drop_softrefs)
- {
- pthread_spin_unlock (&diskfs_node_refcnt_lock);
+ /* While we call the diskfs_try_dropping_softrefs, we need to hold
+ one reference. We use a weak reference for this purpose, which
+ we acquire by demoting our hard reference to a weak one. */
+ refcounts_demote (&np->refcounts, &result);
+ if (result.hard == 0)
+ {
/* This is our cue that something akin to "last process closes file"
in the POSIX.1 sense happened, so make sure any pending node time
updates now happen in a timely fashion. */
diskfs_set_node_times (np);
-
diskfs_lost_hardrefs (np);
if (!np->dn_stat.st_nlink)
{
- /* There are no links. If there are soft references that
- can be dropped, we can't let them postpone deallocation.
- So attempt to drop them. But that's a user-supplied
- routine, which might result in further recursive calls to
- the ref-counting system. So we have to reacquire our
- reference around the call to forestall disaster. */
- pthread_spin_lock (&diskfs_node_refcnt_lock);
- np->references++;
- pthread_spin_unlock (&diskfs_node_refcnt_lock);
-
if (np->sockaddr != MACH_PORT_NULL)
{
mach_port_deallocate (mach_task_self (), np->sockaddr);
np->sockaddr = MACH_PORT_NULL;
}
+ /* There are no links. If there are soft references that
+ can be dropped, we can't let them postpone deallocation.
+ So attempt to drop them. But that's a user-supplied
+ routine, which might result in further recursive calls to
+ the ref-counting system. This is not a problem, as we
+ hold a weak reference ourselves. */
diskfs_try_dropping_softrefs (np);
-
- /* But there's no value in looping forever in this
- routine; only try to drop soft refs once. */
- tried_drop_softrefs = 1;
-
- /* Now we can drop the reference back... */
- goto loop;
}
pthread_mutex_unlock (&np->lock);
}
+
+ /* Finally get rid of our reference. */
+ refcounts_deref_weak (&np->refcounts, &result);
+
+ if (result.hard == 0 && result.weak == 0)
+ diskfs_drop_node (np);
else
- {
- pthread_spin_unlock (&diskfs_node_refcnt_lock);
- pthread_mutex_unlock (&np->lock);
- }
+ pthread_mutex_unlock (&np->lock);
}
diff --git a/libdiskfs/node-nputl.c b/libdiskfs/node-nputl.c
index 19596654..8dac16e3 100644
--- a/libdiskfs/node-nputl.c
+++ b/libdiskfs/node-nputl.c
@@ -25,14 +25,10 @@
void
diskfs_nput_light (struct node *np)
{
- pthread_spin_lock (&diskfs_node_refcnt_lock);
- assert (np->light_references);
- np->light_references--;
- if (np->references + np->light_references == 0)
+ struct references result;
+ refcounts_deref_weak (&np->refcounts, &result);
+ if (result.hard == 0 && result.weak == 0)
diskfs_drop_node (np);
else
- {
- pthread_spin_unlock (&diskfs_node_refcnt_lock);
- pthread_mutex_unlock (&np->lock);
- }
+ pthread_mutex_unlock (&np->lock);
}
diff --git a/libdiskfs/node-nref.c b/libdiskfs/node-nref.c
index 13cea056..766a69c9 100644
--- a/libdiskfs/node-nref.c
+++ b/libdiskfs/node-nref.c
@@ -26,12 +26,9 @@
void
diskfs_nref (struct node *np)
{
- int new_hardref;
- pthread_spin_lock (&diskfs_node_refcnt_lock);
- np->references++;
- new_hardref = (np->references == 1);
- pthread_spin_unlock (&diskfs_node_refcnt_lock);
- if (new_hardref)
+ struct references result;
+ refcounts_ref (&np->refcounts, &result);
+ if (result.hard == 1)
{
pthread_mutex_lock (&np->lock);
diskfs_new_hardrefs (np);
diff --git a/libdiskfs/node-nrefl.c b/libdiskfs/node-nrefl.c
index 96922471..f7a823de 100644
--- a/libdiskfs/node-nrefl.c
+++ b/libdiskfs/node-nrefl.c
@@ -24,7 +24,5 @@
void
diskfs_nref_light (struct node *np)
{
- pthread_spin_lock (&diskfs_node_refcnt_lock);
- np->light_references++;
- pthread_spin_unlock (&diskfs_node_refcnt_lock);
+ refcounts_ref_weak (&np->refcounts, NULL);
}
diff --git a/libdiskfs/node-nrele.c b/libdiskfs/node-nrele.c
index cc680893..d9628469 100644
--- a/libdiskfs/node-nrele.c
+++ b/libdiskfs/node-nrele.c
@@ -28,38 +28,36 @@
void
diskfs_nrele (struct node *np)
{
- int tried_drop_softrefs = 0;
+ struct references result;
- loop:
- pthread_spin_lock (&diskfs_node_refcnt_lock);
- assert (np->references);
- np->references--;
- if (np->references + np->light_references == 0)
- {
- pthread_mutex_lock (&np->lock);
- diskfs_drop_node (np);
- }
- else if (np->references == 0)
+ /* While we call the diskfs_try_dropping_softrefs, we need to hold
+ one reference. We use a weak reference for this purpose, which
+ we acquire by demoting our hard reference to a weak one. */
+ refcounts_demote (&np->refcounts, &result);
+
+ if (result.hard == 0)
{
pthread_mutex_lock (&np->lock);
- pthread_spin_unlock (&diskfs_node_refcnt_lock);
diskfs_lost_hardrefs (np);
- if (!np->dn_stat.st_nlink && !tried_drop_softrefs)
+ if (!np->dn_stat.st_nlink)
{
- /* Same issue here as in nput; see that for explanation */
- pthread_spin_lock (&diskfs_node_refcnt_lock);
- np->references++;
- pthread_spin_unlock (&diskfs_node_refcnt_lock);
-
+ /* There are no links. If there are soft references that
+ can be dropped, we can't let them postpone deallocation.
+ So attempt to drop them. But that's a user-supplied
+ routine, which might result in further recursive calls to
+ the ref-counting system. This is not a problem, as we
+ hold a weak reference ourselves. */
diskfs_try_dropping_softrefs (np);
- tried_drop_softrefs = 1;
-
- /* Now we can drop the reference back... */
- pthread_mutex_unlock (&np->lock);
- goto loop;
}
pthread_mutex_unlock (&np->lock);
}
- else
- pthread_spin_unlock (&diskfs_node_refcnt_lock);
+
+ /* Finally get rid of our reference. */
+ refcounts_deref_weak (&np->refcounts, &result);
+
+ if (result.hard == 0 && result.weak == 0)
+ {
+ pthread_mutex_lock (&np->lock);
+ diskfs_drop_node (np);
+ }
}
diff --git a/libdiskfs/node-nrelel.c b/libdiskfs/node-nrelel.c
index ee53b227..dc4f920f 100644
--- a/libdiskfs/node-nrelel.c
+++ b/libdiskfs/node-nrelel.c
@@ -26,14 +26,11 @@
void
diskfs_nrele_light (struct node *np)
{
- pthread_spin_lock (&diskfs_node_refcnt_lock);
- assert (np->light_references);
- np->light_references--;
- if (np->references + np->light_references == 0)
+ struct references result;
+ refcounts_deref_weak (&np->refcounts, &result);
+ if (result.hard == 0 && result.weak == 0)
{
pthread_mutex_lock (&np->lock);
diskfs_drop_node (np);
}
- else
- pthread_spin_unlock (&diskfs_node_refcnt_lock);
}