Răsfoiți Sursa

- atomic_add & atomic_cmpxchg added to ppc
- atomic_unkown (used when the procesor does not suport atomic ops or
is not among the supported ones), tries now to use a "hash" of locks
if GEN_LOCK_SET_T_UNLIMITED is defined => less contention on multi-cpus
- atomic_ops.h defines *_UNLIMITED macros when the number of locks or set
size are limited only by the available memory (everything exept SYSV sems)
- license changes: all the atomic* stuff and the locks are now under a
BSD (OpenBSD) style license

Andrei Pelinescu-Onciul 18 ani în urmă
părinte
comite
ccab6f017b
13 a modificat fișierele cu 437 adăugiri și 263 ștergeri
  1. 38 19
      atomic/atomic_alpha.h
  2. 36 19
      atomic/atomic_arm.h
  3. 35 19
      atomic/atomic_mips2.h
  4. 69 27
      atomic/atomic_ppc.h
  5. 10 19
      atomic/atomic_sparc.h
  6. 37 19
      atomic/atomic_sparc64.h
  7. 70 29
      atomic/atomic_unknown.h
  8. 16 19
      atomic/atomic_x86.h
  9. 37 26
      atomic_ops.c
  10. 10 19
      atomic_ops.h
  11. 10 19
      fastlock.h
  12. 36 21
      lock_ops.h
  13. 33 8
      test/atomic_test2.c

+ 38 - 19
atomic/atomic_alpha.h

@@ -3,26 +3,17 @@
  * 
  * Copyright (C) 2006 iptelorg GmbH
  *
- * This file is part of ser, a free SIP server.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 /*
  *  atomic operations and memory barriers (alpha specific)
@@ -285,6 +276,18 @@ inline static int mb_atomic_get_and_set_int(volatile int* v, int i)
 	return atomic_get_and_set_int(v, i);
 }
 
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
+{
+	membar();
+	return atomic_cmpxchg_int(v, o, n);
+}
+
+inline static int mb_atomic_add_int(volatile int* v, int i)
+{
+	membar();
+	return atomic_add_int(v, i);
+}
+
 
 
 #define mb_atomic_set_long(v, i) \
@@ -347,6 +350,19 @@ inline static long mb_atomic_get_and_set_long(volatile long* v, long l)
 }
 
 
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
+{
+	membar();
+	return atomic_cmpxchg_long(v, o, n);
+}
+
+inline static long mb_atomic_add_long(volatile long* v, long i)
+{
+	membar();
+	return atomic_add_long(v, i);
+}
+
+
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
 #define mb_atomic_dec(var) mb_atomic_dec_int(&(var)->val)
 #define mb_atomic_and(var, mask) mb_atomic_and_int(&(var)->val, (mask))
@@ -356,5 +372,8 @@ inline static long mb_atomic_get_and_set_long(volatile long* v, long l)
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
+#define mb_atomic_cmpxchg(var, o, n) mb_atomic_cmpxchg_int(&(var)->val, o, n)
+#define mb_atomic_add(var, i) mb_atomic_add_int(&(var)->val, i)
+
 
 #endif

+ 36 - 19
atomic/atomic_arm.h

@@ -3,26 +3,17 @@
  * 
  * Copyright (C) 2006 iptelorg GmbH
  *
- * This file is part of ser, a free SIP server.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 /*
  *  atomic ops and memory barriers for arm (>= v3)
@@ -260,6 +251,18 @@ inline static int mb_atomic_get_and_set_int(volatile int* v, int i)
 	return atomic_get_and_set_int(v, i);
 }
 
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
+{
+	membar();
+	return atomic_cmpxchg_int(v, o, n);
+}
+
+inline static int mb_atomic_add_int(volatile int* v, int i)
+{
+	membar();
+	return atomic_add_int(v, i);
+}
+
 
 
 #define mb_atomic_set_long(v, i) \
@@ -321,6 +324,17 @@ inline static long mb_atomic_get_and_set_long(volatile long* v, long l)
 	return atomic_get_and_set_long(v, l);
 }
 
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
+{
+	membar();
+	return atomic_cmpxchg_long(v, o, n);
+}
+
+inline static long mb_atomic_add_long(volatile long* v, long i)
+{
+	membar();
+	return atomic_add_long(v, i);
+}
 
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
 #define mb_atomic_dec(var) mb_atomic_dec_int(&(var)->val)
@@ -331,6 +345,9 @@ inline static long mb_atomic_get_and_set_long(volatile long* v, long l)
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
+#define mb_atomic_cmpxchg(var, o, n) mb_atomic_cmpxchg_int(&(var)->val, o, n)
+#define mb_atomic_add(var, i) mb_atomic_add_int(&(var)->val, i)
+
 
 
 #else /* ! __CPU_arm6 => __CPU_arm */

