11cb0ef41Sopenharmony_ci/* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
21cb0ef41Sopenharmony_ci *
31cb0ef41Sopenharmony_ci * Permission to use, copy, modify, and/or distribute this software for any
41cb0ef41Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above
51cb0ef41Sopenharmony_ci * copyright notice and this permission notice appear in all copies.
61cb0ef41Sopenharmony_ci *
71cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
81cb0ef41Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
91cb0ef41Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
101cb0ef41Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
111cb0ef41Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
121cb0ef41Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
131cb0ef41Sopenharmony_ci * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
141cb0ef41Sopenharmony_ci */
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci#ifndef UV_SPINLOCK_H_
171cb0ef41Sopenharmony_ci#define UV_SPINLOCK_H_
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci#include "internal.h"  /* ACCESS_ONCE, UV_UNUSED */
201cb0ef41Sopenharmony_ci#include "atomic-ops.h"
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#define UV_SPINLOCK_INITIALIZER { 0 }
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_citypedef struct {
251cb0ef41Sopenharmony_ci  int lock;
261cb0ef41Sopenharmony_ci} uv_spinlock_t;
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciUV_UNUSED(static void uv_spinlock_init(uv_spinlock_t* spinlock));
291cb0ef41Sopenharmony_ciUV_UNUSED(static void uv_spinlock_lock(uv_spinlock_t* spinlock));
301cb0ef41Sopenharmony_ciUV_UNUSED(static void uv_spinlock_unlock(uv_spinlock_t* spinlock));
311cb0ef41Sopenharmony_ciUV_UNUSED(static int uv_spinlock_trylock(uv_spinlock_t* spinlock));
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciUV_UNUSED(static void uv_spinlock_init(uv_spinlock_t* spinlock)) {
341cb0ef41Sopenharmony_ci  ACCESS_ONCE(int, spinlock->lock) = 0;
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciUV_UNUSED(static void uv_spinlock_lock(uv_spinlock_t* spinlock)) {
381cb0ef41Sopenharmony_ci  while (!uv_spinlock_trylock(spinlock)) cpu_relax();
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciUV_UNUSED(static void uv_spinlock_unlock(uv_spinlock_t* spinlock)) {
421cb0ef41Sopenharmony_ci  ACCESS_ONCE(int, spinlock->lock) = 0;
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciUV_UNUSED(static int uv_spinlock_trylock(uv_spinlock_t* spinlock)) {
461cb0ef41Sopenharmony_ci  /* TODO(bnoordhuis) Maybe change to a ticket lock to guarantee fair queueing.
471cb0ef41Sopenharmony_ci   * Not really critical until we have locks that are (frequently) contended
481cb0ef41Sopenharmony_ci   * for by several threads.
491cb0ef41Sopenharmony_ci   */
501cb0ef41Sopenharmony_ci  return 0 == cmpxchgi(&spinlock->lock, 0, 1);
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci#endif  /* UV_SPINLOCK_H_ */
54