1 /*
2  * Copyright (C) 2016-2017 Andes Technology, Inc.
3  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
4  */
5 
6 /* Machine-dependent pthreads configuration and inline functions.
7    Copyright (C) 1997, 1998, 2000, 2002, 2003 Free Software Foundation, Inc.
8    Contributed by Philip Blundell <philb@gnu.org>.
9 
10    The GNU C Library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public License as
12    published by the Free Software Foundation; either version 2.1 of the
13    License, or (at your option) any later version.
14 
15    The GNU C Library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19 
20    You should have received a copy of the GNU Lesser General Public
21    License along with the GNU C Library; see the file COPYING.LIB.  If not,
22    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, USA.  */
24 
25 #ifndef _PT_MACHINE_H
26 #define _PT_MACHINE_H   1
27 
28 #include <features.h>
29 
30 #ifndef PT_EI
31 # define PT_EI __extern_always_inline
32 #endif
33 
34 extern long int testandset (int *spinlock);
35 extern int __compare_and_swap (long int *p, long int oldval, long int newval);
36 
37 /* Spinlock implementation; required.  */
38 PT_EI long int
testandset(int * spinlock)39 testandset (int *spinlock)
40 {
41 	unsigned int		val;
42 	unsigned int		temp;
43 	unsigned int		offset = 0;
44 
45 	__asm__ __volatile__ (
46 		"1:\n\t"
47 		"llw	%[val],	[%[spinlock] + %[offset] << 0]\n\t"
48 		"move	%[temp], #0x1\n\t"
49 		"beq	%[val], %[temp], 2f\n\t"
50 		"scw	%[temp], [%[spinlock] + %[offset] << 0]\n\t"
51 		"beqz   %[temp], 1b\n\t"
52 		"2:\n\t"
53 		: [val] "=&r" (val), [temp] "=&r" (temp)
54 		: [spinlock] "r" (spinlock), [offset] "r" (offset)
55 		: "memory" ) ;
56 
57 	return val ;
58 }
59 
60 
61 /* Get some notion of the current stack.  Need not be exactly the top
62    of the stack, just something somewhere in the current frame.  */
63 #define CURRENT_STACK_FRAME  stack_pointer
64 register char * stack_pointer __asm__ ("$sp");
65 
66 #endif /* pt-machine.h */
67