+ 35 - 19
atomic/atomic_mips2.h

@@ -3,26 +3,17 @@
  * 
  * Copyright (C) 2006 iptelorg GmbH
  *
- * This file is part of ser, a free SIP server.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 /*
  *  atomic operations and memory barriers (mips isa 2 and mips64 specific)
@@ -294,6 +285,17 @@ inline static int mb_atomic_get_and_set_int(volatile int* v, int i)
 	return atomic_get_and_set_int(v, i);
 }
 
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
+{
+	membar();
+	return atomic_cmpxchg_int(v, o, n);
+}
+
+inline static int mb_atomic_add_int(volatile int* v, int i)
+{
+	membar();
+	return atomic_add_int(v, i);
+}
 
 
 #define mb_atomic_set_long(v, i) \
@@ -355,6 +357,18 @@ inline static long mb_atomic_get_and_set_long(volatile long* v, long l)
 	return atomic_get_and_set_long(v, l);
 }
 
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
+{
+	membar();
+	return atomic_cmpxchg_long(v, o, n);
+}
+
+inline static long mb_atomic_add_long(volatile long* v, long i)
+{
+	membar();
+	return atomic_add_long(v, i);
+}
+
 
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
 #define mb_atomic_dec(var) mb_atomic_dec_int(&(var)->val)
@@ -365,5 +379,7 @@ inline static long mb_atomic_get_and_set_long(volatile long* v, long l)
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
+#define mb_atomic_cmpxchg(var, o, n) mb_atomic_cmpxchg_int(&(var)->val, o, n)
+#define mb_atomic_add(var, i) mb_atomic_add_int(&(var)->val, i)
 
 #endif

+ 69 - 27
atomic/atomic_ppc.h

@@ -3,26 +3,17 @@
  * 
  * Copyright (C) 2006 iptelorg GmbH
  *
- * This file is part of ser, a free SIP server.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 /*
  *  atomic operations and memory barriers (powerpc and powerpc64 versions)
@@ -38,6 +29,7 @@
  * History:
  * --------
  *  2006-03-24  created by andrei
+ *  2007-05-11  added atomic_add and atomic_cmpxchg (andrei)
  */
 
 #ifndef _atomic_ppc_h
@@ -133,23 +125,44 @@
 		return RET_EXPR; \
 	}
 
+/* cmpxchg, %3=var, %0=*var, %4=old, %3=new  */
+#define ATOMIC_CMPXCHG_DECL(NAME, P_TYPE) \
+	inline static P_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var, \
+															P_TYPE old \
+															P_TYPE new_v) \
+	{ \
+		P_TYPE ret; \
+		asm volatile( \
+			ATOMIC_ASM_OP3_##P_TYPE("cmpw %0, %4 \n\t bne- 2f") \
+			: "=&r"(ret), "=m"(*var) : "r"(var), "r"(new_v), "r"(old) \
+				: "cc" \
+			); \
+		return ret; \
+	}
+
+
+
 
 
-ATOMIC_FUNC_DECL(inc,      "addic  %0, %0,  1", int, void, /* no return */ )
-ATOMIC_FUNC_DECL(dec,      "addic %0, %0,  -1", int, void, /* no return */ )
+ATOMIC_FUNC_DECL(inc,      "addi  %0, %0,  1", int, void, /* no return */ )
+ATOMIC_FUNC_DECL(dec,      "addi %0, %0,  -1", int, void, /* no return */ )
 ATOMIC_FUNC_DECL1(and,     "and     %0, %0, %3", int, void, /* no return */ )
 ATOMIC_FUNC_DECL1(or,      "or     %0, %0, %3", int, void, /* no return */ )
-ATOMIC_FUNC_DECL(inc_and_test, "addic   %0, %0, 1", int, int, (ret==0) )
-ATOMIC_FUNC_DECL(dec_and_test, "addic  %0, %0, -1", int, int, (ret==0) )
+ATOMIC_FUNC_DECL(inc_and_test, "addi   %0, %0, 1", int, int, (ret==0) )
+ATOMIC_FUNC_DECL(dec_and_test, "addi  %0, %0, -1", int, int, (ret==0) )
 ATOMIC_FUNC_DECL3(get_and_set, /* no extra op needed */ , int, int,  ret)
+ATOMIC_CMPXCHG_DECL(cmpxchg, int)
+ATOMIC_FUNC_DECL1(add, "add %0, %0, %3" , int, int,  ret)
 
