quota.h revision 193149
11573Srgrimes/*
21573Srgrimes * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
31573Srgrimes * Copyright (C) 2000, 2001  Internet Software Consortium.
41573Srgrimes *
51573Srgrimes * Permission to use, copy, modify, and/or distribute this software for any
61573Srgrimes * purpose with or without fee is hereby granted, provided that the above
71573Srgrimes * copyright notice and this permission notice appear in all copies.
81573Srgrimes *
91573Srgrimes * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
101573Srgrimes * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
111573Srgrimes * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
121573Srgrimes * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
131573Srgrimes * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
141573Srgrimes * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
151573Srgrimes * PERFORMANCE OF THIS SOFTWARE.
16148834Sstefanf */
171573Srgrimes
181573Srgrimes/* $Id: quota.h,v 1.16 2007/06/19 23:47:18 tbox Exp $ */
191573Srgrimes
201573Srgrimes#ifndef ISC_QUOTA_H
211573Srgrimes#define ISC_QUOTA_H 1
221573Srgrimes
231573Srgrimes/*****
241573Srgrimes ***** Module Info
251573Srgrimes *****/
261573Srgrimes
271573Srgrimes/*! \file isc/quota.h
281573Srgrimes *
291573Srgrimes * \brief The isc_quota_t object is a simple helper object for implementing
301573Srgrimes * quotas on things like the number of simultaneous connections to
311573Srgrimes * a server.  It keeps track of the amount of quota in use, and
321573Srgrimes * encapsulates the locking necessary to allow multiple tasks to
33148834Sstefanf * share a quota.
3484260Sobrien */
351573Srgrimes
361573Srgrimes/***
371573Srgrimes *** Imports.
381573Srgrimes ***/
391573Srgrimes
401573Srgrimes#include <isc/lang.h>
4184260Sobrien#include <isc/mutex.h>
421573Srgrimes#include <isc/types.h>
431573Srgrimes
441573Srgrimes/*****
451573Srgrimes ***** Types.
4684260Sobrien *****/
4784260Sobrien
4884260SobrienISC_LANG_BEGINDECLS
491573Srgrimes
501573Srgrimes/*% isc_quota structure */
5184260Sobrienstruct isc_quota {
5284260Sobrien	isc_mutex_t	lock; /*%< Locked by lock. */
5384260Sobrien	int 		max;
5484260Sobrien	int 		used;
5584260Sobrien	int		soft;
5684260Sobrien};
5784260Sobrien
581573Srgrimesisc_result_t
591573Srgrimesisc_quota_init(isc_quota_t *quota, int max);
60/*%<
61 * Initialize a quota object.
62 *
63 * Returns:
64 * 	ISC_R_SUCCESS
65 *	Other error	Lock creation failed.
66 */
67
68void
69isc_quota_destroy(isc_quota_t *quota);
70/*%<
71 * Destroy a quota object.
72 */
73
74void
75isc_quota_soft(isc_quota_t *quota, int soft);
76/*%<
77 * Set a soft quota.
78 */
79
80void
81isc_quota_max(isc_quota_t *quota, int max);
82/*%<
83 * Re-set a maximum quota.
84 */
85
86isc_result_t
87isc_quota_reserve(isc_quota_t *quota);
88/*%<
89 * Attempt to reserve one unit of 'quota'.
90 *
91 * Returns:
92 * \li 	#ISC_R_SUCCESS		Success
93 * \li	#ISC_R_SOFTQUOTA	Success soft quota reached
94 * \li	#ISC_R_QUOTA		Quota is full
95 */
96
97void
98isc_quota_release(isc_quota_t *quota);
99/*%<
100 * Release one unit of quota.
101 */
102
103isc_result_t
104isc_quota_attach(isc_quota_t *quota, isc_quota_t **p);
105/*%<
106 * Like isc_quota_reserve, and also attaches '*p' to the
107 * quota if successful (ISC_R_SUCCESS or ISC_R_SOFTQUOTA).
108 */
109
110void
111isc_quota_detach(isc_quota_t **p);
112/*%<
113 * Like isc_quota_release, and also detaches '*p' from the
114 * quota.
115 */
116
117ISC_LANG_ENDDECLS
118
119#endif /* ISC_QUOTA_H */
120