Home | History | Annotate | Line # | Download | only in libpthread
      1 /*	$NetBSD: pthread_spin.c,v 1.11 2023/05/25 14:30:03 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nathan J. Williams and Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Public (POSIX-specified) spinlocks.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __RCSID("$NetBSD: pthread_spin.c,v 1.11 2023/05/25 14:30:03 riastradh Exp $");
     38 
     39 /* Need to use libc-private names for atomic operations. */
     40 #include "../../common/lib/libc/atomic/atomic_op_namespace.h"
     41 
     42 #include <sys/types.h>
     43 #include <sys/ras.h>
     44 
     45 #include <machine/lock.h>
     46 
     47 #include <errno.h>
     48 #include <unistd.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 
     52 #include "pthread.h"
     53 #include "pthread_int.h"
     54 
     55 int
     56 pthread_spin_init(pthread_spinlock_t *lock, int pshared)
     57 {
     58 
     59 	pthread__error(EINVAL, "Invalid pshared",
     60 	    pshared == PTHREAD_PROCESS_PRIVATE ||
     61 	    pshared == PTHREAD_PROCESS_SHARED);
     62 
     63 	lock->pts_magic = _PT_SPINLOCK_MAGIC;
     64 
     65 	/*
     66 	 * We don't actually use the pshared flag for anything;
     67 	 * CPU simple locks have all the process-shared properties
     68 	 * that we want anyway.
     69 	 */
     70 	lock->pts_flags = pshared;
     71 	pthread_lockinit(&lock->pts_spin);
     72 
     73 	return 0;
     74 }
     75 
     76 int
     77 pthread_spin_destroy(pthread_spinlock_t *lock)
     78 {
     79 
     80 	pthread__error(EINVAL, "Invalid spinlock",
     81 	    lock->pts_magic == _PT_SPINLOCK_MAGIC);
     82 
     83 	if (!__SIMPLELOCK_UNLOCKED_P(&lock->pts_spin))
     84 		return EBUSY;
     85 
     86 	lock->pts_magic = _PT_SPINLOCK_DEAD;
     87 
     88 	return 0;
     89 }
     90 
     91 int
     92 pthread_spin_lock(pthread_spinlock_t *lock)
     93 {
     94 	pthread_t self;
     95 
     96 	pthread__error(EINVAL, "Invalid spinlock",
     97 	    lock->pts_magic == _PT_SPINLOCK_MAGIC);
     98 
     99 	self = pthread__self();
    100 	while (pthread__spintrylock(self, &lock->pts_spin) == 0) {
    101 		pthread__smt_wait();
    102 	}
    103 
    104 	return 0;
    105 }
    106 
    107 int
    108 pthread_spin_trylock(pthread_spinlock_t *lock)
    109 {
    110 	pthread_t self;
    111 
    112 	pthread__error(EINVAL, "Invalid spinlock",
    113 	    lock->pts_magic == _PT_SPINLOCK_MAGIC);
    114 
    115 	self = pthread__self();
    116 	if (pthread__spintrylock(self, &lock->pts_spin) == 0)
    117 		return EBUSY;
    118 	return 0;
    119 }
    120 
    121 int
    122 pthread_spin_unlock(pthread_spinlock_t *lock)
    123 {
    124 	pthread_t self;
    125 
    126 	pthread__error(EINVAL, "Invalid spinlock",
    127 	    lock->pts_magic == _PT_SPINLOCK_MAGIC);
    128 
    129 	self = pthread__self();
    130 	pthread__spinunlock(self, &lock->pts_spin);
    131 	pthread__smt_wake();
    132 
    133 	return 0;
    134 }
    135