-ATOMIC_FUNC_DECL(inc,      "addic  %0, %0,  1", long, void, /* no return */ )
-ATOMIC_FUNC_DECL(dec,      "addic %0, %0,  -1", long, void, /* no return */ )
+ATOMIC_FUNC_DECL(inc,      "addi  %0, %0,  1", long, void, /* no return */ )
+ATOMIC_FUNC_DECL(dec,      "addi %0, %0,  -1", long, void, /* no return */ )
 ATOMIC_FUNC_DECL1(and,     "and     %0, %0, %3",long, void, /* no return */ )
 ATOMIC_FUNC_DECL1(or,      "or     %0, %0, %3", long, void, /* no return */ )
-ATOMIC_FUNC_DECL(inc_and_test, "addic   %0, %0, 1", long, long, (ret==0) )
-ATOMIC_FUNC_DECL(dec_and_test, "addic  %0, %0, -1", long, long, (ret==0) )
+ATOMIC_FUNC_DECL(inc_and_test, "addi   %0, %0, 1", long, long, (ret==0) )
+ATOMIC_FUNC_DECL(dec_and_test, "addi  %0, %0, -1", long, long, (ret==0) )
 ATOMIC_FUNC_DECL3(get_and_set, /* no extra op needed */ , long, long,  ret)
+ATOMIC_CMPXCHG_DECL(cmpxchg, long)
+ATOMIC_FUNC_DECL1(add, "add %0, %0, %3" , long, long,  ret)
 
 
 #define atomic_inc(var) atomic_inc_int(&(var)->val)
@@ -159,6 +172,8 @@ ATOMIC_FUNC_DECL3(get_and_set, /* no extra op needed */ , long, long,  ret)
 #define atomic_dec_and_test(var) atomic_dec_and_test_int(&(var)->val)
 #define atomic_inc_and_test(var) atomic_inc_and_test_int(&(var)->val)
 #define atomic_get_and_set(var, i) atomic_get_and_set_int(&(var)->val, i)
+#define atomic_cmpxchg(var, o, n) atomic_cmpxchg_int(&(var)->val, (o), (n))
+#define atomic_add(var, i) atomic_add_int(&(var)->val, (i))
 
 
 /* with integrated membar */
@@ -221,6 +236,17 @@ inline static int mb_atomic_get_and_set_int(volatile int* v, int i)
 	return atomic_get_and_set_int(v, i);
 }
 
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
+{
+	membar();
+	return atomic_cmpxchg_int(v, o, n);
+}
+
+inline static int mb_atomic_add_int(volatile int* v, int i)
+{
+	membar();
+	return atomic_add_int(v, i);
+}
 
 
 #define mb_atomic_set_long(v, i) \
@@ -282,6 +308,19 @@ inline static long mb_atomic_get_and_set_long(volatile long* v, long l)
 	return atomic_get_and_set_long(v, l);
 }
 
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
+{
+	membar();
+	return atomic_cmpxchg_long(v, o, n);
+}
+
+inline static long mb_atomic_add_long(volatile long* v, long i)
+{
+	membar();
+	return atomic_add_long(v, i);
+}
+
+
 
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
 #define mb_atomic_dec(var) mb_atomic_dec_int(&(var)->val)
@@ -292,5 +331,8 @@ inline static long mb_atomic_get_and_set_long(volatile long* v, long l)
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
+#define mb_atomic_cmpxchg(v, o, n)	atomic_cmpxchg_int(&(v)->val, o, n)
+#define mb_atomic_add(v, a)	atomic_add_int(&(v)->val, a)
+
 
 #endif

+ 10 - 19
atomic/atomic_sparc.h

@@ -3,26 +3,17 @@
  * 
  * Copyright (C) 2006 iptelorg GmbH
  *
