refcount.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 1993,1994 The University of Utah and
  3. * the Computer Systems Laboratory (CSL). All rights reserved.
  4. *
  5. * Permission to use, copy, modify and distribute this software and its
  6. * documentation is hereby granted, provided that both the copyright
  7. * notice and this permission notice appear in all copies of the
  8. * software, derivative works or modified versions, and any portions
  9. * thereof, and that both notices appear in supporting documentation.
  10. *
  11. * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
  12. * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
  13. * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  14. *
  15. * CSL requests users of this software to return to csl-dist@cs.utah.edu any
  16. * improvements that they make and grant CSL redistribution rights.
  17. *
  18. * Author: Bryan Ford, University of Utah CSL
  19. */
  20. /*
  21. * File: refcount.h
  22. *
  23. * This defines the system-independent part of the atomic reference count data type.
  24. *
  25. */
  26. #ifndef _KERN_REFCOUNT_H_
  27. #define _KERN_REFCOUNT_H_
  28. #include <kern/macros.h>
  29. /* Unless the above include file specified otherwise,
  30. use the system-independent (unoptimized) atomic reference counter. */
  31. #ifndef MACHINE_REFCOUNT
  32. #include <kern/lock.h>
  33. struct RefCount {
  34. decl_simple_lock_data(,lock) /* lock for reference count */
  35. int ref_count; /* number of references */
  36. };
  37. typedef struct RefCount RefCount;
  38. #define refcount_init(refcount, refs) \
  39. MACRO_BEGIN \
  40. simple_lock_init(&(refcount)->lock); \
  41. ((refcount)->ref_count = (refs)); \
  42. MACRO_END
  43. #define refcount_take(refcount) \
  44. MACRO_BEGIN \
  45. simple_lock(&(refcount)->lock); \
  46. (refcount)->ref_count++; \
  47. simple_unlock(&(refcount)->lock); \
  48. MACRO_END
  49. #define refcount_drop(refcount, func) \
  50. MACRO_BEGIN \
  51. int new_value; \
  52. simple_lock(&(refcount)->lock); \
  53. new_value = --(refcount)->ref_count; \
  54. simple_unlock(&(refcount)->lock); \
  55. if (new_value == 0) { func; } \
  56. MACRO_END
  57. #endif /* MACHINE_REFCOUNT */
  58. #endif /* _KERN_REFCOUNT_H_ */