summaryrefslogtreecommitdiff
path: root/libfshelp/rlock.h
blob: 5c620279060929bf2cad1dc1608451cef60b813a (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
/*
   Copyright (C) 2001, 2014-2019 Free Software Foundation

   Written by Neal H Walfield <neal@cs.uml.edu>

   This file is part of the GNU Hurd.

   The GNU Hurd 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, or (at your option)
   any later version.

   The 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 General Public License for more details.

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

#ifndef FSHELP_RLOCK_H
#define FSHELP_RLOCK_H

#include <pthread.h>
#include <string.h>

struct rlock_linked_list
{
  struct rlock_list *next;
  struct rlock_list **prevp;
};

struct rlock_list
{
  loff_t start;
  loff_t len;
  int type;

  struct rlock_linked_list node;
  struct rlock_linked_list po;

  pthread_cond_t wait;
  int waiting;

  void *po_id;
};

extern inline error_t
rlock_list_init (struct rlock_peropen *po, struct rlock_list *l)
{
  memset (l, 0, sizeof (struct rlock_list));
  pthread_cond_init (&l->wait, NULL);
  l->po_id = po->locks;
  return 0;
}

/* void list_list (X ={po,node}, struct rlock_list **head,
		   struct rlock_list *node)

   Insert a node in the given list, X, in sorted order.  */
#define list_link(X, head, node)				\
	do							\
	  {							\
	    struct rlock_list **e;				\
	    for (e = head;					\
		 *e && ((*e)->start < node->start);		\
		 e = &(*e)->X.next)				\
	      ;							\
	    node->X.next = *e;					\
	    if (node->X.next)					\
	      node->X.next->X.prevp = &node->X.next;		\
	    node->X.prevp = e;					\
	    *e = node;						\
	  }							\
	while (0)

/* void list_unlock (X = {po,node}, struct rlock_list *node)  */
#define list_unlink(X, node)					\
	do							\
	  {							\
	    *node->X.prevp = node->X.next;			\
	    if (node->X.next)					\
	      node->X.next->X.prevp = node->X.prevp;		\
	  }							\
	while (0)

#endif /* FSHELP_RLOCK_H */