- * This file is part of ser, a free SIP server.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 /*
  *  memory barriers for sparc32 ( version < v 9))

+ 37 - 19
atomic/atomic_sparc64.h

@@ -3,26 +3,17 @@
  * 
  * Copyright (C) 2006 iptelorg GmbH
  *
- * This file is part of ser, a free SIP server.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 /*
  *  atomic operations and memory barriers (sparc64 version, 32 and 64 bit modes)
@@ -254,6 +245,18 @@ inline static int mb_atomic_get_and_set_int(volatile int* v, int i)
 	return atomic_get_and_set_int(v, i);
 }
 
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
+{
+	membar();
+	return atomic_cmpxchg_int(v, o, n);
+}
+
+inline static int mb_atomic_add_int(volatile int* v, int i)
+{
+	membar();
+	return atomic_add_int(v, i);
+}
+
 
 
 #define mb_atomic_set_long(v, i) \
@@ -315,6 +318,18 @@ inline static long mb_atomic_get_and_set_long(volatile long* v, long l)
 	return atomic_get_and_set_long(v, l);
 }
 
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
+{
+	membar();
+	return atomic_cmpxchg_long(v, o, n);
+}
+
+inline static long mb_atomic_add_long(volatile long* v, long i)
+{
+	membar();
+	return atomic_add_long(v, i);
+}
+
 
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
 #define mb_atomic_dec(var) mb_atomic_dec_int(&(var)->val)
@@ -325,5 +340,8 @@ inline static long mb_atomic_get_and_set_long(volatile long* v, long l)
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
+#define mb_atomic_cmpxchg(var, o, n) mb_atomic_cmpxchg_int(&(var)->val, o, n)
+#define mb_atomic_add(var, i) mb_atomic_add_int(&(var)->val, i)
+
 
 #endif

+ 70 - 29
atomic/atomic_unknown.h

@@ -3,26 +3,17 @@
  * 
  * Copyright (C) 2006 iptelorg GmbH
  *
- * This file is part of ser, a free SIP server.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 /*
  *  atomic operations and memory barriers implemented using locks
@@ -42,6 +33,8 @@
  * History:
  * --------
  *  2006-03-08  created by andrei
+ *  2007-05-11  added atomic_add and atomic_cmpxchg 
+ *              use lock_set if lock economy is not needed (andrei)
  */
 
 #ifndef _atomic_unknown_h
@@ -94,15 +87,33 @@ extern gen_lock_t* __membar_lock; /* init in atomic_ops.c */
 
 #ifndef HAVE_ASM_INLINE_ATOMIC_OPS
 
+#ifdef GEN_LOCK_SET_T_UNLIMITED
+#ifndef ATOMIC_OPS_USE_LOCK_SET
+#define ATOMIC_OPS_USE_LOCK_SET
+#endif
+#else
 #ifndef ATOMIC_OPS_USE_LOCK
 #define ATOMIC_OPS_USE_LOCK
 #endif
+#endif /* GEN_LOCK_SET_T_UNLIMITED */
 
+#ifdef ATOMIC_OPS_USE_LOCK_SET 
+#define _ATOMIC_LS_SIZE	256
+/* hash after the variable address: ignore first 4 bits since
+ * vars are generally alloc'ed at at least 16 bytes multiples */
+#define _atomic_ls_hash(v)  ((((unsigned long)(v))>>4)&(_ATOMIC_LS_SIZE-1))
+extern gen_lock_set_t* _atomic_lock_set;
+
+#define atomic_lock(v)   lock_set_get(_atomic_lock_set, _atomic_ls_hash(v))
+#define atomic_unlock(v) lock_set_release(_atomic_lock_set, _atomic_ls_hash(v))
+
+#else
 extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
 
-#define atomic_lock    lock_get(_atomic_lock)
-#define atomic_unlock  lock_release(_atomic_lock)
+#define atomic_lock(v)    lock_get(_atomic_lock)
+#define atomic_unlock(v)  lock_release(_atomic_lock)
 
+#endif /* ATOMIC_OPS_USE_LOCK_SET */
 
 /* atomic ops */
 
@@ -111,9 +122,9 @@ extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
 #define ATOMIC_FUNC_DECL(NAME, OP, P_TYPE, RET_TYPE, RET_EXPR) \
 	inline static RET_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var) \
 	{ \
-		atomic_lock; \
+		atomic_lock(var); \
 		OP ; \
-		atomic_unlock; \
+		atomic_unlock(var); \
 		return RET_EXPR; \
 	}
 
@@ -124,9 +135,9 @@ extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
 	inline static RET_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var, \
 														P_TYPE v) \
 	{ \
-		atomic_lock; \
+		atomic_lock(var); \
 		OP ; \
-		atomic_unlock; \
+		atomic_unlock(var); \
 		return RET_EXPR; \
 	}
 
@@ -136,9 +147,9 @@ extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
 	inline static RET_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var) \
 	{ \
 		P_TYPE ret; \
-		atomic_lock; \
+		atomic_lock(var); \
 		OP ; \
-		atomic_unlock; \
+		atomic_unlock(var); \
 		return RET_EXPR; \
 	}
 
@@ -148,12 +159,25 @@ extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
 														P_TYPE v) \
 	{ \
 		P_TYPE ret; \
-		atomic_lock; \
+		atomic_lock(var); \
+		OP ; \
+		atomic_unlock(var); \
+		return RET_EXPR; \
+	}
+
+/* like ATOMIC_FUNC_DECL1_RET, but takes an extra param */
+#define ATOMIC_FUNC_DECL2_RET(NAME, OP, P_TYPE, RET_TYPE, RET_EXPR) \
+	inline static RET_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var, \
+														P_TYPE v1, P_TYPE v2)\
+	{ \
+		P_TYPE ret; \
+		atomic_lock(var); \
 		OP ; \
-		atomic_unlock; \
+		atomic_unlock(var); \
 		return RET_EXPR; \
 	}
 
