Browse code

- 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 authored on 11/05/2007 20:44:15
Showing 13 changed files
... ...
@@ -3,26 +3,17 @@
3 3
  * 
4 4
  * Copyright (C) 2006 iptelorg GmbH
5 5
  *
6
- * This file is part of ser, a free SIP server.
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
7 9
  *
8
- * ser is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * For a license to use the ser software under conditions
14
- * other than those described here, or to purchase support for this
15
- * software, please contact iptel.org by e-mail at the following addresses:
16
- *    info@iptel.org
17
- *
18
- * ser is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License
24
- * along with this program; if not, write to the Free Software
25
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 17
  */
27 18
 /*
28 19
  *  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)
285 285
 	return atomic_get_and_set_int(v, i);
286 286
 }
287 287
 
288
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
289
+{
290
+	membar();
291
+	return atomic_cmpxchg_int(v, o, n);
292
+}
293
+
294
+inline static int mb_atomic_add_int(volatile int* v, int i)
295
+{
296
+	membar();
297
+	return atomic_add_int(v, i);
298
+}
299
+
288 300
 
289 301
 
290 302
 #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)
347 347
 }
348 348
 
349 349
 
350
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
351
+{
352
+	membar();
353
+	return atomic_cmpxchg_long(v, o, n);
354
+}
355
+
356
+inline static long mb_atomic_add_long(volatile long* v, long i)
357
+{
358
+	membar();
359
+	return atomic_add_long(v, i);
360
+}
361
+
362
+
350 363
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
351 364
 #define mb_atomic_dec(var) mb_atomic_dec_int(&(var)->val)
352 365
 #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)
356 356
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
357 357
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
358 358
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
359
+#define mb_atomic_cmpxchg(var, o, n) mb_atomic_cmpxchg_int(&(var)->val, o, n)
360
+#define mb_atomic_add(var, i) mb_atomic_add_int(&(var)->val, i)
361
+
359 362
 
360 363
 #endif
... ...
@@ -3,26 +3,17 @@
3 3
  * 
4 4
  * Copyright (C) 2006 iptelorg GmbH
5 5
  *
6
- * This file is part of ser, a free SIP server.
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
7 9
  *
8
- * ser is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * For a license to use the ser software under conditions
14
- * other than those described here, or to purchase support for this
15
- * software, please contact iptel.org by e-mail at the following addresses:
16
- *    info@iptel.org
17
- *
18
- * ser is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License
24
- * along with this program; if not, write to the Free Software
25
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 17
  */
27 18
 /*
28 19
  *  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)
260 260
 	return atomic_get_and_set_int(v, i);
261 261
 }
262 262
 
263
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
264
+{
265
+	membar();
266
+	return atomic_cmpxchg_int(v, o, n);
267
+}
268
+
269
+inline static int mb_atomic_add_int(volatile int* v, int i)
270
+{
271
+	membar();
272
+	return atomic_add_int(v, i);
273
+}
274
+
263 275
 
264 276
 
265 277
 #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)
321 321
 	return atomic_get_and_set_long(v, l);
322 322
 }
323 323
 
324
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
325
+{
326
+	membar();
327
+	return atomic_cmpxchg_long(v, o, n);
328
+}
329
+
330
+inline static long mb_atomic_add_long(volatile long* v, long i)
331
+{
332
+	membar();
333
+	return atomic_add_long(v, i);
334
+}
324 335
 
325 336
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
326 337
 #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)
331 331
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
332 332
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
333 333
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
334
+#define mb_atomic_cmpxchg(var, o, n) mb_atomic_cmpxchg_int(&(var)->val, o, n)
335
+#define mb_atomic_add(var, i) mb_atomic_add_int(&(var)->val, i)
336
+
334 337
 
335 338
 
336 339
 #else /* ! __CPU_arm6 => __CPU_arm */
... ...
@@ -3,26 +3,17 @@
3 3
  * 
4 4
  * Copyright (C) 2006 iptelorg GmbH
5 5
  *
6
- * This file is part of ser, a free SIP server.
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
7 9
  *
8
- * ser is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * For a license to use the ser software under conditions
14
- * other than those described here, or to purchase support for this
15
- * software, please contact iptel.org by e-mail at the following addresses:
16
- *    info@iptel.org
17
- *
18
- * ser is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License
24
- * along with this program; if not, write to the Free Software
25
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 17
  */
27 18
 /*
28 19
  *  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)
294 294
 	return atomic_get_and_set_int(v, i);
295 295
 }
296 296
 
297
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
298
+{
299
+	membar();
300
+	return atomic_cmpxchg_int(v, o, n);
301
+}
302
+
303
+inline static int mb_atomic_add_int(volatile int* v, int i)
304
+{
305
+	membar();
306
+	return atomic_add_int(v, i);
307
+}
297 308
 
298 309
 
299 310
 #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)
355 355
 	return atomic_get_and_set_long(v, l);
356 356
 }
357 357
 
358
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
359
+{
360
+	membar();
361
+	return atomic_cmpxchg_long(v, o, n);
362
+}
363
+
364
+inline static long mb_atomic_add_long(volatile long* v, long i)
365
+{
366
+	membar();
367
+	return atomic_add_long(v, i);
368
+}
369
+
358 370
 
359 371
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
360 372
 #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)
365 365
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
366 366
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
367 367
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
368
+#define mb_atomic_cmpxchg(var, o, n) mb_atomic_cmpxchg_int(&(var)->val, o, n)
369
+#define mb_atomic_add(var, i) mb_atomic_add_int(&(var)->val, i)
368 370
 
369 371
 #endif
... ...
@@ -3,26 +3,17 @@
3 3
  * 
4 4
  * Copyright (C) 2006 iptelorg GmbH
5 5
  *
6
- * This file is part of ser, a free SIP server.
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
7 9
  *
8
- * ser is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * For a license to use the ser software under conditions
14
- * other than those described here, or to purchase support for this
15
- * software, please contact iptel.org by e-mail at the following addresses:
16
- *    info@iptel.org
17
- *
18
- * ser is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License
24
- * along with this program; if not, write to the Free Software
25
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 17
  */
