quota.h revision 153816
1/*
2 * Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id: quota.h,v 1.8.12.6 2005/08/11 15:00:08 marka Exp $ */
19
20#ifndef ISC_QUOTA_H
21#define ISC_QUOTA_H 1
22
23/*****
24 ***** Module Info
25 *****/
26
27/*
28 * Quota
29 *
30 * The isc_quota_t object is a simple helper object for implementing
31 * quotas on things like the number of simultaneous connections to
32 * a server.  It keeps track of the amount of quota in use, and
33 * encapsulates the locking necessary to allow multiple tasks to
34 * share a quota.
35 */
36
37/***
38 *** Imports.
39 ***/
40
41#include <isc/lang.h>
42#include <isc/mutex.h>
43#include <isc/types.h>
44
45/*****
46 ***** Types.
47 *****/
48
49ISC_LANG_BEGINDECLS
50
51struct isc_quota {
52	isc_mutex_t	lock;
53	/* Locked by lock. */
54	int 		max;
55	int 		used;
56	int		soft;
57};
58
59isc_result_t
60isc_quota_init(isc_quota_t *quota, int max);
61/*
62 * Initialize a quota object.
63 *
64 * Returns:
65 * 	ISC_R_SUCCESS
66 *	Other error	Lock creation failed.
67 */
68
69void
70isc_quota_destroy(isc_quota_t *quota);
71/*
72 * Destroy a quota object.
73 */
74
75void
76isc_quota_soft(isc_quota_t *quota, int soft);
77/*
78 * Turn on/off soft quotas.
79 */
80
81void
82isc_quota_max(isc_quota_t *quota, int max);
83/*
84 * Re-set a maximum quota.
85 */
86
87isc_result_t
88isc_quota_reserve(isc_quota_t *quota);
89/*
90 * Attempt to reserve one unit of 'quota'.
91 *
92 * Returns:
93 * 	ISC_R_SUCCESS	Success
94 *	ISC_R_SOFTQUOTA	Success soft quota reached
95 *	ISC_R_QUOTA	Quota is full
96 */
97
98void
99isc_quota_release(isc_quota_t *quota);
100/*
101 * Release one unit of quota.
102 */
103
104isc_result_t
105isc_quota_attach(isc_quota_t *quota, isc_quota_t **p);
106/*
107 * Like isc_quota_reserve, and also attaches '*p' to the
108 * quota if successful (ISC_R_SUCCESS or ISC_R_SOFTQUOTA).
109 */
110
111void
112isc_quota_detach(isc_quota_t **p);
113/*
114 * Like isc_quota_release, and also detaches '*p' from the
115 * quota.
116 */
117
118ISC_LANG_ENDDECLS
119
120#endif /* ISC_QUOTA_H */
121