+
 ATOMIC_FUNC_DECL(inc,      (*var)++, int, void, /* no return */ )
 ATOMIC_FUNC_DECL(dec,      (*var)--, int, void, /* no return */ )
 ATOMIC_FUNC_DECL1(and,     *var&=v, int, void, /* no return */ )
@@ -161,6 +185,10 @@ ATOMIC_FUNC_DECL1(or,      *var|=v, int, void, /* no return */ )
 ATOMIC_FUNC_DECL_RET(inc_and_test, ret=++(*var), int, int, (ret==0) )
 ATOMIC_FUNC_DECL_RET(dec_and_test, ret=--(*var), int, int, (ret==0) )
 ATOMIC_FUNC_DECL1_RET(get_and_set, ret=*var;*var=v , int, int,  ret)
+ATOMIC_FUNC_DECL2_RET(cmpxchg, ret=*var;\
+							*var=(((ret!=v1)-1)&v2)+(~((ret!=v1)-1)&ret),\
+							int, int,  ret)
+ATOMIC_FUNC_DECL1_RET(add, *var+=v;ret=*var, int, int, ret )
 
 ATOMIC_FUNC_DECL(inc,      (*var)++, long, void, /* no return */ )
 ATOMIC_FUNC_DECL(dec,      (*var)--, long, void, /* no return */ )
@@ -169,6 +197,10 @@ ATOMIC_FUNC_DECL1(or,      *var|=v, long, void, /* no return */ )
 ATOMIC_FUNC_DECL_RET(inc_and_test, ret=++(*var), long, long, (ret==0) )
 ATOMIC_FUNC_DECL_RET(dec_and_test, ret=--(*var), long, long, (ret==0) )
 ATOMIC_FUNC_DECL1_RET(get_and_set, ret=*var;*var=v , long, long,  ret)
+ATOMIC_FUNC_DECL2_RET(cmpxchg, ret=*var;\
+							*var=(((ret!=v1)-1)&v2)+(~((ret!=v1)-1)&ret),\
+							long, long,  ret)
+ATOMIC_FUNC_DECL1_RET(add, *var+=v;ret=*var, long, long, ret )
 
 
 #define atomic_inc(var) atomic_inc_int(&(var)->val)
@@ -178,6 +210,9 @@ ATOMIC_FUNC_DECL1_RET(get_and_set, ret=*var;*var=v , long, long,  ret)
 #define atomic_dec_and_test(var) atomic_dec_and_test_int(&(var)->val)
 #define atomic_inc_and_test(var) atomic_inc_and_test_int(&(var)->val)
 #define atomic_get_and_set(var, i) atomic_get_and_set_int(&(var)->val, i)
