Annotation of parser3/src/lib/sdbm/sdbm_lock.c, revision 1.3
1.3 ! moko 1: /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
! 2: * applicable.
1.1 paf 3: *
1.3 ! moko 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.
1.1 paf 15: */
16:
1.2 moko 17: #include "pa_file_info.h"
18: #include "pa_file_io.h"
19: #include "pa_sdbm.h"
1.1 paf 20:
21: #include "sdbm_private.h"
22: #include "sdbm_tune.h"
23:
24: /* NOTE: this function blocks until it acquires the lock */
1.2 moko 25: pa_status_t pa_sdbm_lock(pa_sdbm_t *db, int type)
1.1 paf 26: {
1.2 moko 27: pa_status_t status;
1.1 paf 28:
1.2 moko 29: if (!(type == PA_FLOCK_SHARED || type == PA_FLOCK_EXCLUSIVE))
30: return PA_EINVAL;
1.1 paf 31:
32: if (db->flags & SDBM_EXCLUSIVE_LOCK) {
33: ++db->lckcnt;
1.2 moko 34: return PA_SUCCESS;
1.1 paf 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: */
1.2 moko 41: if (type == PA_FLOCK_EXCLUSIVE)
42: return PA_EINVAL;
1.1 paf 43: ++db->lckcnt;
1.2 moko 44: return PA_SUCCESS;
1.1 paf 45: }
46: /*
47: * zero size: either a fresh database, or one with a single,
48: * unsplit data page: dirpage is all zeros.
49: */
1.2 moko 50: if ((status = pa_file_lock(db->dirf, type)) == PA_SUCCESS)
1.1 paf 51: {
1.2 moko 52: pa_finfo_t finfo;
53: if ((status = pa_file_info_get(&finfo, PA_FINFO_SIZE, db->dirf))
54: != PA_SUCCESS) {
55: (void) pa_file_unlock(db->dirf);
1.1 paf 56: return status;
57: }
58:
59: SDBM_INVALIDATE_CACHE(db, finfo);
60:
61: ++db->lckcnt;
1.2 moko 62: if (type == PA_FLOCK_SHARED)
1.1 paf 63: db->flags |= SDBM_SHARED_LOCK;
1.2 moko 64: else if (type == PA_FLOCK_EXCLUSIVE)
1.1 paf 65: db->flags |= SDBM_EXCLUSIVE_LOCK;
66: }
67: return status;
68: }
69:
1.2 moko 70: pa_status_t pa_sdbm_unlock(pa_sdbm_t *db)
1.1 paf 71: {
72: if (!(db->flags & (SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK)))
1.2 moko 73: return PA_EINVAL;
1.1 paf 74: if (--db->lckcnt > 0)
1.2 moko 75: return PA_SUCCESS;
1.1 paf 76: db->flags &= ~(SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK);
1.2 moko 77: return pa_file_unlock(db->dirf);
1.1 paf 78: }
E-mail: