4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
36 # define DEBUG_SUBSYSTEM S_LNET
38 #include <libcfs/libcfs.h>
43 void __declspec (naked) FASTCALL
50 // EDX = v ; [EDX][0] = v->counter
53 lock add dword ptr [edx][0], ecx
58 void __declspec (naked) FASTCALL
65 // EDX = v ; [EDX][0] = v->counter
68 lock sub dword ptr [edx][0], ecx
73 void __declspec (naked) FASTCALL
78 //InterlockedIncrement((PULONG)(&((v)->counter)));
80 //` ECX = v ; [ECX][0] = v->counter
83 lock inc dword ptr [ecx][0]
88 void __declspec (naked) FASTCALL
93 // ECX = v ; [ECX][0] = v->counter
96 lock dec dword ptr [ecx][0]
101 int __declspec (naked) FASTCALL
102 cfs_atomic_sub_and_test(
109 // EDX = v ; [EDX][0] = v->counter
113 lock sub dword ptr [edx][0], ecx
119 int __declspec (naked) FASTCALL
120 cfs_atomic_inc_and_test(
124 // ECX = v ; [ECX][0] = v->counter
128 lock inc dword ptr [ecx][0]
134 int __declspec (naked) FASTCALL
135 cfs_atomic_dec_and_test(
139 // ECX = v ; [ECX][0] = v->counter
143 lock dec dword ptr [ecx][0]
149 #elif defined(_AMD64_)
157 InterlockedExchangeAdd( (PULONG)(&((v)->counter)) , (LONG) (i));
166 InterlockedExchangeAdd( (PULONG)(&((v)->counter)) , (LONG) (-1*i));
174 InterlockedIncrement((PULONG)(&((v)->counter)));
182 InterlockedDecrement((PULONG)(&((v)->counter)));
186 cfs_atomic_sub_and_test(
195 counter = v->counter;
196 result = counter - i;
198 } while ( InterlockedCompareExchange(
201 counter) != counter);
203 return (result == 0);
207 cfs_atomic_inc_and_test(
215 counter = v->counter;
216 result = counter + 1;
218 } while ( InterlockedCompareExchange(
221 counter) != counter);
223 return (result == 0);
227 cfs_atomic_dec_and_test(
235 counter = v->counter;
236 result = counter - 1;
238 } while ( InterlockedCompareExchange(
241 counter) != counter);
243 return (result == 0);
248 #error CPU arch type isn't specified.
253 * atomic_add_return - add integer and return
254 * \param v pointer of type atomic_t
255 * \param i integer value to add
257 * Atomically adds \a i to \a v and returns \a i + \a v
259 int FASTCALL cfs_atomic_add_return(int i, cfs_atomic_t *v)
265 counter = v->counter;
266 result = counter + i;
268 } while ( InterlockedCompareExchange(
271 counter) != counter);
278 * atomic_sub_return - subtract integer and return
279 * \param v pointer of type atomic_t
280 * \param i integer value to subtract
282 * Atomically subtracts \a i from \a v and returns \a v - \a i
284 int FASTCALL cfs_atomic_sub_return(int i, cfs_atomic_t *v)
286 return cfs_atomic_add_return(-i, v);
289 int FASTCALL cfs_atomic_dec_and_lock(cfs_atomic_t *v, spinlock_t *lock)
291 if (cfs_atomic_read(v) != 1)
295 if (cfs_atomic_dec_and_test(v))
308 rwlock_init(rwlock_t *rwlock)
310 spin_lock_init(&rwlock->guard);
315 cfs_rwlock_fini(rwlock_t *rwlock)
320 read_lock(rwlock_t *rwlock)
322 cfs_task_t * task = cfs_current();
323 PTASK_SLOT slot = NULL;
326 /* should bugchk here */
327 cfs_enter_debugger();
331 slot = CONTAINING_RECORD(task, TASK_SLOT, task);
332 ASSERT(slot->Magic == TASKSLT_MAGIC);
334 slot->irql = KeRaiseIrqlToDpcLevel();
337 spin_lock(&rwlock->guard);
338 if (rwlock->count >= 0)
340 spin_unlock(&rwlock->guard);
344 spin_unlock(&rwlock->guard);
348 read_unlock(rwlock_t *rwlock)
350 cfs_task_t * task = cfs_current();
351 PTASK_SLOT slot = NULL;
354 /* should bugchk here */
355 cfs_enter_debugger();
359 slot = CONTAINING_RECORD(task, TASK_SLOT, task);
360 ASSERT(slot->Magic == TASKSLT_MAGIC);
362 spin_lock(&rwlock->guard);
363 ASSERT(rwlock->count > 0);
366 cfs_enter_debugger();
367 spin_unlock(&rwlock->guard);
369 KeLowerIrql(slot->irql);
373 write_lock(rwlock_t *rwlock)
375 cfs_task_t * task = cfs_current();
376 PTASK_SLOT slot = NULL;
379 /* should bugchk here */
380 cfs_enter_debugger();
384 slot = CONTAINING_RECORD(task, TASK_SLOT, task);
385 ASSERT(slot->Magic == TASKSLT_MAGIC);
387 slot->irql = KeRaiseIrqlToDpcLevel();
390 spin_lock(&rwlock->guard);
391 if (rwlock->count == 0)
393 spin_unlock(&rwlock->guard);
397 spin_unlock(&rwlock->guard);
401 write_unlock(rwlock_t *rwlock)
403 cfs_task_t * task = cfs_current();
404 PTASK_SLOT slot = NULL;
407 /* should bugchk here */
408 cfs_enter_debugger();
412 slot = CONTAINING_RECORD(task, TASK_SLOT, task);
413 ASSERT(slot->Magic == TASKSLT_MAGIC);
415 spin_lock(&rwlock->guard);
416 ASSERT(rwlock->count == -1);
418 spin_unlock(&rwlock->guard);
420 KeLowerIrql(slot->irql);