+#define atomic_cmpxchg(var, old, new_v) \
+	atomic_cmpxchg_int(&(var)->val, old, new_v)
+#define atomic_add(var, v) atomic_add_int(&(var)->val, v)
 
 
 /* memory barrier versions, the same as "normal" versions (since the
@@ -221,6 +256,8 @@ inline static long mb_atomic_get_long(volatile long* v)
 #define mb_atomic_inc_and_test_int(v)	atomic_inc_and_test_int(v)
 #define mb_atomic_dec_and_test_int(v)	atomic_dec_and_test_int(v)
 #define mb_atomic_get_and_set_int(v, i)	atomic_get_and_set_int(v, i)
+#define mb_atomic_cmpxchg_int(v, o, n)	atomic_cmpxchg_int(v, o, n)
+#define mb_atomic_add_int(v, i)	atomic_add_int(v, i)
 
 #define mb_atomic_inc_long(v)	atomic_inc_long(v)
 #define mb_atomic_dec_long(v)	atomic_dec_long(v)
@@ -229,6 +266,8 @@ inline static long mb_atomic_get_long(volatile long* v)
 #define mb_atomic_inc_and_test_long(v)	atomic_inc_and_test_long(v)
 #define mb_atomic_dec_and_test_long(v)	atomic_dec_and_test_long(v)
 #define mb_atomic_get_and_set_long(v, i)	atomic_get_and_set_long(v, i)
+#define mb_atomic_cmpxchg_long(v, o, n)	atomic_cmpxchg_long(v, o, n)
+#define mb_atomic_add_long(v, i)	atomic_add_long(v, i)
 
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
 #define mb_atomic_dec(var) mb_atomic_dec_int(&(var)->val)
@@ -237,6 +276,8 @@ inline static long mb_atomic_get_long(volatile long* v)
 #define mb_atomic_dec_and_test(var) mb_atomic_dec_and_test_int(&(var)->val)
 #define mb_atomic_inc_and_test(var) mb_atomic_inc_and_test_int(&(var)->val)
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
+#define mb_atomic_cmpxchg(v, o, n)	atomic_cmpxchg_int(&(v)->val, o, n)
+#define mb_atomic_add(v, i)	atomic_add_int(&(v)->val, i)
 
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)

+ 16 - 19
atomic/atomic_x86.h

@@ -3,26 +3,17 @@
  * 
  * Copyright (C) 2006 iptelorg GmbH
  *
- * This file is part of ser, a free SIP server.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 /*
  *  atomic operations and memory barriers (x86 and x86_64/amd64 specific)
@@ -335,6 +326,8 @@ inline static long mb_atomic_get_long(volatile long* var)
 #define mb_atomic_inc_and_test_int(v)	atomic_inc_and_test_int(v)
 #define mb_atomic_dec_and_test_int(v)	atomic_dec_and_test_int(v)
 #define mb_atomic_get_and_set_int(v, i)	atomic_get_and_set_int(v, i)
+#define mb_atomic_cmpxchg_int(v, o, n)	atomic_cmpxchg_int(v, o, n)
+#define mb_atomic_add_int(v, a)	atomic_add_int(v, a)
 
 #define mb_atomic_inc_long(v)	atomic_inc_long(v)
 #define mb_atomic_dec_long(v)	atomic_dec_long(v)
@@ -343,6 +336,8 @@ inline static long mb_atomic_get_long(volatile long* var)
 #define mb_atomic_inc_and_test_long(v)	atomic_inc_and_test_long(v)
 #define mb_atomic_dec_and_test_long(v)	atomic_dec_and_test_long(v)
 #define mb_atomic_get_and_set_long(v, i)	atomic_get_and_set_long(v, i)
+#define mb_atomic_cmpxchg_long(v, o, n)	atomic_cmpxchg_long(v, o, n)
+#define mb_atomic_add_long(v, a)	atomic_add_long(v, a)
 
 #define mb_atomic_inc(v)	atomic_inc(v)
 #define mb_atomic_dec(v)	atomic_dec(v)
@@ -353,6 +348,8 @@ inline static long mb_atomic_get_long(volatile long* var)
 #define mb_atomic_get(v)	mb_atomic_get_int( &(v)->val)
 #define mb_atomic_set(v, i)	mb_atomic_set_int(&(v)->val, i)
 #define mb_atomic_get_and_set(v, i)	atomic_get_and_set_int(&(v)->val, i)
+#define mb_atomic_cmpxchg(v, o, n)	atomic_cmpxchg_int(&(v)->val, o, n)
+#define mb_atomic_add(v, a)	atomic_add_int(&(v)->val, a)
 
 
 #endif

+ 37 - 26
atomic_ops.c

@@ -3,26 +3,17 @@
  * 
  * Copyright (C) 2006 iptelorg GmbH
  *
- * This file is part of ser, a free SIP server.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 /*
  *  atomic operations init
@@ -31,12 +22,14 @@
  * History:
  * --------
  *  2006-03-08  created by andrei
+ *  2007-05-11  added lock_set support (andrei)
  */
 
 #include "atomic_ops_init.h"
 #include "atomic_ops.h"
 
-#if defined ATOMIC_OPS_USE_LOCK  || defined MEMBAR_USES_LOCK
+#if defined ATOMIC_OPS_USE_LOCK  || defined ATOMIC_OPS_USE_LOCK_SET || \
+	defined MEMBAR_USES_LOCK
 #include "locking.h"
 #endif
 
@@ -44,7 +37,9 @@
 gen_lock_t* __membar_lock=0; /* init in atomic_ops.c */
 #endif
 
-#ifdef ATOMIC_OPS_USE_LOCK
+#ifdef ATOMIC_OPS_USE_LOCK_SET
+gen_lock_set_t* _atomic_lock_set=0;
+#elif defined ATOMIC_OPS_USE_LOCK
 gen_lock_t* _atomic_lock=0;
 #endif
 
@@ -65,7 +60,16 @@ int init_atomic_ops()
 	_membar_lock; /* start with the lock "taken" so that we can safely use
 					 unlock/lock sequences on it later */
 #endif
