mdbm_handle_pool.h
Go to the documentation of this file.
1 /* Copyright 2013 Yahoo! Inc. */
2 /* See LICENSE in the root of the distribution for licensing details. */
3 
4 #ifndef MDBM_HANDLE_POOL_H
5 #define MDBM_HANDLE_POOL_H
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 #include <mdbm.h>
12 
13 typedef struct mdbm_pool_s mdbm_pool_t;
14 
15 /**
16  * Parse and return the configured pool size for the this process
17  *
18  * \param value configuration setting value. The format of the value
19  * is comma (,) separated list name=value pairs where
20  * name is the application basename (e.g. yapache, yjava_daemon) and the
21  * value is the number of handles in the pool.
22  *
23  * \return Number of configured number of mdbm handles for the pool
24  */
25 int mdbm_pool_parse_pool_size(const char *value);
26 
27 /**
28  * Verifies and returns updated values for number of handles in the pool.
29  * The maximum useful size of handles is affected by both the maximum allowed
30  * number of open files (NOFILE) and the maximum number of processes (NPROC):
31 
32  * NOFILE limit: Each mdbm handle requires two open file entries (one for the
33  * db file and one for the lock file). Thus, if ynetdblib sees that the
34  * number of requested mdbm handles exceeds 3/4 of the maximum allowed
35  * number of open files ( determined by getrlimit(RLIMIT_NOFILE, ...),
36  * it will scale back accordingly.
37  *
38  * NPROC limit: Since each thread requires only one mdbm handle, and not all
39  * threads need to open ynetdblib mdbm files, there is no good
40  * reason to support having as many handles as the maximum number
41  * of processes. Thus, if ynetdblib sees that the number of
42  * requested mdbm handles exceeds 3/4 of the maximum allowed number
43  * of processes (determined by getrlimit(RLIMIT_NPROC, ...)
44  * it will scale back accordingly.
45  *
46  * \param vals integer array that includes the values to be checked. If the
47  * value is larger than the system limit as described above, the array is
48  * updated to reflect the new value
49  * \param count the number of integers in the vals array
50  */
51 void mdbm_pool_verify_pool_size(int *vals, int count);
52 
53 /**
54  * Create a pool of mdbm handles
55  *
56  * \param db MDBM handle that will be duplicated
57  * \param size number of duplicated handles
58  *
59  * \return pointer to newly created pool object. This object must
60  * destroyed using mdbm_pool_destroy_pool function.
61  */
62 mdbm_pool_t *mdbm_pool_create_pool(MDBM *db, int size);
63 
64 /**
65  * Delete a mdbm handle pool
66  *
67  * \param pool pointer to a pool object that was returned from
68  * calling mdbm_pool_create_pool function
69  *
70  * \return 1 if successful or 0 for failure.
71  */
73 
74 /**
75  * Acquire an MDBM handle for database operations. If there are no
76  * available handles in the pool, the function will block until
77  * there is one available.
78  *
79  * \param pool pointer to a pool object that was returned from
80  * calling mdbm_pool_create_pool function
81  *
82  * \return pointer to MDBM handle or NULL for failure. The MDBM
83  * handle must be returned to the pool with the mdbm_pool_release_handle function
84  */
86 
87 /**
88  * Release MDBM handle back to the pool
89  *
90  * \param pool pointer to a pool object that was returned from
91  * calling mdbm_pool_create_pool function
92  * \param db pointer to the MDBM handle to be released back to the pool.
93  * This pointer must have been acquired from the mdbm_pool_acquire_handle call.
94  *
95  * \return 1 if successful or 0 for failure.
96  */
98 
99 /**
100  * Acquire an exclusive MDBM handle for database operations.
101  * The original MDBM handle that was used for duplication is returned
102  * to the caller. The function guarnatees that all of the duplicated
103  * MDBM handles are in the pool and nobody is using them.
104  *
105  * \param pool pointer to a pool object that was returned from
106  * calling mdbm_pool_create_pool function
107  *
108  * \return pointer to MDBM handle or NULL for failure. The MDBM
109  * handle must be returned to the pool with the mdbm_pool_release_handle function
110  */
112 
113 /**
114  * Release an exclusive MDBM handle
115  *
116  * \param pool pointer to a pool object that was returned from
117  * calling mdbm_pool_create_pool function
118  * \param db pointer to the exclusive MDBM handle to be released.
119  * This pointer must have been acquired from the mdbm_pool_acquire_excl_handle call.
120  *
121  * \return 1 if successful or 0 for failure.
122  */
124 
125 /**
126  * \page example_usage "Using Core-Tech MDBM Handle Pool in your application"
127  *
128  * Properties wishing to use mdbm in multi-threaded applications must
129  * make sure each thread uses a unique handle to carry out db operations.
130  * There are two changes that are required to use this library:
131  *
132  * 1] Create an mdbm handle pool by duplicating your original mdbm handle
133  *
134  * \code
135  *
136  * // open mdbm file
137  *
138  * if ((db = mdbm_open(fn, flags, 0, 0, 0)) == NULL)
139  * return;
140  *
141  * // create the mdbm handle pool
142  *
143  * if ((pool = mdbm_pool_create_pool(db, pool_size, NULL)) == NULL) {
144  * mdbm_close(db);
145  * return;
146  * }
147  *
148  * \endcode
149  *
150  * 2] Obtain a unique handle in your thread for your db operation
151  *
152  * \code
153  *
154  * // get mdbm handle from the pool
155  *
156  * if ((dbh = mdbm_pool_acquire_handle(pool, NULL)) == NULL)
157  * return;
158  *
159  * // lock the mdbm handle
160  *
161  * if (mdbm_lock(dbh) != 1) {
162  * mdbm_pool_release_handle(pool, dbh, NULL);
163  * return;
164  * }
165  *
166  * // carry out your mdbm operations ...
167  *
168  * // unlock and return the handle to the pool
169  *
170  * mdbm_unlock(dbh);
171  * mdbm_pool_release_handle(pool, dbh, NULL);
172  *
173  * \endcode
174  *
175  */
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 #endif /* MDBM_HANDLE_POOL_H */