27 18
 /*
28 19
  *  atomic operations and memory barriers (powerpc and powerpc64 versions)
... ...
@@ -38,6 +29,7 @@
38 38
  * History:
39 39
  * --------
40 40
  *  2006-03-24  created by andrei
41
+ *  2007-05-11  added atomic_add and atomic_cmpxchg (andrei)
41 42
  */
42 43
 
43 44
 #ifndef _atomic_ppc_h
... ...
@@ -133,23 +125,44 @@
133 133
 		return RET_EXPR; \
134 134
 	}
135 135
 
136
+/* cmpxchg, %3=var, %0=*var, %4=old, %3=new  */
137
+#define ATOMIC_CMPXCHG_DECL(NAME, P_TYPE) \
138
+	inline static P_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var, \
139
+															P_TYPE old \
140
+															P_TYPE new_v) \
141
+	{ \
142
+		P_TYPE ret; \
143
+		asm volatile( \
144
+			ATOMIC_ASM_OP3_##P_TYPE("cmpw %0, %4 \n\t bne- 2f") \
145
+			: "=&r"(ret), "=m"(*var) : "r"(var), "r"(new_v), "r"(old) \
146
+				: "cc" \
147
+			); \
148
+		return ret; \
149
+	}
150
+
151
+
152
+
136 153
 
137 154
 
138
-ATOMIC_FUNC_DECL(inc,      "addic  %0, %0,  1", int, void, /* no return */ )
139
-ATOMIC_FUNC_DECL(dec,      "addic %0, %0,  -1", int, void, /* no return */ )
155
+ATOMIC_FUNC_DECL(inc,      "addi  %0, %0,  1", int, void, /* no return */ )
156
+ATOMIC_FUNC_DECL(dec,      "addi %0, %0,  -1", int, void, /* no return */ )
140 157
 ATOMIC_FUNC_DECL1(and,     "and     %0, %0, %3", int, void, /* no return */ )
141 158
 ATOMIC_FUNC_DECL1(or,      "or     %0, %0, %3", int, void, /* no return */ )
142
-ATOMIC_FUNC_DECL(inc_and_test, "addic   %0, %0, 1", int, int, (ret==0) )
143
-ATOMIC_FUNC_DECL(dec_and_test, "addic  %0, %0, -1", int, int, (ret==0) )
159
+ATOMIC_FUNC_DECL(inc_and_test, "addi   %0, %0, 1", int, int, (ret==0) )
160
+ATOMIC_FUNC_DECL(dec_and_test, "addi  %0, %0, -1", int, int, (ret==0) )
144 161
 ATOMIC_FUNC_DECL3(get_and_set, /* no extra op needed */ , int, int,  ret)
162
+ATOMIC_CMPXCHG_DECL(cmpxchg, int)
163
+ATOMIC_FUNC_DECL1(add, "add %0, %0, %3" , int, int,  ret)
145 164
 
146
-ATOMIC_FUNC_DECL(inc,      "addic  %0, %0,  1", long, void, /* no return */ )
147
-ATOMIC_FUNC_DECL(dec,      "addic %0, %0,  -1", long, void, /* no return */ )
165
+ATOMIC_FUNC_DECL(inc,      "addi  %0, %0,  1", long, void, /* no return */ )
166
+ATOMIC_FUNC_DECL(dec,      "addi %0, %0,  -1", long, void, /* no return */ )
148 167
 ATOMIC_FUNC_DECL1(and,     "and     %0, %0, %3",long, void, /* no return */ )
149 168
 ATOMIC_FUNC_DECL1(or,      "or     %0, %0, %3", long, void, /* no return */ )
150
-ATOMIC_FUNC_DECL(inc_and_test, "addic   %0, %0, 1", long, long, (ret==0) )
151
-ATOMIC_FUNC_DECL(dec_and_test, "addic  %0, %0, -1", long, long, (ret==0) )
169
+ATOMIC_FUNC_DECL(inc_and_test, "addi   %0, %0, 1", long, long, (ret==0) )
170
+ATOMIC_FUNC_DECL(dec_and_test, "addi  %0, %0, -1", long, long, (ret==0) )
152 171
 ATOMIC_FUNC_DECL3(get_and_set, /* no extra op needed */ , long, long,  ret)
172
+ATOMIC_CMPXCHG_DECL(cmpxchg, long)
173
+ATOMIC_FUNC_DECL1(add, "add %0, %0, %3" , long, long,  ret)
153 174
 
154 175
 
155 176
 #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)
