1103840Sjulian/*-
2103840Sjulian * Copyright (c) 2002 David Xu (davidxu@freebsd.org).
3103840Sjulian * All rights reserved.
4103840Sjulian *
5103840Sjulian * Redistribution and use in source and binary forms, with or without
6103840Sjulian * modification, are permitted provided that the following conditions
7103840Sjulian * are met:
8103840Sjulian * 1. Redistributions of source code must retain the above copyright
9103840Sjulian *    notice, this list of conditions and the following disclaimer.
10103840Sjulian * 2. Redistributions in binary form must reproduce the above copyright
11103840Sjulian *    notice, this list of conditions and the following disclaimer in the
12103840Sjulian *    documentation and/or other materials provided with the distribution.
13103840Sjulian *
14103840Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15103840Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16103840Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17103840Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18103840Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19103840Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20103840Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21103840Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22103840Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23103840Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24103840Sjulian * SUCH DAMAGE.
25103840Sjulian *
26103840Sjulian * $FreeBSD$
27103840Sjulian */
28103840Sjulian
29103840Sjulian#ifndef _SIMPLELOCK_H
30103840Sjulian#define _SIMPLELOCK_H
31103840Sjulian
32103840Sjulian#include <machine/asmacros.h>
33103840Sjulian#include <machine/atomic.h>
34103840Sjulian
35103840Sjulianstruct simplelock {
36103840Sjulian	int s_lock;
37103840Sjulian};
38103840Sjulian
39103840Sjulianstatic inline void
40103840Sjuliansimplelock_init(struct simplelock *lock)
41103840Sjulian{
42103840Sjulian	lock->s_lock = 0;
43103840Sjulian}
44103840Sjulian
45103840Sjulianstatic inline void
46103840Sjuliansimplelock_lock(struct simplelock *lock)
47103840Sjulian{
48106336Sdavidxu	while (!atomic_cmpset_int(&lock->s_lock, 0, 1))
49103840Sjulian		;
50103840Sjulian}
51103840Sjulian
52110535Sdavidxustatic inline void
53110535Sdavidxusimplelock_unlock(struct simplelock *lock)
54103840Sjulian{
55103840Sjulian	atomic_store_rel_int(&lock->s_lock, 0);
56103840Sjulian}
57103840Sjulian
58103840Sjulian#endif
59103840Sjulian
60