1111519Sdavidxu/*-
2111519Sdavidxu * Copyright (c) 2002 David Xu (davidxu@freebsd.org).
3111519Sdavidxu * All rights reserved.
4111519Sdavidxu *
5111519Sdavidxu * Redistribution and use in source and binary forms, with or without
6111519Sdavidxu * modification, are permitted provided that the following conditions
7111519Sdavidxu * are met:
8111519Sdavidxu * 1. Redistributions of source code must retain the above copyright
9111519Sdavidxu *    notice, this list of conditions and the following disclaimer.
10111519Sdavidxu * 2. Redistributions in binary form must reproduce the above copyright
11111519Sdavidxu *    notice, this list of conditions and the following disclaimer in the
12111519Sdavidxu *    documentation and/or other materials provided with the distribution.
13111519Sdavidxu *
14111519Sdavidxu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15111519Sdavidxu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16111519Sdavidxu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17111519Sdavidxu * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18111519Sdavidxu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19111519Sdavidxu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20111519Sdavidxu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21111519Sdavidxu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22111519Sdavidxu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23111519Sdavidxu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24111519Sdavidxu * SUCH DAMAGE.
25111519Sdavidxu *
26111519Sdavidxu * $FreeBSD$
27111519Sdavidxu */
28111519Sdavidxu
29111519Sdavidxu#ifndef _SIMPLELOCK_H
30111519Sdavidxu#define _SIMPLELOCK_H
31111519Sdavidxu
32111519Sdavidxu#include <machine/asmacros.h>
33111519Sdavidxu#include <machine/atomic.h>
34111519Sdavidxu
35111519Sdavidxustruct simplelock {
36111519Sdavidxu	int s_lock;
37111519Sdavidxu};
38111519Sdavidxu
39111519Sdavidxustatic inline void
40111519Sdavidxusimplelock_init(struct simplelock *lock)
41111519Sdavidxu{
42111519Sdavidxu	lock->s_lock = 0;
43111519Sdavidxu}
44111519Sdavidxu
45111519Sdavidxustatic inline void
46111519Sdavidxusimplelock_lock(struct simplelock *lock)
47111519Sdavidxu{
48111519Sdavidxu	while (!atomic_cmpset_int(&lock->s_lock, 0, 1))
49111519Sdavidxu		;
50111519Sdavidxu}
51111519Sdavidxu
52111519Sdavidxustatic inline void
53111519Sdavidxusimplelock_unlock(struct simplelock *lock)
54111519Sdavidxu{
55111519Sdavidxu	atomic_store_rel_int(&lock->s_lock, 0);
56111519Sdavidxu}
57111519Sdavidxu
58111519Sdavidxu#endif
59111519Sdavidxu
60