159 159
 #define atomic_dec_and_test(var) atomic_dec_and_test_int(&(var)->val)
160 160
 #define atomic_inc_and_test(var) atomic_inc_and_test_int(&(var)->val)
161 161
 #define atomic_get_and_set(var, i) atomic_get_and_set_int(&(var)->val, i)
162
+#define atomic_cmpxchg(var, o, n) atomic_cmpxchg_int(&(var)->val, (o), (n))
163
+#define atomic_add(var, i) atomic_add_int(&(var)->val, (i))
162 164
 
163 165
 
164 166
 /* with integrated membar */
... ...
@@ -221,6 +236,17 @@ inline static int mb_atomic_get_and_set_int(volatile int* v, int i)
221 221
 	return atomic_get_and_set_int(v, i);
222 222
 }
223 223
 
224
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
225
+{
226
+	membar();
227
+	return atomic_cmpxchg_int(v, o, n);
228
+}
229
+
230
+inline static int mb_atomic_add_int(volatile int* v, int i)
231
+{
232
+	membar();
233
+	return atomic_add_int(v, i);
234
+}
224 235
 
225 236
 
226 237
 #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)
282 282
 	return atomic_get_and_set_long(v, l);
283 283
 }
284 284
 
285
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
286
+{
287
+	membar();
288
+	return atomic_cmpxchg_long(v, o, n);
289
+}
290
+
291
+inline static long mb_atomic_add_long(volatile long* v, long i)
292
+{
293
+	membar();
294
+	return atomic_add_long(v, i);
295
+}
296
+
297
+
285 298
 
286 299
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
287 300
 #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)
292 292
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
293 293
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
294 294
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
295
+#define mb_atomic_cmpxchg(v, o, n)	atomic_cmpxchg_int(&(v)->val, o, n)
296
+#define mb_atomic_add(v, a)	atomic_add_int(&(v)->val, a)
297
+
295 298
 
296 299
 #endif
... ...
@@ -3,26 +3,17 @@
3 3
  * 
4 4
  * Copyright (C) 2006 iptelorg GmbH
5 5
  *
6
- * This file is part of ser, a free SIP server.
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
7 9
  *
8
- * ser is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * For a license to use the ser software under conditions
14
- * other than those described here, or to purchase support for this
15
- * software, please contact iptel.org by e-mail at the following addresses:
16
- *    info@iptel.org
17
- *
18
- * ser is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License
24
- * along with this program; if not, write to the Free Software
25
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 17
  */
27 18
 /*
28 19
  *  memory barriers for sparc32 ( version < v 9))
... ...
@@ -3,26 +3,17 @@
3 3
  * 
4 4
  * Copyright (C) 2006 iptelorg GmbH
5 5
  *
6
- * This file is part of ser, a free SIP server.
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
7 9
  *
8
- * ser is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * For a license to use the ser software under conditions
14
- * other than those described here, or to purchase support for this
15
- * software, please contact iptel.org by e-mail at the following addresses:
16
- *    info@iptel.org
17
- *
18
- * ser is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License
24
- * along with this program; if not, write to the Free Software
25
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 17
  */
27 18
 /*
28 19
  *  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)
254 254
 	return atomic_get_and_set_int(v, i);
255 255
 }
256 256
 
257
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
258
+{
259
+	membar();
260
+	return atomic_cmpxchg_int(v, o, n);
261
+}
262
+
263
+inline static int mb_atomic_add_int(volatile int* v, int i)
264
+{
265
+	membar();
266
+	return atomic_add_int(v, i);
267
+}
268
+
257 269
 
258 270
 
259 271
 #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)
315 315
 	return atomic_get_and_set_long(v, l);
316 316
 }
317 317
 
318
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
319
+{
320
+	membar();
321
+	return atomic_cmpxchg_long(v, o, n);
322
+}
323
+
324
+inline static long mb_atomic_add_long(volatile long* v, long i)
325
+{
326
+	membar();
327
+	return atomic_add_long(v, i);
328
+}
329
+
318 330
 
319 331
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
320 332
 #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)
325 325
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
326 326
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
327 327
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
328
+#define mb_atomic_cmpxchg(var, o, n) mb_atomic_cmpxchg_int(&(var)->val, o, n)
329
+#define mb_atomic_add(var, i) mb_atomic_add_int(&(var)->val, i)
330
+
328 331
 
329 332
 #endif
... ...
@@ -3,26 +3,17 @@
3 3
  * 
4 4
  * Copyright (C) 2006 iptelorg GmbH
5 5
  *
6
- * This file is part of ser, a free SIP server.
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
7 9
  *
8
- * ser is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * For a license to use the ser software under conditions
14
- * other than those described here, or to purchase support for this
15
- * software, please contact iptel.org by e-mail at the following addresses:
16
- *    info@iptel.org
17
- *
18
- * ser is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License
24
- * along with this program; if not, write to the Free Software
25
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 17
  */
27 18
 /*
28 19
  *  atomic operations and memory barriers implemented using locks
... ...
@@ -42,6 +33,8 @@
42 42
  * History:
43 43
  * --------
44 44
  *  2006-03-08  created by andrei
45
+ *  2007-05-11  added atomic_add and atomic_cmpxchg 
46
+ *              use lock_set if lock economy is not needed (andrei)
45 47
  */
46 48
 
