X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;ds=sidebyside;f=libcfs%2Flibcfs%2Fuser-lock.c;h=ab5cf6ea040edaadd66a14c9936590e36b8b1ada;hb=f95393b0d0a59cf3dc2f29cffc35dcc4cc9d7728;hp=a5ef616810e72f5291f53ed39b24e57e4b24da01;hpb=e903932500fc08b143467ce5a1c2702df35d8f0f;p=fs%2Flustre-release.git diff --git a/libcfs/libcfs/user-lock.c b/libcfs/libcfs/user-lock.c index a5ef616..ab5cf6e 100644 --- a/libcfs/libcfs/user-lock.c +++ b/libcfs/libcfs/user-lock.c @@ -1,26 +1,41 @@ /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*- * vim:expandtab:shiftwidth=8:tabstop=8: * - * Copyright (C) 2004 Cluster File Systems, Inc. - * Author: Nikita Danilov + * GPL HEADER START + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * - * This file is part of Lustre, http://www.lustre.org. + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 only, + * as published by the Free Software Foundation. * - * Lustre is free software; you can redistribute it and/or modify it under the - * terms of version 2 of the GNU General Public License as published by the - * Free Software Foundation. + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License version 2 for more details (a copy is included + * in the LICENSE file that accompanied this code). * - * Lustre is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. + * You should have received a copy of the GNU General Public License + * version 2 along with this program; If not, see + * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf * - * You should have received a copy of the GNU General Public License along - * with Lustre; if not, write to the Free Software Foundation, Inc., 675 Mass - * Ave, Cambridge, MA 02139, USA. + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + * + * GPL HEADER END + */ +/* + * Copyright 2008 Sun Microsystems, Inc. All rights reserved + * Use is subject to license terms. + */ +/* + * This file is part of Lustre, http://www.lustre.org/ + * Lustre is a trademark of Sun Microsystems, Inc. * - * Implementation of portable time API for user-level. + * libcfs/libcfs/user-lock.c * + * Author: Nikita Danilov */ /* Implementations of portable synchronization APIs for liblustre */ @@ -35,19 +50,18 @@ #ifndef __KERNEL__ -#include #include /* * Optional debugging (magic stamping and checking ownership) can be added. */ -#if 0 /* * spin_lock * * - spin_lock_init(x) * - spin_lock(x) + * - spin_lock_nested(x, subclass) * - spin_unlock(x) * - spin_trylock(x) * @@ -57,41 +71,41 @@ * No-op implementation. */ -void spin_lock_init(spinlock_t *lock) +void cfs_spin_lock_init(cfs_spinlock_t *lock) { LASSERT(lock != NULL); (void)lock; } -void spin_lock(spinlock_t *lock) +void cfs_spin_lock(cfs_spinlock_t *lock) { (void)lock; } -void spin_unlock(spinlock_t *lock) +void cfs_spin_unlock(cfs_spinlock_t *lock) { (void)lock; } -int spin_trylock(spinlock_t *lock) +int cfs_spin_trylock(cfs_spinlock_t *lock) { (void)lock; return 1; } -void spin_lock_bh_init(spinlock_t *lock) +void cfs_spin_lock_bh_init(cfs_spinlock_t *lock) { LASSERT(lock != NULL); (void)lock; } -void spin_lock_bh(spinlock_t *lock) +void cfs_spin_lock_bh(cfs_spinlock_t *lock) { LASSERT(lock != NULL); (void)lock; } -void spin_unlock_bh(spinlock_t *lock) +void cfs_spin_unlock_bh(cfs_spinlock_t *lock) { LASSERT(lock != NULL); (void)lock; @@ -104,41 +118,26 @@ void spin_unlock_bh(spinlock_t *lock) * - __down(x) * - __up(x) */ -struct semaphore {}; -void sema_init(struct semaphore *s, int val) +void cfs_sema_init(cfs_semaphore_t *s, int val) { LASSERT(s != NULL); (void)s; (void)val; } -void __down(struct semaphore *s) +void __down(cfs_semaphore_t *s) { LASSERT(s != NULL); (void)s; } -void __up(struct semaphore *s) +void __up(cfs_semaphore_t *s) { LASSERT(s != NULL); (void)s; } -/* - * Mutex: - * - * - init_mutex(x) - * - init_mutex_locked(x) - * - mutex_up(x) - * - mutex_down(x) - */ - -#define mutex_up(s) __up(s) -#define mutex_down(s) __down(s) - -#define init_mutex(x) sema_init(x, 1) -#define init_mutex_locked(x) sema_init(x, 0) /* * Completion: @@ -147,24 +146,52 @@ void __up(struct semaphore *s) * - complete(c) * - wait_for_completion(c) */ -struct completion {}; -void init_completion(struct completion *c) +static cfs_wait_handler_t wait_handler; + +void cfs_init_completion_module(cfs_wait_handler_t handler) +{ + wait_handler = handler; +} + +int cfs_call_wait_handler(int timeout) +{ + if (!wait_handler) + return -ENOSYS; + return wait_handler(timeout); +} + +void cfs_init_completion(cfs_completion_t *c) { LASSERT(c != NULL); - (void)c; + c->done = 0; + cfs_waitq_init(&c->wait); } -void complete(struct completion *c) +void cfs_complete(cfs_completion_t *c) { LASSERT(c != NULL); - (void)c; + c->done = 1; + cfs_waitq_signal(&c->wait); } -void wait_for_completion(struct completion *c) +void cfs_wait_for_completion(cfs_completion_t *c) { LASSERT(c != NULL); - (void)c; + do { + if (cfs_call_wait_handler(1000) < 0) + break; + } while (c->done == 0); +} + +int cfs_wait_for_completion_interruptible(cfs_completion_t *c) +{ + LASSERT(c != NULL); + do { + if (cfs_call_wait_handler(1000) < 0) + break; + } while (c->done == 0); + return 0; } /* @@ -177,60 +204,64 @@ void wait_for_completion(struct completion *c) * - down_write(x) * - up_write(x) */ -struct rw_semaphore {}; -void init_rwsem(struct rw_semaphore *s) +void cfs_init_rwsem(cfs_rw_semaphore_t *s) { LASSERT(s != NULL); (void)s; } -void down_read(struct rw_semaphore *s) +void cfs_down_read(cfs_rw_semaphore_t *s) { LASSERT(s != NULL); (void)s; } -int down_read_trylock(struct rw_semaphore *s) +int cfs_down_read_trylock(cfs_rw_semaphore_t *s) { LASSERT(s != NULL); (void)s; return 1; } -void down_write(struct rw_semaphore *s) +void cfs_down_write(cfs_rw_semaphore_t *s) { LASSERT(s != NULL); (void)s; } -int down_write_trylock(struct rw_semaphore *s) +int cfs_down_write_trylock(cfs_rw_semaphore_t *s) { LASSERT(s != NULL); (void)s; return 1; } -void up_read(struct rw_semaphore *s) +void cfs_up_read(cfs_rw_semaphore_t *s) { LASSERT(s != NULL); (void)s; } -void up_write(struct rw_semaphore *s) +void cfs_up_write(cfs_rw_semaphore_t *s) +{ + LASSERT(s != NULL); + (void)s; +} + +void cfs_fini_rwsem(cfs_rw_semaphore_t *s) { LASSERT(s != NULL); (void)s; } -#endif #ifdef HAVE_LIBPTHREAD /* - * Completion + * Multi-threaded user space completion */ -void cfs_init_completion(struct cfs_completion *c) +void cfs_mt_init_completion(cfs_mt_completion_t *c) { LASSERT(c != NULL); c->c_done = 0; @@ -238,14 +269,14 @@ void cfs_init_completion(struct cfs_completion *c) pthread_cond_init(&c->c_cond, NULL); } -void cfs_fini_completion(struct cfs_completion *c) +void cfs_mt_fini_completion(cfs_mt_completion_t *c) { LASSERT(c != NULL); pthread_mutex_destroy(&c->c_mut); pthread_cond_destroy(&c->c_cond); } -void cfs_complete(struct cfs_completion *c) +void cfs_mt_complete(cfs_mt_completion_t *c) { LASSERT(c != NULL); pthread_mutex_lock(&c->c_mut); @@ -254,7 +285,7 @@ void cfs_complete(struct cfs_completion *c) pthread_mutex_unlock(&c->c_mut); } -void cfs_wait_for_completion(struct cfs_completion *c) +void cfs_mt_wait_for_completion(cfs_mt_completion_t *c) { LASSERT(c != NULL); pthread_mutex_lock(&c->c_mut); @@ -265,12 +296,12 @@ void cfs_wait_for_completion(struct cfs_completion *c) } /* - * atomic primitives + * Multi-threaded user space atomic primitives */ static pthread_mutex_t atomic_guard_lock = PTHREAD_MUTEX_INITIALIZER; -int cfs_atomic_read(cfs_atomic_t *a) +int cfs_mt_atomic_read(cfs_mt_atomic_t *a) { int r; @@ -280,14 +311,14 @@ int cfs_atomic_read(cfs_atomic_t *a) return r; } -void cfs_atomic_set(cfs_atomic_t *a, int b) +void cfs_mt_atomic_set(cfs_mt_atomic_t *a, int b) { pthread_mutex_lock(&atomic_guard_lock); a->counter = b; pthread_mutex_unlock(&atomic_guard_lock); } -int cfs_atomic_dec_and_test(cfs_atomic_t *a) +int cfs_mt_atomic_dec_and_test(cfs_mt_atomic_t *a) { int r; @@ -297,20 +328,20 @@ int cfs_atomic_dec_and_test(cfs_atomic_t *a) return (r == 0); } -void cfs_atomic_inc(cfs_atomic_t *a) +void cfs_mt_atomic_inc(cfs_mt_atomic_t *a) { pthread_mutex_lock(&atomic_guard_lock); ++a->counter; pthread_mutex_unlock(&atomic_guard_lock); } -void cfs_atomic_dec(cfs_atomic_t *a) +void cfs_mt_atomic_dec(cfs_mt_atomic_t *a) { pthread_mutex_lock(&atomic_guard_lock); --a->counter; pthread_mutex_unlock(&atomic_guard_lock); } -void cfs_atomic_add(int b, cfs_atomic_t *a) +void cfs_mt_atomic_add(int b, cfs_mt_atomic_t *a) { pthread_mutex_lock(&atomic_guard_lock); @@ -318,7 +349,7 @@ void cfs_atomic_add(int b, cfs_atomic_t *a) pthread_mutex_unlock(&atomic_guard_lock); } -void cfs_atomic_sub(int b, cfs_atomic_t *a) +void cfs_mt_atomic_sub(int b, cfs_mt_atomic_t *a) { pthread_mutex_lock(&atomic_guard_lock); a->counter -= b;