Simple thread-safe implementation of reference count.
Showing
1 changed file
with
93 additions
and
0 deletions
mailbox/refcount.c
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 1999, 2000, 2001, 2003 Free Software Foundation, Inc. | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Lesser General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 2 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General Public | ||
15 | License along with this library; if not, write to the Free Software | ||
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ | ||
17 | |||
18 | #ifdef HAVE_CONFIG_H | ||
19 | # include <config.h> | ||
20 | #endif | ||
21 | |||
22 | #include <stdlib.h> | ||
23 | #include <errno.h> | ||
24 | |||
25 | #include <mailutils/monitor.h> | ||
26 | #include <mailutils/refcount.h> | ||
27 | |||
28 | int | ||
29 | mu_refcount_create (mu_refcount_t *prefcount) | ||
30 | { | ||
31 | int status = 0; | ||
32 | mu_refcount_t refcount; | ||
33 | if (prefcount == NULL) | ||
34 | return EINVAL; | ||
35 | refcount = calloc (1, sizeof *refcount); | ||
36 | if (refcount != NULL) | ||
37 | { | ||
38 | refcount->ref = 1; | ||
39 | status = monitor_create (&(refcount->lock)); | ||
40 | if (status == 0) | ||
41 | { | ||
42 | *prefcount = refcount; | ||
43 | } | ||
44 | else | ||
45 | { | ||
46 | free (refcount); | ||
47 | } | ||
48 | } | ||
49 | else | ||
50 | { | ||
51 | status = ENOMEM; | ||
52 | } | ||
53 | return status; | ||
54 | } | ||
55 | |||
56 | void | ||
57 | mu_refcount_destroy (mu_refcount_t *prefcount) | ||
58 | { | ||
59 | if (prefcount && *prefcount) | ||
60 | { | ||
61 | mu_refcount_t refcount = *prefcount; | ||
62 | monitor_destroy (&refcount->lock); | ||
63 | free (refcount); | ||
64 | *prefcount = NULL; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | int | ||
69 | mu_refcount_inc (mu_refcount_t refcount) | ||
70 | { | ||
71 | int count = 0; | ||
72 | if (refcount) | ||
73 | { | ||
74 | monitor_lock (refcount->lock); | ||
75 | count = ++refcount->ref; | ||
76 | monitor_unlock (refcount->lock); | ||
77 | } | ||
78 | return count; | ||
79 | } | ||
80 | |||
81 | int | ||
82 | mu_refcount_dec (mu_refcount_t refcount) | ||
83 | { | ||
84 | int count = 0; | ||
85 | if (refcount) | ||
86 | { | ||
87 | monitor_lock (refcount->lock); | ||
88 | if (refcount->ref) | ||
89 | count = --refcount->ref; | ||
90 | monitor_unlock (refcount->lock); | ||
91 | } | ||
92 | return count; | ||
93 | } |
-
Please register or sign in to post a comment