47 49
 #ifndef _atomic_unknown_h
... ...
@@ -94,15 +87,33 @@ extern gen_lock_t* __membar_lock; /* init in atomic_ops.c */
94 94
 
95 95
 #ifndef HAVE_ASM_INLINE_ATOMIC_OPS
96 96
 
97
+#ifdef GEN_LOCK_SET_T_UNLIMITED
98
+#ifndef ATOMIC_OPS_USE_LOCK_SET
99
+#define ATOMIC_OPS_USE_LOCK_SET
100
+#endif
101
+#else
97 102
 #ifndef ATOMIC_OPS_USE_LOCK
98 103
 #define ATOMIC_OPS_USE_LOCK
99 104
 #endif
105
+#endif /* GEN_LOCK_SET_T_UNLIMITED */
100 106
 
107
+#ifdef ATOMIC_OPS_USE_LOCK_SET 
108
+#define _ATOMIC_LS_SIZE	256
109
+/* hash after the variable address: ignore first 4 bits since
110
+ * vars are generally alloc'ed at at least 16 bytes multiples */
111
+#define _atomic_ls_hash(v)  ((((unsigned long)(v))>>4)&(_ATOMIC_LS_SIZE-1))
112
+extern gen_lock_set_t* _atomic_lock_set;
113
+
114
+#define atomic_lock(v)   lock_set_get(_atomic_lock_set, _atomic_ls_hash(v))
115
+#define atomic_unlock(v) lock_set_release(_atomic_lock_set, _atomic_ls_hash(v))
116
+
117
+#else
101 118
 extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
102 119
 
103
-#define atomic_lock    lock_get(_atomic_lock)
104
-#define atomic_unlock  lock_release(_atomic_lock)
120
+#define atomic_lock(v)    lock_get(_atomic_lock)
121
+#define atomic_unlock(v)  lock_release(_atomic_lock)
105 122
 
123
+#endif /* ATOMIC_OPS_USE_LOCK_SET */
106 124
 
107 125
 /* atomic ops */
108 126
 
... ...
@@ -111,9 +122,9 @@ extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
111 111
 #define ATOMIC_FUNC_DECL(NAME, OP, P_TYPE, RET_TYPE, RET_EXPR) \
112 112
 	inline static RET_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var) \
113 113
 	{ \
114
-		atomic_lock; \
114
+		atomic_lock(var); \
115 115
 		OP ; \
116
-		atomic_unlock; \
116
+		atomic_unlock(var); \
117 117
 		return RET_EXPR; \
118 118
 	}
119 119
 
... ...
@@ -124,9 +135,9 @@ extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
124 124
 	inline static RET_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var, \
125 125
 														P_TYPE v) \
126 126
 	{ \
127
-		atomic_lock; \
127
+		atomic_lock(var); \
128 128
 		OP ; \
129
-		atomic_unlock; \
129
+		atomic_unlock(var); \
130 130
 		return RET_EXPR; \
131 131
 	}
132 132
 
... ...
@@ -136,9 +147,9 @@ extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
136 136
 	inline static RET_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var) \
137 137
 	{ \
138 138
 		P_TYPE ret; \
139
-		atomic_lock; \
139
+		atomic_lock(var); \
140 140
 		OP ; \
141
-		atomic_unlock; \
141
+		atomic_unlock(var); \
142 142
 		return RET_EXPR; \
143 143
 	}
144 144
 
... ...
@@ -148,12 +159,25 @@ extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
148 148
 														P_TYPE v) \
149 149
 	{ \
150 150
 		P_TYPE ret; \
151
-		atomic_lock; \
151
+		atomic_lock(var); \
152
+		OP ; \
153
+		atomic_unlock(var); \
154
+		return RET_EXPR; \
155
+	}
156
+
157
+/* like ATOMIC_FUNC_DECL1_RET, but takes an extra param */
158
+#define ATOMIC_FUNC_DECL2_RET(NAME, OP, P_TYPE, RET_TYPE, RET_EXPR) \
159
+	inline static RET_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var, \
160
+														P_TYPE v1, P_TYPE v2)\
161
+	{ \
162
+		P_TYPE ret; \
163
+		atomic_lock(var); \
152 164
 		OP ; \
153
-		atomic_unlock; \
165
+		atomic_unlock(var); \
154 166
 		return RET_EXPR; \
155 167
 	}
156 168
 
169
+
157 170
 ATOMIC_FUNC_DECL(inc,      (*var)++, int, void, /* no return */ )
158 171
 ATOMIC_FUNC_DECL(dec,      (*var)--, int, void, /* no return */ )
159 172
 ATOMIC_FUNC_DECL1(and,     *var&=v, int, void, /* no return */ )
... ...
@@ -161,6 +185,10 @@ ATOMIC_FUNC_DECL1(or,      *var|=v, int, void, /* no return */ )
161 161
 ATOMIC_FUNC_DECL_RET(inc_and_test, ret=++(*var), int, int, (ret==0) )
162 162
 ATOMIC_FUNC_DECL_RET(dec_and_test, ret=--(*var), int, int, (ret==0) )
163 163
 ATOMIC_FUNC_DECL1_RET(get_and_set, ret=*var;*var=v , int, int,  ret)
