summaryrefslogtreecommitdiff
path: root/kern/refcount.h
blob: 7fd6cdfb252ff1e8e4e92f83008b1b44cc0dcac0 (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
/*
 * Copyright (c) 1993,1994 The University of Utah and
 * the Computer Systems Laboratory (CSL).  All rights reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
 * IS" CONDITION.  THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
 * improvements that they make and grant CSL redistribution rights.
 *
 *      Author: Bryan Ford, University of Utah CSL
 */
/*
 *	File:	refcount.h
 *
 *	This defines the system-independent part of the atomic reference count data type.
 *
 */

#ifndef	_KERN_REFCOUNT_H_
#define _KERN_REFCOUNT_H_

#include <kern/macro_help.h>

#include "refcount.h" /*XXX*/

/* Unless the above include file specified otherwise,
   use the system-independent (unoptimized) atomic reference counter.  */
#ifndef MACHINE_REFCOUNT

#include <kern/lock.h>

struct RefCount {
	decl_simple_lock_data(,lock)	/* lock for reference count */
	int		ref_count;	/* number of references */
};
typedef struct RefCount RefCount;

#define refcount_init(refcount, refs)			\
	MACRO_BEGIN					\
		simple_lock_init(&(refcount)->lock);	\
		((refcount)->ref_count = (refs));	\
	MACRO_END

#define refcount_take(refcount)				\
	MACRO_BEGIN					\
		simple_lock(&(refcount)->lock);		\
		(refcount)->ref_count++;		\
		simple_unlock(&(refcount)->lock);	\
	MACRO_END

#define refcount_drop(refcount, func)			\
	MACRO_BEGIN					\
		int new_value;				\
		simple_lock(&(refcount)->lock);		\
		new_value = --(refcount)->ref_count;	\
		simple_unlock(&(refcount)->lock);	\
		if (new_value == 0) { func; }		\
	MACRO_END

#endif

#endif _KERN_REFCOUNT_H_