bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr-util / dbm / sdbm / sdbm_lock.c
1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
2  * applicable.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr_file_info.h"
18 #include "apr_file_io.h"
19 #include "apr_sdbm.h"
20
21 #include "sdbm_private.h"
22 #include "sdbm_tune.h"
23
24 /* NOTE: this function blocks until it acquires the lock */
25 APU_DECLARE(apr_status_t) apr_sdbm_lock(apr_sdbm_t *db, int type)
26 {
27     apr_status_t status;
28
29     if (!(type == APR_FLOCK_SHARED || type == APR_FLOCK_EXCLUSIVE))
30         return APR_EINVAL;
31
32     if (db->flags & SDBM_EXCLUSIVE_LOCK) {
33         ++db->lckcnt;
34         return APR_SUCCESS;
35     }
36     else if (db->flags & SDBM_SHARED_LOCK) {
37         /*
38          * Cannot promote a shared lock to an exlusive lock
39          * in a cross-platform compatibile manner.
40          */
41         if (type == APR_FLOCK_EXCLUSIVE)
42             return APR_EINVAL;
43         ++db->lckcnt;
44         return APR_SUCCESS;
45     }
46     /*
47      * zero size: either a fresh database, or one with a single,
48      * unsplit data page: dirpage is all zeros.
49      */
50     if ((status = apr_file_lock(db->dirf, type)) == APR_SUCCESS) 
51     {
52         apr_finfo_t finfo;
53         if ((status = apr_file_info_get(&finfo, APR_FINFO_SIZE, db->dirf))
54                 != APR_SUCCESS) {
55             (void) apr_file_unlock(db->dirf);
56             return status;
57         }
58
59         SDBM_INVALIDATE_CACHE(db, finfo);
60
61         ++db->lckcnt;
62         if (type == APR_FLOCK_SHARED)
63             db->flags |= SDBM_SHARED_LOCK;
64         else if (type == APR_FLOCK_EXCLUSIVE)
65             db->flags |= SDBM_EXCLUSIVE_LOCK;
66     }
67     return status;
68 }
69
70 APU_DECLARE(apr_status_t) apr_sdbm_unlock(apr_sdbm_t *db)
71 {
72     if (!(db->flags & (SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK)))
73         return APR_EINVAL;
74     if (--db->lckcnt > 0)
75         return APR_SUCCESS;
76     db->flags &= ~(SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK);
77     return apr_file_unlock(db->dirf);
78 }