164
+ATOMIC_FUNC_DECL2_RET(cmpxchg, ret=*var;\
165
+							*var=(((ret!=v1)-1)&v2)+(~((ret!=v1)-1)&ret),\
166
+							int, int,  ret)
167
+ATOMIC_FUNC_DECL1_RET(add, *var+=v;ret=*var, int, int, ret )
164 168
 
165 169
 ATOMIC_FUNC_DECL(inc,      (*var)++, long, void, /* no return */ )
166 170
 ATOMIC_FUNC_DECL(dec,      (*var)--, long, void, /* no return */ )
... ...
@@ -169,6 +197,10 @@ ATOMIC_FUNC_DECL1(or,      *var|=v, long, void, /* no return */ )
169 169
 ATOMIC_FUNC_DECL_RET(inc_and_test, ret=++(*var), long, long, (ret==0) )
170 170
 ATOMIC_FUNC_DECL_RET(dec_and_test, ret=--(*var), long, long, (ret==0) )
171 171
 ATOMIC_FUNC_DECL1_RET(get_and_set, ret=*var;*var=v , long, long,  ret)
172
+ATOMIC_FUNC_DECL2_RET(cmpxchg, ret=*var;\
173
+							*var=(((ret!=v1)-1)&v2)+(~((ret!=v1)-1)&ret),\
174
+							long, long,  ret)
175
+ATOMIC_FUNC_DECL1_RET(add, *var+=v;ret=*var, long, long, ret )
172 176
 
173 177
 
174 178
 #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)
178 178
 #define atomic_dec_and_test(var) atomic_dec_and_test_int(&(var)->val)
179 179
 #define atomic_inc_and_test(var) atomic_inc_and_test_int(&(var)->val)
180 180
 #define atomic_get_and_set(var, i) atomic_get_and_set_int(&(var)->val, i)
181
+#define atomic_cmpxchg(var, old, new_v) \
182
+	atomic_cmpxchg_int(&(var)->val, old, new_v)
183
+#define atomic_add(var, v) atomic_add_int(&(var)->val, v)
181 184
 
182 185
 
183 186
 /* memory barrier versions, the same as "normal" versions (since the
... ...
@@ -221,6 +256,8 @@ inline static long mb_atomic_get_long(volatile long* v)
221 221
 #define mb_atomic_inc_and_test_int(v)	atomic_inc_and_test_int(v)
222 222
 #define mb_atomic_dec_and_test_int(v)	atomic_dec_and_test_int(v)
223 223
 #define mb_atomic_get_and_set_int(v, i)	atomic_get_and_set_int(v, i)
224
+#define mb_atomic_cmpxchg_int(v, o, n)	atomic_cmpxchg_int(v, o, n)
225
+#define mb_atomic_add_int(v, i)	atomic_add_int(v, i)
224 226
 
225 227
 #define mb_atomic_inc_long(v)	atomic_inc_long(v)
226 228
 #define mb_atomic_dec_long(v)	atomic_dec_long(v)
... ...
@@ -229,6 +266,8 @@ inline static long mb_atomic_get_long(volatile long* v)
229 229
 #define mb_atomic_inc_and_test_long(v)	atomic_inc_and_test_long(v)
230 230
 #define mb_atomic_dec_and_test_long(v)	atomic_dec_and_test_long(v)
231 231
 #define mb_atomic_get_and_set_long(v, i)	atomic_get_and_set_long(v, i)
232
+#define mb_atomic_cmpxchg_long(v, o, n)	atomic_cmpxchg_long(v, o, n)
233
+#define mb_atomic_add_long(v, i)	atomic_add_long(v, i)
232 234
 
233 235
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
234 236
 #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)
237 237
 #define mb_atomic_dec_and_test(var) mb_atomic_dec_and_test_int(&(var)->val)
238 238
 #define mb_atomic_inc_and_test(var) mb_atomic_inc_and_test_int(&(var)->val)
239 239
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
240
+#define mb_atomic_cmpxchg(v, o, n)	atomic_cmpxchg_int(&(v)->val, o, n)
241
+#define mb_atomic_add(v, i)	atomic_add_int(&(v)->val, i)
240 242
 
241 243
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
242 244
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
... ...
@@ -3,26 +3,17 @@
3 3
  * 
4 4
  * Copyright (C) 2006 iptelorg GmbH
5 5
  *
6
- * This file is part of ser, a free SIP server.
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
7 9
  *
8
- * ser is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * For a license to use the ser software under conditions
14
- * other than those described here, or to purchase support for this
15
- * software, please contact iptel.org by e-mail at the following addresses:
16
- *    info@iptel.org
17
- *
18
- * ser is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License
24
- * along with this program; if not, write to the Free Software
25
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 17
  */