-#ifdef ATOMIC_OPS_USE_LOCK
+#ifdef ATOMIC_OPS_USE_LOCK_SET
+	if ((_atomic_lock_set=lock_set_alloc(_ATOMIC_LS_SIZE))==0){
+		goto error;
+	}
+	if (lock_set_init(_atomic_lock_set)==0){
+		lock_set_dealloc(_atomic_lock_set);
+		_atomic_lock_set=0;
+		goto error;
+	}
+#elif defined ATOMIC_OPS_USE_LOCK
 	if ((_atomic_lock=lock_alloc())==0){
 		goto error;
 	}
@@ -74,9 +78,10 @@ int init_atomic_ops()
 		_atomic_lock=0;
 		goto error;
 	}
-#endif
+#endif /* ATOMIC_OPS_USE_LOCK_SET/ATOMIC_OPS_USE_LOCK */
 	return 0;
-#if defined MEMBAR_USES_LOCK || defined ATOMIC_OPS_USE_LOCK
+#if defined MEMBAR_USES_LOCK || defined ATOMIC_OPS_USE_LOCK || \
+	defined ATOMIC_OPS_USE_LOCK_SET
 error:
 	destroy_atomic_ops();
 	return -1;
@@ -94,11 +99,17 @@ void destroy_atomic_ops()
 		__membar_lock=0;
 	}
 #endif
-#ifdef ATOMIC_OPS_USE_LOCK
+#ifdef ATOMIC_OPS_USE_LOCK_SET
+	if (_atomic_lock_set!=0){
+		lock_set_destroy(_atomic_lock_set);
+		lock_set_dealloc(_atomic_lock_set);
+		_atomic_lock_set=0;
+	}
+#elif defined ATOMIC_OPS_USE_LOCK
 	if (_atomic_lock!=0){
 		lock_destroy(_atomic_lock);
 		lock_dealloc(_atomic_lock);
 		_atomic_lock=0;
 	}
-#endif
+#endif /* ATOMIC_OPS_USE_LOCK_SET / ATOMIC_OPS_USE_LOCK*/
 }

+ 10 - 19
atomic_ops.h

@@ -3,26 +3,17 @@
  * 
  * Copyright (C) 2006 iptelorg GmbH
  *
- * This file is part of ser, a free SIP server.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 /*
  *  atomic operations and memory barriers

+ 10 - 19
fastlock.h

@@ -7,26 +7,17 @@
  *
  * Copyright (C) 2001-2003 FhG Fokus
  *
- * This file is part of ser, a free SIP server.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License 
- * along with this program; if not, write to the Free Software 
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 /*
  *

+ 36 - 21
lock_ops.h

@@ -3,26 +3,17 @@
  *
  * Copyright (C) 2001-2003 FhG Fokus
  *
- * This file is part of ser, a free SIP server.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License 
- * along with this program; if not, write to the Free Software 
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
 /*
@@ -65,6 +56,22 @@ Implements:
 	int  lock_set_try(gen_lock_set_t* s, int i);    - tries to lock the sem i,
 	                                                  returns 0 on success and
 	                                                  -1 on failure
+	
+	defines:
+	--------
+	GEN_LOCK_T_PREFERRED - defined if using  arrays of gen_lock_t is as good as
+	                      using a lock set (gen_lock_set_t). 
+						  In general is better to have the locks "close" or 
+						  inside the protected data structure rather then 
+						  having a separate array or lock set. However in some
+						  case (e.g. SYSV_LOCKS) is better to use lock sets,
+						  either due to lock number limitations, excesive 
+						  performance or memory overhead. In this cases
+						  GEN_LOCK_T_PREFERRED will not be defined.
+	GEN_LOCK_T_UNLIMITED - defined if there is no system imposed limit on
+	                       the number of locks (other then the memory).
+	GEN_LOCK_SET_T_UNLIMITED
+	                      - like above but for the size of a lock set.
 
 WARNING: - lock_set_init may fail for large number of sems (e.g. sysv). 
          - signals are not treated! (some locks are "awakened" by the signals)
@@ -257,7 +264,10 @@ tryagain:
 /* lock sets */
 
 #if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || defined(USE_POSIX_SEM)
