Merge from vendor branch BIND:
[dragonfly.git] / contrib / bind-9.3 / lib / isc / include / isc / rwlock.h
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2001, 2003  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: rwlock.h,v 1.18.2.3.2.1 2004/03/06 08:14:47 marka Exp $ */
19
20 #ifndef ISC_RWLOCK_H
21 #define ISC_RWLOCK_H 1
22
23 #include <isc/condition.h>
24 #include <isc/lang.h>
25 #include <isc/platform.h>
26 #include <isc/types.h>
27
28 ISC_LANG_BEGINDECLS
29
30 typedef enum {
31         isc_rwlocktype_none = 0,
32         isc_rwlocktype_read,
33         isc_rwlocktype_write
34 } isc_rwlocktype_t;
35
36 #ifdef ISC_PLATFORM_USETHREADS
37 struct isc_rwlock {
38         /* Unlocked. */
39         unsigned int            magic;
40         isc_mutex_t             lock;
41         /* Locked by lock. */
42         isc_condition_t         readable;
43         isc_condition_t         writeable;
44         isc_rwlocktype_t        type;
45
46         /* The number of threads that have the lock. */
47         unsigned int            active;
48
49         /*
50          * The number of lock grants made since the lock was last switched
51          * from reading to writing or vice versa; used in determining
52          * when the quota is reached and it is time to switch.
53          */
54         unsigned int            granted;
55         
56         unsigned int            readers_waiting;
57         unsigned int            writers_waiting;
58         unsigned int            read_quota;
59         unsigned int            write_quota;
60         isc_rwlocktype_t        original;
61 };
62 #else /* ISC_PLATFORM_USETHREADS */
63 struct isc_rwlock {
64         unsigned int            magic;
65         isc_rwlocktype_t        type;
66         unsigned int            active;
67 };
68 #endif /* ISC_PLATFORM_USETHREADS */
69
70
71 isc_result_t
72 isc_rwlock_init(isc_rwlock_t *rwl, unsigned int read_quota,
73                 unsigned int write_quota);
74
75 isc_result_t
76 isc_rwlock_lock(isc_rwlock_t *rwl, isc_rwlocktype_t type);
77
78 isc_result_t
79 isc_rwlock_trylock(isc_rwlock_t *rwl, isc_rwlocktype_t type);
80
81 isc_result_t
82 isc_rwlock_unlock(isc_rwlock_t *rwl, isc_rwlocktype_t type);
83
84 isc_result_t
85 isc_rwlock_tryupgrade(isc_rwlock_t *rwl);
86
87 void
88 isc_rwlock_downgrade(isc_rwlock_t *rwl);
89
90 void
91 isc_rwlock_destroy(isc_rwlock_t *rwl);
92
93 ISC_LANG_ENDDECLS
94
95 #endif /* ISC_RWLOCK_H */