27 18
 /*
28 19
  *  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)
335 335
 #define mb_atomic_inc_and_test_int(v)	atomic_inc_and_test_int(v)
336 336
 #define mb_atomic_dec_and_test_int(v)	atomic_dec_and_test_int(v)
337 337
 #define mb_atomic_get_and_set_int(v, i)	atomic_get_and_set_int(v, i)
338
+#define mb_atomic_cmpxchg_int(v, o, n)	atomic_cmpxchg_int(v, o, n)
339
+#define mb_atomic_add_int(v, a)	atomic_add_int(v, a)
338 340
 
339 341
 #define mb_atomic_inc_long(v)	atomic_inc_long(v)
340 342
 #define mb_atomic_dec_long(v)	atomic_dec_long(v)
... ...
@@ -343,6 +336,8 @@ inline static long mb_atomic_get_long(volatile long* var)
343 343
 #define mb_atomic_inc_and_test_long(v)	atomic_inc_and_test_long(v)
344 344
 #define mb_atomic_dec_and_test_long(v)	atomic_dec_and_test_long(v)
345 345
 #define mb_atomic_get_and_set_long(v, i)	atomic_get_and_set_long(v, i)
346
+#define mb_atomic_cmpxchg_long(v, o, n)	atomic_cmpxchg_long(v, o, n)
347
+#define mb_atomic_add_long(v, a)	atomic_add_long(v, a)
346 348
 
347 349
 #define mb_atomic_inc(v)	atomic_inc(v)
348 350
 #define mb_atomic_dec(v)	atomic_dec(v)
... ...
@@ -353,6 +348,8 @@ inline static long mb_atomic_get_long(volatile long* var)
353 353
 #define mb_atomic_get(v)	mb_atomic_get_int( &(v)->val)
354 354
 #define mb_atomic_set(v, i)	mb_atomic_set_int(&(v)->val, i)
355 355
 #define mb_atomic_get_and_set(v, i)	atomic_get_and_set_int(&(v)->val, i)
356
+#define mb_atomic_cmpxchg(v, o, n)	atomic_cmpxchg_int(&(v)->val, o, n)
357
+#define mb_atomic_add(v, a)	atomic_add_int(&(v)->val, a)
356 358
 
357 359
 
358 360
 #endif
... ...
@@ -3,26 +3,17 @@
3 3
  * 
4 4
  * Copyright (C) 2006 iptelorg GmbH
5 5
  *
6
- * This file is part of ser, a free SIP server.
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
7 9
  *
8
- * ser is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * For a license to use the ser software under conditions
14
- * other than those described here, or to purchase support for this
15
- * software, please contact iptel.org by e-mail at the following addresses:
16
- *    info@iptel.org
17
- *
18
- * ser is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License
24
- * along with this program; if not, write to the Free Software
25
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 17
  */
27 18
 /*
28 19
  *  atomic operations init
... ...
@@ -31,12 +22,14 @@
31 31
  * History:
32 32
  * --------
33 33
  *  2006-03-08  created by andrei
34
+ *  2007-05-11  added lock_set support (andrei)
34 35
  */
35 36
 
36 37
 #include "atomic_ops_init.h"
37 38
 #include "atomic_ops.h"
38 39
 
39
-#if defined ATOMIC_OPS_USE_LOCK  || defined MEMBAR_USES_LOCK
40
+#if defined ATOMIC_OPS_USE_LOCK  || defined ATOMIC_OPS_USE_LOCK_SET || \
41
+	defined MEMBAR_USES_LOCK
40 42
 #include "locking.h"
41 43
 #endif
42 44
 
... ...
@@ -44,7 +37,9 @@
44 44
 gen_lock_t* __membar_lock=0; /* init in atomic_ops.c */
45 45
 #endif
46 46
 
47
-#ifdef ATOMIC_OPS_USE_LOCK
47
+#ifdef ATOMIC_OPS_USE_LOCK_SET
48
+gen_lock_set_t* _atomic_lock_set=0;
49
+#elif defined ATOMIC_OPS_USE_LOCK
48 50
 gen_lock_t* _atomic_lock=0;
49 51
 #endif
50 52
 
... ...
@@ -65,7 +60,16 @@ int init_atomic_ops()
65 65
 	_membar_lock; /* start with the lock "taken" so that we can safely use
66 66
 					 unlock/lock sequences on it later */
67 67
 #endif
68
-#ifdef ATOMIC_OPS_USE_LOCK
68
+#ifdef ATOMIC_OPS_USE_LOCK_SET
69
+	if ((_atomic_lock_set=lock_set_alloc(_ATOMIC_LS_SIZE))==0){
70
+		goto error;
71
+	}
72
+	if (lock_set_init(_atomic_lock_set)==0){
73
+		lock_set_dealloc(_atomic_lock_set);
74
+		_atomic_lock_set=0;
75
+		goto error;
76
+	}
77
+#elif defined ATOMIC_OPS_USE_LOCK
69 78
 	if ((_atomic_lock=lock_alloc())==0){
70 79
 		goto error;
71 80
 	}
... ...
@@ -74,9 +78,10 @@ int init_atomic_ops()
74 74
 		_atomic_lock=0;
75 75
 		goto error;
76 76
 	}
77
-#endif
77
+#endif /* ATOMIC_OPS_USE_LOCK_SET/ATOMIC_OPS_USE_LOCK */
78 78
 	return 0;
79
-#if defined MEMBAR_USES_LOCK || defined ATOMIC_OPS_USE_LOCK
79
+#if defined MEMBAR_USES_LOCK || defined ATOMIC_OPS_USE_LOCK || \
80
+	defined ATOMIC_OPS_USE_LOCK_SET
80 81
 error:
81 82
 	destroy_atomic_ops();
82 83
 	return -1;
... ...
@@ -94,11 +99,17 @@ void destroy_atomic_ops()
94 94
 		__membar_lock=0;
95 95
 	}
96 96
 #endif