-#define GEN_LOCK_T_PREFERED
+#define GEN_LOCK_T_PREFERRED
+#define GEN_LOCK_T_PREFERED  /* backwards compat. */
+#define GEN_LOCK_T_UNLIMITED
+#define GEN_LOCK_SET_T_UNLIMITED
 
 struct gen_lock_set_t_ {
 	long size;
@@ -281,7 +291,12 @@ inline static gen_lock_set_t* lock_set_init(gen_lock_set_t* s)
 #define lock_set_release(set, i) lock_release(&set->locks[i])
 
 #elif defined(USE_SYSV_SEM)
-#undef GEN_LOCK_T_PREFERED
+#undef GEN_LOCK_T_PREFERRED
+#undef GEN_LOCK_T_PREFERED  /* backwards compat. */
+#undef GEN_LOCK_T_UNLIMITED
+#undef GEN_LOCK_SET_T_UNLIMITED
+#define GEN_LOCK_T_LIMITED
+#define GEN_LOCK_SET_T_LIMITED
 
 struct gen_lock_set_t_ {
 	int size;

+ 33 - 8
test/atomic_test2.c

@@ -41,7 +41,8 @@
 
 #include "../atomic_ops.h"
 
-#if defined ATOMIC_OPS_USE_LOCK  || defined MEMBAR_USES_LOCK
+#if defined ATOMIC_OPS_USE_LOCK  || defined MEMBAR_USES_LOCK || \
+	defined ATOMIC_OPS_USE_LOCK_SET
 /* hack to make lock work */
 #include "../lock_ops.h"
 #endif
@@ -51,11 +52,15 @@ gen_lock_t* __membar_lock=0; /* init in atomic_ops.c */
 gen_lock_t dummy_membar_lock;
 #endif
 
-#ifdef ATOMIC_OPS_USE_LOCK
+#ifdef ATOMIC_OPS_USE_LOCK_SET
+gen_lock_set_t* _atomic_lock_set=0;
+gen_lock_set_t dummy_atomic_lock_set;
+gen_lock_t locks_array[_ATOMIC_LS_SIZE];
+#elif defined ATOMIC_OPS_USE_LOCK
 gen_lock_t* _atomic_lock=0;
 gen_lock_t dummy_atomic_lock;
+#endif /* ATOMIC_OPS_USE_LOCK / _SET */
 
-#endif
 
 
 
@@ -100,7 +105,13 @@ static char* flags=
 	"no_asm_membar(slow) "
 #endif
 #ifndef HAVE_ASM_INLINE_ATOMIC_OPS
-	"no_asm_atomic_ops "
+	"no_asm_atomic_ops"
+#ifdef ATOMIC_OPS_USE_LOCK_SET
+	":use_lock_set"
+#elif defined ATOMIC_OPS_USE_LOCK
+	":use_lock"
+#endif
+	" "
 #endif
 #ifdef TYPE
 	STR(TYPE) " "
@@ -147,7 +158,7 @@ static char* flags=
 
 #define VERIFY(ops, y) \
 	ops ; \
-	CHECK_ERR( ops, get_val(v), y)
+	CHECK_ERR( ops, y, get_val(v))
 
 
 int main(int argc, char** argv)
@@ -170,7 +181,16 @@ int main(int argc, char** argv)
 	_membar_lock; /* start with the lock "taken" so that we can safely use
 					 unlock/lock sequences on it later */
 #endif
-#ifdef ATOMIC_OPS_USE_LOCK
+#ifdef ATOMIC_OPS_USE_LOCK_SET
+	/* init the lock (emulate atomic_ops.c) */
+	dummy_atomic_lock_set.locks=&locks_array[0];
+	_atomic_lock_set=&dummy_atomic_lock_set;
+	if (lock_set_init(_atomic_lock_set)==0){
+		fprintf(stderr, "ERROR: failed to initialize atomic_lock\n");
+		_atomic_lock_set=0;
+		goto error;
+	}
+#elif defined ATOMIC_OPS_USE_LOCK
 	/* init the lock (emulate atomic_ops.c) */
 	_atomic_lock=&dummy_atomic_lock;
 	if (lock_init(_atomic_lock)==0){
@@ -241,7 +261,9 @@ int main(int argc, char** argv)
 #ifdef MEMBAR_USES_LOCK
 	lock_destroy(__membar_lock);
 #endif
-#ifdef ATOMIC_OPS_USE_LOCK
+#ifdef ATOMIC_OPS_USE_LOCK_SET
+	lock_set_destroy(_atomic_lock_set);
+#elif defined ATOMIC_OPS_USE_LOCK
 	lock_destroy(_atomic_lock);
 #endif
 	return 0;
@@ -250,7 +272,10 @@ error:
 	if (__membar_lock)
 		lock_destroy(__membar_lock);
 #endif
-#ifdef ATOMIC_OPS_USE_LOCK
+#ifdef ATOMIC_OPS_USE_LOCK_SET
+	if (_atomic_lock_set)
+		lock_set_destroy(_atomic_lock_set);
+#elif defined ATOMIC_OPS_USE_LOCK
 	if (_atomic_lock)
 		lock_destroy(_atomic_lock);
 #endif