1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/include/libcfs/winnt/winnt-lock.h
38 * Basic library routines.
41 #ifndef __LIBCFS_WINNT_CFS_LOCK_H__
42 #define __LIBCFS_WINNT_CFS_LOCK_H__
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
54 * All locks' declaration are not guaranteed to be initialized,
55 * Althought some of they are initialized in Linux. All locks
56 * declared by CFS_DECL_* should be initialized explicitly.
60 * spinlock & event definitions
63 typedef struct cfs_spin_lock cfs_spinlock_t;
67 typedef struct { volatile int counter; } cfs_atomic_t;
69 #define CFS_ATOMIC_INIT(i) { i }
71 #define cfs_atomic_read(v) ((v)->counter)
72 #define cfs_atomic_set(v,i) (((v)->counter) = (i))
74 void FASTCALL cfs_atomic_add(int i, cfs_atomic_t *v);
75 void FASTCALL cfs_atomic_sub(int i, cfs_atomic_t *v);
77 int FASTCALL cfs_atomic_sub_and_test(int i, cfs_atomic_t *v);
79 void FASTCALL cfs_atomic_inc(cfs_atomic_t *v);
80 void FASTCALL cfs_atomic_dec(cfs_atomic_t *v);
82 int FASTCALL cfs_atomic_dec_and_test(cfs_atomic_t *v);
83 int FASTCALL cfs_atomic_inc_and_test(cfs_atomic_t *v);
85 int FASTCALL cfs_atomic_add_return(int i, cfs_atomic_t *v);
86 int FASTCALL cfs_atomic_sub_return(int i, cfs_atomic_t *v);
88 #define cfs_atomic_inc_return(v) cfs_atomic_add_return(1, v)
89 #define cfs_atomic_dec_return(v) cfs_atomic_sub_return(1, v)
91 int FASTCALL cfs_atomic_dec_and_lock(cfs_atomic_t *v, cfs_spinlock_t *lock);
95 typedef KEVENT event_t;
99 * To initialize the event object
102 * event: pointer to the event object
103 * type: Non Zero: SynchronizationEvent
104 * Zero: NotificationEvent
105 * status: the initial stats of the event
116 cfs_init_event(event_t *event, int type, int status)
120 (type) ? SynchronizationEvent: NotificationEvent,
121 (status) ? TRUE : FALSE
126 * cfs_wait_event_internal
127 * To wait on an event to syncrhonize the process
130 * event: pointer to the event object
131 * timeout: the timeout for waitting or 0 means infinite time.
134 * Zero: waiting timeouts
135 * Non Zero: event signaled ...
141 static inline int64_t
142 cfs_wait_event_internal(event_t * event, int64_t timeout)
145 LARGE_INTEGER TimeOut;
147 TimeOut.QuadPart = -1 * (10000000/CFS_HZ) * timeout;
149 Status = KeWaitForSingleObject(
154 (timeout != 0) ? (&TimeOut) : (NULL)
157 if (Status == STATUS_TIMEOUT) {
161 return TRUE; // signaled case
166 * To signal the event object
169 * event: pointer to the event object
179 cfs_wake_event(event_t * event)
181 return (KeSetEvent(event, 0, FALSE) != 0);
186 * To clear/reset the status of the event object
189 * event: pointer to the event object
199 cfs_clear_event(event_t * event)
205 * spin lock defintions / routines
211 * for spinlock operations, try to grab nesting acquisition of
212 * spinlock will cause dead-lock in MP system and current irql
213 * overwritten for UP system. (UP system could allow nesting spin
214 * acqisition, because it's not spin at all just raising the irql.)
218 struct cfs_spin_lock {
223 #define CFS_DECL_SPIN(name) cfs_spinlock_t name;
224 #define CFS_DECL_SPIN_EXTERN(name) extern cfs_spinlock_t name;
226 #define CFS_SPIN_LOCK_UNLOCKED {0}
228 static inline void cfs_spin_lock_init(cfs_spinlock_t *lock)
230 KeInitializeSpinLock(&(lock->lock));
233 static inline void cfs_spin_lock(cfs_spinlock_t *lock)
235 KeAcquireSpinLock(&(lock->lock), &(lock->irql));
238 static inline void cfs_spin_lock_nested(cfs_spinlock_t *lock, unsigned subclass)
240 KeAcquireSpinLock(&(lock->lock), &(lock->irql));
243 static inline void cfs_spin_unlock(cfs_spinlock_t *lock)
245 KIRQL irql = lock->irql;
246 KeReleaseSpinLock(&(lock->lock), irql);
250 #define cfs_spin_lock_irqsave(lock, flags) \
251 do {(flags) = 0; cfs_spin_lock(lock);} while(0)
253 #define cfs_spin_unlock_irqrestore(lock, flags) \
254 do {cfs_spin_unlock(lock);} while(0)
257 /* There's no corresponding routine in windows kernel.
258 We must realize a light one of our own. But there's
259 no way to identify the system is MP build or UP build
260 on the runtime. We just uses a workaround for it. */
262 extern int libcfs_mp_system;
264 static int cfs_spin_trylock(cfs_spinlock_t *lock)
269 ASSERT(lock != NULL);
271 KeRaiseIrql(DISPATCH_LEVEL, &Irql);
273 if (libcfs_mp_system) {
274 if (0 == (ulong_ptr_t)lock->lock) {
277 mov edx, dword ptr [ebp + 8]
278 lock bts dword ptr[edx], 0
301 static int cfs_spin_is_locked(cfs_spinlock_t *lock)
303 #if _WIN32_WINNT >= 0x502
304 /* KeTestSpinLock only avalilable on 2k3 server or later */
305 return (!KeTestSpinLock(&lock->lock));
307 return (int) (lock->lock);
311 /* synchronization between cpus: it will disable all DPCs
312 kernel task scheduler on the CPU */
313 #define cfs_spin_lock_bh(x) cfs_spin_lock(x)
314 #define cfs_spin_unlock_bh(x) cfs_spin_unlock(x)
315 #define cfs_spin_lock_bh_init(x) cfs_spin_lock_init(x)
318 * cfs_rw_semaphore (using ERESOURCE)
322 typedef struct cfs_rw_semaphore {
324 } cfs_rw_semaphore_t;
327 #define CFS_DECLARE_RWSEM(name) cfs_rw_semaphore_t name
328 #define CFS_DECLARE_RWSEM_EXTERN(name) extern cfs_rw_semaphore_t name
332 * To initialize the the cfs_rw_semaphore_t structure
335 * rwsem: pointer to the cfs_rw_semaphore_t structure
344 static inline void cfs_init_rwsem(cfs_rw_semaphore_t *s)
346 ExInitializeResourceLite(&s->rwsem);
348 #define rwsem_init cfs_init_rwsem
352 * To finilize/destroy the the cfs_rw_semaphore_t structure
355 * rwsem: pointer to the cfs_rw_semaphore_t structure
361 * For winnt system, we need this routine to delete the ERESOURCE.
362 * Just define it NULL for other systems.
365 static inline void cfs_fini_rwsem(cfs_rw_semaphore_t *s)
367 ExDeleteResourceLite(&s->rwsem);
372 * To acquire read-lock of the cfs_rw_semaphore
375 * rwsem: pointer to the cfs_rw_semaphore_t structure
384 static inline void cfs_down_read(cfs_rw_semaphore_t *s)
386 ExAcquireResourceSharedLite(&s->rwsem, TRUE);
388 #define cfs_down_read_nested cfs_down_read
392 * cfs_down_read_trylock
393 * To acquire read-lock of the cfs_rw_semaphore without blocking
396 * rwsem: pointer to the cfs_rw_semaphore_t structure
399 * Zero: failed to acquire the read lock
400 * Non-Zero: succeeded to acquire the read lock
403 * This routine will return immediately without waiting.
406 static inline int cfs_down_read_trylock(cfs_rw_semaphore_t *s)
408 return ExAcquireResourceSharedLite(&s->rwsem, FALSE);
414 * To acquire write-lock of the cfs_rw_semaphore
417 * rwsem: pointer to the cfs_rw_semaphore_t structure
426 static inline void cfs_down_write(cfs_rw_semaphore_t *s)
428 ExAcquireResourceExclusiveLite(&(s->rwsem), TRUE);
430 #define cfs_down_write_nested cfs_down_write
434 * To acquire write-lock of the cfs_rw_semaphore without blocking
437 * rwsem: pointer to the cfs_rw_semaphore_t structure
440 * Zero: failed to acquire the write lock
441 * Non-Zero: succeeded to acquire the read lock
444 * This routine will return immediately without waiting.
447 static inline int cfs_down_write_trylock(cfs_rw_semaphore_t *s)
449 return ExAcquireResourceExclusiveLite(&(s->rwsem), FALSE);
455 * To release read-lock of the cfs_rw_semaphore
458 * rwsem: pointer to the cfs_rw_semaphore_t structure
467 static inline void cfs_up_read(cfs_rw_semaphore_t *s)
469 ExReleaseResourceForThreadLite(
471 ExGetCurrentResourceThread());
477 * To release write-lock of the cfs_rw_semaphore
480 * rwsem: pointer to the cfs_rw_semaphore_t structure
489 static inline void cfs_up_write(cfs_rw_semaphore_t *s)
491 ExReleaseResourceForThreadLite(
493 ExGetCurrentResourceThread());
497 * rwlock_t (using sempahore)
507 cfs_spinlock_t guard;
511 void cfs_rwlock_init(cfs_rwlock_t * rwlock);
512 void cfs_rwlock_fini(cfs_rwlock_t * rwlock);
514 void cfs_read_lock(cfs_rwlock_t * rwlock);
515 void cfs_read_unlock(cfs_rwlock_t * rwlock);
516 void cfs_write_lock(cfs_rwlock_t * rwlock);
517 void cfs_write_unlock(cfs_rwlock_t * rwlock);
519 #define cfs_write_lock_irqsave(l, f) do {f = 0; cfs_write_lock(l);} while(0)
520 #define cfs_write_unlock_irqrestore(l, f) do {cfs_write_unlock(l);} while(0)
521 #define cfs_read_lock_irqsave(l, f do {f=0; cfs_read_lock(l);} while(0)
522 #define cfs_read_unlock_irqrestore(l, f) do {cfs_read_unlock(l);} while(0)
524 #define cfs_write_lock_bh cfs_write_lock
525 #define cfs_write_unlock_bh cfs_write_unlock
527 typedef struct cfs_lock_class_key {
529 } cfs_lock_class_key_t;
531 #define cfs_lockdep_set_class(lock, class) do {} while(0)
533 static inline void cfs_lockdep_off(void)
537 static inline void cfs_lockdep_on(void)
549 typedef struct cfs_semaphore {
553 static inline void cfs_sema_init(cfs_semaphore_t *s, int val)
555 KeInitializeSemaphore(&s->sem, val, val);
558 static inline void __down(cfs_semaphore_t *s)
560 KeWaitForSingleObject( &(s->sem), Executive,
561 KernelMode, FALSE, NULL );
564 static inline void __up(cfs_semaphore_t *s)
566 KeReleaseSemaphore(&s->sem, 0, 1, FALSE);
569 static inline int down_trylock(cfs_semaphore_t *s)
571 LARGE_INTEGER timeout = {0};
573 KeWaitForSingleObject( &(s->sem), Executive,
574 KernelMode, FALSE, &timeout);
576 if (status == STATUS_SUCCESS) {
587 * - init_mutex_locked(x)
592 typedef struct cfs_semaphore cfs_mutex_t;
594 #define CFS_DECLARE_MUTEX(x) cfs_mutex_t x
598 * To initialize a mutex_t structure
601 * mutex: pointer to the mutex_t structure
609 #define cfs_mutex_init cfs_init_mutex
610 static inline void cfs_init_mutex(cfs_mutex_t *mutex)
612 cfs_sema_init(mutex, 1);
617 * To acquire the mutex lock
620 * mutex: pointer to the mutex_t structure
629 static inline void cfs_mutex_down(cfs_mutex_t *mutex)
634 #define cfs_mutex_lock(m) cfs_mutex_down(m)
635 #define cfs_mutex_trylock(s) down_trylock(s)
636 #define cfs_mutex_lock_nested(m) cfs_mutex_down(m)
637 #define cfs_down(m) cfs_mutex_down(m)
641 * To release the mutex lock (acquired already)
644 * mutex: pointer to the mutex_t structure
653 static inline void cfs_mutex_up(cfs_mutex_t *mutex)
658 #define cfs_mutex_unlock(m) cfs_mutex_up(m)
659 #define cfs_up(m) cfs_mutex_up(m)
663 * To initialize the mutex as acquired state
666 * mutex: pointer to the mutex_t structure
675 static inline void cfs_init_mutex_locked(cfs_mutex_t *mutex)
677 cfs_init_mutex(mutex);
678 cfs_mutex_down(mutex);
681 static inline void cfs_mutex_destroy(cfs_mutex_t *mutex)
688 * - init_complition(c)
690 * - wait_for_completion(c)
700 * To initialize the completion object
703 * c: pointer to the completion structure
712 static inline void cfs_init_completion(cfs_completion_t *c)
714 cfs_init_event(&(c->event), 1, FALSE);
720 * To complete/signal the completion object
723 * c: pointer to the completion structure
732 static inline void cfs_complete(cfs_completion_t *c)
734 cfs_wake_event(&(c->event));
738 * wait_for_completion
739 * To wait on the completion object. If the event is signaled,
740 * this function will return to the call with the event un-singled.
743 * c: pointer to the completion structure
752 static inline void cfs_wait_for_completion(cfs_completion_t *c)
754 cfs_wait_event_internal(&(c->event), 0);
757 static inline int cfs_wait_for_completion_interruptible(cfs_completion_t *c)
759 cfs_wait_event_internal(&(c->event), 0);
763 #else /* !__KERNEL__ */
764 #endif /* !__KERNEL__ */