97
-#ifdef ATOMIC_OPS_USE_LOCK
97
+#ifdef ATOMIC_OPS_USE_LOCK_SET
98
+	if (_atomic_lock_set!=0){
99
+		lock_set_destroy(_atomic_lock_set);
100
+		lock_set_dealloc(_atomic_lock_set);
101
+		_atomic_lock_set=0;
102
+	}
103
+#elif defined ATOMIC_OPS_USE_LOCK
98 104
 	if (_atomic_lock!=0){
99 105
 		lock_destroy(_atomic_lock);
100 106
 		lock_dealloc(_atomic_lock);
101 107
 		_atomic_lock=0;
102 108
 	}
103
-#endif
109
+#endif /* ATOMIC_OPS_USE_LOCK_SET / ATOMIC_OPS_USE_LOCK*/
104 110
 }
... ...
@@ -3,26 +3,17 @@
3 3
  * 
4 4
  * Copyright (C) 2006 iptelorg GmbH
5 5
  *
6
- * This file is part of ser, a free SIP server.
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
7 9
  *
8
- * ser is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * For a license to use the ser software under conditions
14
- * other than those described here, or to purchase support for this
15
- * software, please contact iptel.org by e-mail at the following addresses:
16
- *    info@iptel.org
17
- *
18
- * ser is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License
24
- * along with this program; if not, write to the Free Software
25
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 17
  */
27 18
 /*
28 19
  *  atomic operations and memory barriers
... ...
@@ -7,26 +7,17 @@
7 7
  *
8 8
  * Copyright (C) 2001-2003 FhG Fokus
9 9
  *
10
- * This file is part of ser, a free SIP server.
10
+ * Permission to use, copy, modify, and distribute this software for any
11
+ * purpose with or without fee is hereby granted, provided that the above
12
+ * copyright notice and this permission notice appear in all copies.
11 13
  *
12
- * ser is free software; you can redistribute it and/or modify
13
- * it under the terms of the GNU General Public License as published by
14
- * the Free Software Foundation; either version 2 of the License, or
15
- * (at your option) any later version
16
- *
17
- * For a license to use the ser software under conditions
18
- * other than those described here, or to purchase support for this
19
- * software, please contact iptel.org by e-mail at the following addresses:
20
- *    info@iptel.org
21
- *
22
- * ser is distributed in the hope that it will be useful,
23
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
- * GNU General Public License for more details.
26
- *
27
- * You should have received a copy of the GNU General Public License 
28
- * along with this program; if not, write to the Free Software 
29
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 21
  */
31 22
 /*
32 23
  *
... ...
@@ -3,26 +3,17 @@
3 3
  *
4 4
  * Copyright (C) 2001-2003 FhG Fokus
5 5
  *
6
- * This file is part of ser, a free SIP server.
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
7 9
  *
8
- * ser is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * For a license to use the ser software under conditions
14
- * other than those described here, or to purchase support for this
15
- * software, please contact iptel.org by e-mail at the following addresses:
16
- *    info@iptel.org
17
- *
18
- * ser is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License 
24
- * along with this program; if not, write to the Free Software 
25
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 17
  */
27 18
 
28 19
 /*
... ...
@@ -65,6 +56,22 @@ Implements:
65 65
 	int  lock_set_try(gen_lock_set_t* s, int i);    - tries to lock the sem i,
66 66
 	                                                  returns 0 on success and
67 67
 	                                                  -1 on failure
68
+	
69
+	defines:
70
+	--------
71
+	GEN_LOCK_T_PREFERRED - defined if using  arrays of gen_lock_t is as good as
72
+	                      using a lock set (gen_lock_set_t). 
73
+						  In general is better to have the locks "close" or 
74
+						  inside the protected data structure rather then 
75
+						  having a separate array or lock set. However in some
76
+						  case (e.g. SYSV_LOCKS) is better to use lock sets,
77
+						  either due to lock number limitations, excesive 
78
+						  performance or memory overhead. In this cases
79
+						  GEN_LOCK_T_PREFERRED will not be defined.
80
+	GEN_LOCK_T_UNLIMITED - defined if there is no system imposed limit on
81
+	                       the number of locks (other then the memory).
82
+	GEN_LOCK_SET_T_UNLIMITED
83
+	                      - like above but for the size of a lock set.
68 84
 
69 85
 WARNING: - lock_set_init may fail for large number of sems (e.g. sysv). 
70 86
          - signals are not treated! (some locks are "awakened" by the signals)
... ...
@@ -257,7 +264,10 @@ tryagain:
257 257
 /* lock sets */
258 258
 
259 259
 #if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || defined(USE_POSIX_SEM)
260
-#define GEN_LOCK_T_PREFERED
260
+#define GEN_LOCK_T_PREFERRED
261
+#define GEN_LOCK_T_PREFERED  /* backwards compat. */
262
+#define GEN_LOCK_T_UNLIMITED
263
+#define GEN_LOCK_SET_T_UNLIMITED
261 264
 
262 265
 struct gen_lock_set_t_ {
263 266
 	long size;
... ...
@@ -281,7 +291,12 @@ inline static gen_lock_set_t* lock_set_init(gen_lock_set_t* s)
281 281
 #define lock_set_release(set, i) lock_release(&set->locks[i])
282 282
 
283 283
 #elif defined(USE_SYSV_SEM)
284
-#undef GEN_LOCK_T_PREFERED
284
+#undef GEN_LOCK_T_PREFERRED
285
+#undef GEN_LOCK_T_PREFERED  /* backwards compat. */
286
+#undef GEN_LOCK_T_UNLIMITED
287
+#undef GEN_LOCK_SET_T_UNLIMITED
288
+#define GEN_LOCK_T_LIMITED
289
+#define GEN_LOCK_SET_T_LIMITED
285 290
 
286 291
 struct gen_lock_set_t_ {
287 292
 	int size;
... ...
@@ -41,7 +41,8 @@
41 41
 
42 42
 #include "../atomic_ops.h"
43 43
 
44
-#if defined ATOMIC_OPS_USE_LOCK  || defined MEMBAR_USES_LOCK
44
+#if defined ATOMIC_OPS_USE_LOCK  || defined MEMBAR_USES_LOCK || \
45
+	defined ATOMIC_OPS_USE_LOCK_SET
45 46
 /* hack to make lock work */
46 47
 #include "../lock_ops.h"
47 48
 #endif
... ...
@@ -51,11 +52,15 @@ gen_lock_t* __membar_lock=0; /* init in atomic_ops.c */
51 51
 gen_lock_t dummy_membar_lock;
52 52
 #endif
53 53
 
54
-#ifdef ATOMIC_OPS_USE_LOCK
54
+#ifdef ATOMIC_OPS_USE_LOCK_SET
55
+gen_lock_set_t* _atomic_lock_set=0;
56
+gen_lock_set_t dummy_atomic_lock_set;
57
+gen_lock_t locks_array[_ATOMIC_LS_SIZE];
58
+#elif defined ATOMIC_OPS_USE_LOCK
55 59
 gen_lock_t* _atomic_lock=0;
56 60
 gen_lock_t dummy_atomic_lock;
61
+#endif /* ATOMIC_OPS_USE_LOCK / _SET */
57 62
 
58
-#endif
59 63
 
60 64
 
61 65
 
... ...
@@ -100,7 +105,13 @@ static char* flags=
100 100
 	"no_asm_membar(slow) "
101 101
 #endif
102 102
 #ifndef HAVE_ASM_INLINE_ATOMIC_OPS
103
-	"no_asm_atomic_ops "
103
+	"no_asm_atomic_ops"
104
+#ifdef ATOMIC_OPS_USE_LOCK_SET
105
+	":use_lock_set"
106
+#elif defined ATOMIC_OPS_USE_LOCK
107
+	":use_lock"
108
+#endif
109
+	" "
104 110
 #endif
105 111
 #ifdef TYPE
106 112
 	STR(TYPE) " "
... ...
@@ -147,7 +158,7 @@ static char* flags=
147 147
 
148 148
 #define VERIFY(ops, y) \
149 149
 	ops ; \
150
-	CHECK_ERR( ops, get_val(v), y)
150
+	CHECK_ERR( ops, y, get_val(v))
151 151
 
152 152
 
153 153
 int main(int argc, char** argv)
... ...
@@ -170,7 +181,16 @@ int main(int argc, char** argv)
170 170
 	_membar_lock; /* start with the lock "taken" so that we can safely use
171 171
 					 unlock/lock sequences on it later */
172 172
 #endif
173
-#ifdef ATOMIC_OPS_USE_LOCK
173
+#ifdef ATOMIC_OPS_USE_LOCK_SET
174
+	/* init the lock (emulate atomic_ops.c) */
175
+	dummy_atomic_lock_set.locks=&locks_array[0];
176
+	_atomic_lock_set=&dummy_atomic_lock_set;
177
+	if (lock_set_init(_atomic_lock_set)==0){
178
+		fprintf(stderr, "ERROR: failed to initialize atomic_lock\n");
179
+		_atomic_lock_set=0;
180
+		goto error;
181
+	}
182
+#elif defined ATOMIC_OPS_USE_LOCK
174 183
 	/* init the lock (emulate atomic_ops.c) */
175 184
 	_atomic_lock=&dummy_atomic_lock;
176 185
 	if (lock_init(_atomic_lock)==0){
... ...
@@ -241,7 +261,9 @@ int main(int argc, char** argv)
241 241
 #ifdef MEMBAR_USES_LOCK
242 242
 	lock_destroy(__membar_lock);
243 243
 #endif
244
-#ifdef ATOMIC_OPS_USE_LOCK
244
+#ifdef ATOMIC_OPS_USE_LOCK_SET
245
+	lock_set_destroy(_atomic_lock_set);
246
+#elif defined ATOMIC_OPS_USE_LOCK
245 247
 	lock_destroy(_atomic_lock);
246 248
 #endif
247 249
 	return 0;
... ...
@@ -250,7 +272,10 @@ error:
250 250
 	if (__membar_lock)
251 251
 		lock_destroy(__membar_lock);
252 252
 #endif
253
-#ifdef ATOMIC_OPS_USE_LOCK
253
+#ifdef ATOMIC_OPS_USE_LOCK_SET
254
+	if (_atomic_lock_set)
255
+		lock_set_destroy(_atomic_lock_set);
256
+#elif defined ATOMIC_OPS_USE_LOCK
254 257
 	if (_atomic_lock)
255 258
 		lock_destroy(_atomic_lock);
256 259
 #endif