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 276
 	return atomic_get_and_set_int(v, i);
286 277
 }
287 278
 
279
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
280
+{
281
+	membar();
282
+	return atomic_cmpxchg_int(v, o, n);
283
+}
284
+
285
+inline static int mb_atomic_add_int(volatile int* v, int i)
286
+{
287
+	membar();
288
+	return atomic_add_int(v, i);
289
+}
290
+
288 291
 
289 292
 
290 293
 #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 350
 }
348 351
 
349 352
 
353
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
354
+{
355
+	membar();
356
+	return atomic_cmpxchg_long(v, o, n);
357
+}
358
+
359
+inline static long mb_atomic_add_long(volatile long* v, long i)
360
+{
361
+	membar();
362
+	return atomic_add_long(v, i);
363
+}
364
+
365
+
350 366
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
351 367
 #define mb_atomic_dec(var) mb_atomic_dec_int(&(var)->val)
352 368
 #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 372
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
357 373
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
358 374
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
375
+#define mb_atomic_cmpxchg(var, o, n) mb_atomic_cmpxchg_int(&(var)->val, o, n)
376
+#define mb_atomic_add(var, i) mb_atomic_add_int(&(var)->val, i)
377
+
359 378
 
360 379
 #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 251
 	return atomic_get_and_set_int(v, i);
261 252
 }
262 253
 
254
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
255
+{
256
+	membar();
257
+	return atomic_cmpxchg_int(v, o, n);
258
+}
259
+
260
+inline static int mb_atomic_add_int(volatile int* v, int i)
261
+{
262
+	membar();
263
+	return atomic_add_int(v, i);
264
+}
265
+
263 266
 
264 267
 
265 268
 #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 324
 	return atomic_get_and_set_long(v, l);
322 325
 }
323 326
 
327
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
328
+{
329
+	membar();
330
+	return atomic_cmpxchg_long(v, o, n);
331
+}
332
+
333
+inline static long mb_atomic_add_long(volatile long* v, long i)
334
+{
335
+	membar();
336
+	return atomic_add_long(v, i);
337
+}
324 338
 
325 339
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
326 340
 #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 345
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
332 346
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
333 347
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
348
+#define mb_atomic_cmpxchg(var, o, n) mb_atomic_cmpxchg_int(&(var)->val, o, n)
349
+#define mb_atomic_add(var, i) mb_atomic_add_int(&(var)->val, i)
350
+
334 351
 
335 352
 
336 353
 #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 285
 	return atomic_get_and_set_int(v, i);
295 286
 }
296 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
+}
297 299
 
298 300
 
299 301
 #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 357
 	return atomic_get_and_set_long(v, l);
356 358
 }
357 359
 
360
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
361
+{
362
+	membar();
363
+	return atomic_cmpxchg_long(v, o, n);
364
+}
365
+
366
+inline static long mb_atomic_add_long(volatile long* v, long i)
367
+{
368
+	membar();
369
+	return atomic_add_long(v, i);
370
+}
371
+
358 372
 
359 373
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
360 374
 #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 379
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
366 380
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
367 381
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
382
+#define mb_atomic_cmpxchg(var, o, n) mb_atomic_cmpxchg_int(&(var)->val, o, n)
383
+#define mb_atomic_add(var, i) mb_atomic_add_int(&(var)->val, i)
368 384
 
369 385
 #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 29
  * History:
39 30
  * --------
40 31
  *  2006-03-24  created by andrei
32
+ *  2007-05-11  added atomic_add and atomic_cmpxchg (andrei)
41 33
  */
42 34
 
43 35
 #ifndef _atomic_ppc_h
... ...
@@ -133,23 +125,44 @@
133 125
 		return RET_EXPR; \
134 126
 	}
135 127
 
128
+/* cmpxchg, %3=var, %0=*var, %4=old, %3=new  */
129
+#define ATOMIC_CMPXCHG_DECL(NAME, P_TYPE) \
130
+	inline static P_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var, \
131
+															P_TYPE old \
132
+															P_TYPE new_v) \
133
+	{ \
134
+		P_TYPE ret; \
135
+		asm volatile( \
136
+			ATOMIC_ASM_OP3_##P_TYPE("cmpw %0, %4 \n\t bne- 2f") \
137
+			: "=&r"(ret), "=m"(*var) : "r"(var), "r"(new_v), "r"(old) \
138
+				: "cc" \
139
+			); \
140
+		return ret; \
141
+	}
142
+
143
+
144
+
136 145
 
137 146
 
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 */ )
147
+ATOMIC_FUNC_DECL(inc,      "addi  %0, %0,  1", int, void, /* no return */ )
148
+ATOMIC_FUNC_DECL(dec,      "addi %0, %0,  -1", int, void, /* no return */ )
140 149
 ATOMIC_FUNC_DECL1(and,     "and     %0, %0, %3", int, void, /* no return */ )
141 150
 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) )
151
+ATOMIC_FUNC_DECL(inc_and_test, "addi   %0, %0, 1", int, int, (ret==0) )
152
+ATOMIC_FUNC_DECL(dec_and_test, "addi  %0, %0, -1", int, int, (ret==0) )
144 153
 ATOMIC_FUNC_DECL3(get_and_set, /* no extra op needed */ , int, int,  ret)
154
+ATOMIC_CMPXCHG_DECL(cmpxchg, int)
155
+ATOMIC_FUNC_DECL1(add, "add %0, %0, %3" , int, int,  ret)
145 156
 
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 */ )
157
+ATOMIC_FUNC_DECL(inc,      "addi  %0, %0,  1", long, void, /* no return */ )
158
+ATOMIC_FUNC_DECL(dec,      "addi %0, %0,  -1", long, void, /* no return */ )
148 159
 ATOMIC_FUNC_DECL1(and,     "and     %0, %0, %3",long, void, /* no return */ )
149 160
 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) )
161
+ATOMIC_FUNC_DECL(inc_and_test, "addi   %0, %0, 1", long, long, (ret==0) )
162
+ATOMIC_FUNC_DECL(dec_and_test, "addi  %0, %0, -1", long, long, (ret==0) )
152 163
 ATOMIC_FUNC_DECL3(get_and_set, /* no extra op needed */ , long, long,  ret)
164
+ATOMIC_CMPXCHG_DECL(cmpxchg, long)
165
+ATOMIC_FUNC_DECL1(add, "add %0, %0, %3" , long, long,  ret)
153 166
 
154 167
 
155 168
 #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 172
 #define atomic_dec_and_test(var) atomic_dec_and_test_int(&(var)->val)
160 173
 #define atomic_inc_and_test(var) atomic_inc_and_test_int(&(var)->val)
161 174
 #define atomic_get_and_set(var, i) atomic_get_and_set_int(&(var)->val, i)
175
+#define atomic_cmpxchg(var, o, n) atomic_cmpxchg_int(&(var)->val, (o), (n))
176
+#define atomic_add(var, i) atomic_add_int(&(var)->val, (i))
162 177
 
163 178
 
164 179
 /* with integrated membar */
... ...
@@ -221,6 +236,17 @@ inline static int mb_atomic_get_and_set_int(volatile int* v, int i)
221 236
 	return atomic_get_and_set_int(v, i);
222 237
 }
223 238
 
239
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
240
+{
241
+	membar();
242
+	return atomic_cmpxchg_int(v, o, n);
243
+}
244
+
245
+inline static int mb_atomic_add_int(volatile int* v, int i)
246
+{
247
+	membar();
248
+	return atomic_add_int(v, i);
249
+}
224 250
 
225 251
 
226 252
 #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 308
 	return atomic_get_and_set_long(v, l);
283 309
 }
284 310
 
311
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
312
+{
313
+	membar();
314
+	return atomic_cmpxchg_long(v, o, n);
315
+}
316
+
317
+inline static long mb_atomic_add_long(volatile long* v, long i)
318
+{
319
+	membar();
320
+	return atomic_add_long(v, i);
321
+}
322
+
323
+
285 324
 
286 325
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
287 326
 #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 331
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
293 332
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
294 333
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
334
+#define mb_atomic_cmpxchg(v, o, n)	atomic_cmpxchg_int(&(v)->val, o, n)
335
+#define mb_atomic_add(v, a)	atomic_add_int(&(v)->val, a)
336
+
295 337
 
296 338
 #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 245
 	return atomic_get_and_set_int(v, i);
255 246
 }
256 247
 
248
+inline static int mb_atomic_cmpxchg_int(volatile int* v, int o, int n)
249
+{
250
+	membar();
251
+	return atomic_cmpxchg_int(v, o, n);
252
+}
253
+
254
+inline static int mb_atomic_add_int(volatile int* v, int i)
255
+{
256
+	membar();
257
+	return atomic_add_int(v, i);
258
+}
259
+
257 260
 
258 261
 
259 262
 #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 318
 	return atomic_get_and_set_long(v, l);
316 319
 }
317 320
 
321
+inline static long mb_atomic_cmpxchg_long(volatile long* v, long o, long n)
322
+{
323
+	membar();
324
+	return atomic_cmpxchg_long(v, o, n);
325
+}
326
+
327
+inline static long mb_atomic_add_long(volatile long* v, long i)
328
+{
329
+	membar();
330
+	return atomic_add_long(v, i);
331
+}
332
+
318 333
 
319 334
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
320 335
 #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 340
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
326 341
 #define mb_atomic_set(var, i)	mb_atomic_set_int(&(var)->val, i)
327 342
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
343
+#define mb_atomic_cmpxchg(var, o, n) mb_atomic_cmpxchg_int(&(var)->val, o, n)
344
+#define mb_atomic_add(var, i) mb_atomic_add_int(&(var)->val, i)
345
+
328 346
 
329 347
 #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 33
  * History:
43 34
  * --------
44 35
  *  2006-03-08  created by andrei
36
+ *  2007-05-11  added atomic_add and atomic_cmpxchg 
37
+ *              use lock_set if lock economy is not needed (andrei)
45 38
  */
46 39
 
47 40
 #ifndef _atomic_unknown_h
... ...
@@ -94,15 +87,33 @@ extern gen_lock_t* __membar_lock; /* init in atomic_ops.c */
94 87
 
95 88
 #ifndef HAVE_ASM_INLINE_ATOMIC_OPS
96 89
 
90
+#ifdef GEN_LOCK_SET_T_UNLIMITED
91
+#ifndef ATOMIC_OPS_USE_LOCK_SET
92
+#define ATOMIC_OPS_USE_LOCK_SET
93
+#endif
94
+#else
97 95
 #ifndef ATOMIC_OPS_USE_LOCK
98 96
 #define ATOMIC_OPS_USE_LOCK
99 97
 #endif
98
+#endif /* GEN_LOCK_SET_T_UNLIMITED */
100 99
 
100
+#ifdef ATOMIC_OPS_USE_LOCK_SET 
101
+#define _ATOMIC_LS_SIZE	256
102
+/* hash after the variable address: ignore first 4 bits since
103
+ * vars are generally alloc'ed at at least 16 bytes multiples */
104
+#define _atomic_ls_hash(v)  ((((unsigned long)(v))>>4)&(_ATOMIC_LS_SIZE-1))
105
+extern gen_lock_set_t* _atomic_lock_set;
106
+
107
+#define atomic_lock(v)   lock_set_get(_atomic_lock_set, _atomic_ls_hash(v))
108
+#define atomic_unlock(v) lock_set_release(_atomic_lock_set, _atomic_ls_hash(v))
109
+
110
+#else
101 111
 extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
102 112
 
103
-#define atomic_lock    lock_get(_atomic_lock)
104
-#define atomic_unlock  lock_release(_atomic_lock)
113
+#define atomic_lock(v)    lock_get(_atomic_lock)
114
+#define atomic_unlock(v)  lock_release(_atomic_lock)
105 115
 
116
+#endif /* ATOMIC_OPS_USE_LOCK_SET */
106 117
 
107 118
 /* atomic ops */
108 119
 
... ...
@@ -111,9 +122,9 @@ extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
111 122
 #define ATOMIC_FUNC_DECL(NAME, OP, P_TYPE, RET_TYPE, RET_EXPR) \
112 123
 	inline static RET_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var) \
113 124
 	{ \
114
-		atomic_lock; \
125
+		atomic_lock(var); \
115 126
 		OP ; \
116
-		atomic_unlock; \
127
+		atomic_unlock(var); \
117 128
 		return RET_EXPR; \
118 129
 	}
119 130
 
... ...
@@ -124,9 +135,9 @@ extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
124 135
 	inline static RET_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var, \
125 136
 														P_TYPE v) \
126 137
 	{ \
127
-		atomic_lock; \
138
+		atomic_lock(var); \
128 139
 		OP ; \
129
-		atomic_unlock; \
140
+		atomic_unlock(var); \
130 141
 		return RET_EXPR; \
131 142
 	}
132 143
 
... ...
@@ -136,9 +147,9 @@ extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
136 147
 	inline static RET_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var) \
137 148
 	{ \
138 149
 		P_TYPE ret; \
139
-		atomic_lock; \
150
+		atomic_lock(var); \
140 151
 		OP ; \
141
-		atomic_unlock; \
152
+		atomic_unlock(var); \
142 153
 		return RET_EXPR; \
143 154
 	}
144 155
 
... ...
@@ -148,12 +159,25 @@ extern gen_lock_t* _atomic_lock; /* declared and init in ../atomic_ops.c */
148 159
 														P_TYPE v) \
149 160
 	{ \
150 161
 		P_TYPE ret; \
151
-		atomic_lock; \
162
+		atomic_lock(var); \
163
+		OP ; \
164
+		atomic_unlock(var); \
165
+		return RET_EXPR; \
166
+	}
167
+
168
+/* like ATOMIC_FUNC_DECL1_RET, but takes an extra param */
169
+#define ATOMIC_FUNC_DECL2_RET(NAME, OP, P_TYPE, RET_TYPE, RET_EXPR) \
170
+	inline static RET_TYPE atomic_##NAME##_##P_TYPE (volatile P_TYPE *var, \
171
+														P_TYPE v1, P_TYPE v2)\
172
+	{ \
173
+		P_TYPE ret; \
174
+		atomic_lock(var); \
152 175
 		OP ; \
153
-		atomic_unlock; \
176
+		atomic_unlock(var); \
154 177
 		return RET_EXPR; \
155 178
 	}
156 179
 
180
+
157 181
 ATOMIC_FUNC_DECL(inc,      (*var)++, int, void, /* no return */ )
158 182
 ATOMIC_FUNC_DECL(dec,      (*var)--, int, void, /* no return */ )
159 183
 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 185
 ATOMIC_FUNC_DECL_RET(inc_and_test, ret=++(*var), int, int, (ret==0) )
162 186
 ATOMIC_FUNC_DECL_RET(dec_and_test, ret=--(*var), int, int, (ret==0) )
163 187
 ATOMIC_FUNC_DECL1_RET(get_and_set, ret=*var;*var=v , int, int,  ret)
188
+ATOMIC_FUNC_DECL2_RET(cmpxchg, ret=*var;\
189
+							*var=(((ret!=v1)-1)&v2)+(~((ret!=v1)-1)&ret),\
190
+							int, int,  ret)
191
+ATOMIC_FUNC_DECL1_RET(add, *var+=v;ret=*var, int, int, ret )
164 192
 
165 193
 ATOMIC_FUNC_DECL(inc,      (*var)++, long, void, /* no return */ )
166 194
 ATOMIC_FUNC_DECL(dec,      (*var)--, long, void, /* no return */ )
... ...
@@ -169,6 +197,10 @@ ATOMIC_FUNC_DECL1(or,      *var|=v, long, void, /* no return */ )
169 197
 ATOMIC_FUNC_DECL_RET(inc_and_test, ret=++(*var), long, long, (ret==0) )
170 198
 ATOMIC_FUNC_DECL_RET(dec_and_test, ret=--(*var), long, long, (ret==0) )
171 199
 ATOMIC_FUNC_DECL1_RET(get_and_set, ret=*var;*var=v , long, long,  ret)
200
+ATOMIC_FUNC_DECL2_RET(cmpxchg, ret=*var;\
201
+							*var=(((ret!=v1)-1)&v2)+(~((ret!=v1)-1)&ret),\
202
+							long, long,  ret)
203
+ATOMIC_FUNC_DECL1_RET(add, *var+=v;ret=*var, long, long, ret )
172 204
 
173 205
 
174 206
 #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 210
 #define atomic_dec_and_test(var) atomic_dec_and_test_int(&(var)->val)
179 211
 #define atomic_inc_and_test(var) atomic_inc_and_test_int(&(var)->val)
180 212
 #define atomic_get_and_set(var, i) atomic_get_and_set_int(&(var)->val, i)
213
+#define atomic_cmpxchg(var, old, new_v) \
214
+	atomic_cmpxchg_int(&(var)->val, old, new_v)
215
+#define atomic_add(var, v) atomic_add_int(&(var)->val, v)
181 216
 
182 217
 
183 218
 /* 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 256
 #define mb_atomic_inc_and_test_int(v)	atomic_inc_and_test_int(v)
222 257
 #define mb_atomic_dec_and_test_int(v)	atomic_dec_and_test_int(v)
223 258
 #define mb_atomic_get_and_set_int(v, i)	atomic_get_and_set_int(v, i)
259
+#define mb_atomic_cmpxchg_int(v, o, n)	atomic_cmpxchg_int(v, o, n)
260
+#define mb_atomic_add_int(v, i)	atomic_add_int(v, i)
224 261
 
225 262
 #define mb_atomic_inc_long(v)	atomic_inc_long(v)
226 263
 #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 266
 #define mb_atomic_inc_and_test_long(v)	atomic_inc_and_test_long(v)
230 267
 #define mb_atomic_dec_and_test_long(v)	atomic_dec_and_test_long(v)
231 268
 #define mb_atomic_get_and_set_long(v, i)	atomic_get_and_set_long(v, i)
269
+#define mb_atomic_cmpxchg_long(v, o, n)	atomic_cmpxchg_long(v, o, n)
270
+#define mb_atomic_add_long(v, i)	atomic_add_long(v, i)
232 271
 
233 272
 #define mb_atomic_inc(var) mb_atomic_inc_int(&(var)->val)
234 273
 #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 276
 #define mb_atomic_dec_and_test(var) mb_atomic_dec_and_test_int(&(var)->val)
238 277
 #define mb_atomic_inc_and_test(var) mb_atomic_inc_and_test_int(&(var)->val)
239 278
 #define mb_atomic_get_and_set(var, i) mb_atomic_get_and_set_int(&(var)->val, i)
279
+#define mb_atomic_cmpxchg(v, o, n)	atomic_cmpxchg_int(&(v)->val, o, n)
280
+#define mb_atomic_add(v, i)	atomic_add_int(&(v)->val, i)
240 281
 
241 282
 #define mb_atomic_get(var)	mb_atomic_get_int(&(var)->val)
242 283
 #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 326
 #define mb_atomic_inc_and_test_int(v)	atomic_inc_and_test_int(v)
336 327
 #define mb_atomic_dec_and_test_int(v)	atomic_dec_and_test_int(v)
337 328
 #define mb_atomic_get_and_set_int(v, i)	atomic_get_and_set_int(v, i)
329
+#define mb_atomic_cmpxchg_int(v, o, n)	atomic_cmpxchg_int(v, o, n)
330
+#define mb_atomic_add_int(v, a)	atomic_add_int(v, a)
338 331
 
339 332
 #define mb_atomic_inc_long(v)	atomic_inc_long(v)
340 333
 #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 336
 #define mb_atomic_inc_and_test_long(v)	atomic_inc_and_test_long(v)
344 337
 #define mb_atomic_dec_and_test_long(v)	atomic_dec_and_test_long(v)
345 338
 #define mb_atomic_get_and_set_long(v, i)	atomic_get_and_set_long(v, i)
339
+#define mb_atomic_cmpxchg_long(v, o, n)	atomic_cmpxchg_long(v, o, n)
340
+#define mb_atomic_add_long(v, a)	atomic_add_long(v, a)
346 341
 
347 342
 #define mb_atomic_inc(v)	atomic_inc(v)
348 343
 #define mb_atomic_dec(v)	atomic_dec(v)
... ...
@@ -353,6 +348,8 @@ inline static long mb_atomic_get_long(volatile long* var)
353 348
 #define mb_atomic_get(v)	mb_atomic_get_int( &(v)->val)
354 349
 #define mb_atomic_set(v, i)	mb_atomic_set_int(&(v)->val, i)
355 350
 #define mb_atomic_get_and_set(v, i)	atomic_get_and_set_int(&(v)->val, i)
351
+#define mb_atomic_cmpxchg(v, o, n)	atomic_cmpxchg_int(&(v)->val, o, n)
352
+#define mb_atomic_add(v, a)	atomic_add_int(&(v)->val, a)
356 353
 
357 354
 
358 355
 #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 22
  * History:
32 23
  * --------
33 24
  *  2006-03-08  created by andrei
25
+ *  2007-05-11  added lock_set support (andrei)
34 26
  */
35 27
 
36 28
 #include "atomic_ops_init.h"
37 29
 #include "atomic_ops.h"
38 30
 
39
-#if defined ATOMIC_OPS_USE_LOCK  || defined MEMBAR_USES_LOCK
31
+#if defined ATOMIC_OPS_USE_LOCK  || defined ATOMIC_OPS_USE_LOCK_SET || \
32
+	defined MEMBAR_USES_LOCK
40 33
 #include "locking.h"
41 34
 #endif
42 35
 
... ...
@@ -44,7 +37,9 @@
44 37
 gen_lock_t* __membar_lock=0; /* init in atomic_ops.c */
45 38
 #endif
46 39
 
47
-#ifdef ATOMIC_OPS_USE_LOCK
40
+#ifdef ATOMIC_OPS_USE_LOCK_SET
41
+gen_lock_set_t* _atomic_lock_set=0;
42
+#elif defined ATOMIC_OPS_USE_LOCK
48 43
 gen_lock_t* _atomic_lock=0;
49 44
 #endif
50 45
 
... ...
@@ -65,7 +60,16 @@ int init_atomic_ops()
65 60
 	_membar_lock; /* start with the lock "taken" so that we can safely use
66 61
 					 unlock/lock sequences on it later */
67 62
 #endif
68
-#ifdef ATOMIC_OPS_USE_LOCK
63
+#ifdef ATOMIC_OPS_USE_LOCK_SET
64
+	if ((_atomic_lock_set=lock_set_alloc(_ATOMIC_LS_SIZE))==0){
65
+		goto error;
66
+	}
67
+	if (lock_set_init(_atomic_lock_set)==0){
68
+		lock_set_dealloc(_atomic_lock_set);
69
+		_atomic_lock_set=0;
70
+		goto error;
71
+	}
72
+#elif defined ATOMIC_OPS_USE_LOCK
69 73
 	if ((_atomic_lock=lock_alloc())==0){
70 74
 		goto error;
71 75
 	}
... ...
@@ -74,9 +78,10 @@ int init_atomic_ops()
74 78
 		_atomic_lock=0;
75 79
 		goto error;
76 80
 	}
77
-#endif
81
+#endif /* ATOMIC_OPS_USE_LOCK_SET/ATOMIC_OPS_USE_LOCK */
78 82
 	return 0;
79
-#if defined MEMBAR_USES_LOCK || defined ATOMIC_OPS_USE_LOCK
83
+#if defined MEMBAR_USES_LOCK || defined ATOMIC_OPS_USE_LOCK || \
84
+	defined ATOMIC_OPS_USE_LOCK_SET
80 85
 error:
81 86
 	destroy_atomic_ops();
82 87
 	return -1;
... ...
@@ -94,11 +99,17 @@ void destroy_atomic_ops()
94 99
 		__membar_lock=0;
95 100
 	}
96 101
 #endif
97
-#ifdef ATOMIC_OPS_USE_LOCK
102
+#ifdef ATOMIC_OPS_USE_LOCK_SET
103
+	if (_atomic_lock_set!=0){
104
+		lock_set_destroy(_atomic_lock_set);
105
+		lock_set_dealloc(_atomic_lock_set);
106
+		_atomic_lock_set=0;
107
+	}
108
+#elif defined ATOMIC_OPS_USE_LOCK
98 109
 	if (_atomic_lock!=0){
99 110
 		lock_destroy(_atomic_lock);
100 111
 		lock_dealloc(_atomic_lock);
101 112
 		_atomic_lock=0;
102 113
 	}
103
-#endif
114
+#endif /* ATOMIC_OPS_USE_LOCK_SET / ATOMIC_OPS_USE_LOCK*/
104 115
 }
... ...
@@ -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 56
 	int  lock_set_try(gen_lock_set_t* s, int i);    - tries to lock the sem i,
66 57
 	                                                  returns 0 on success and
67 58
 	                                                  -1 on failure
59
+	
60
+	defines:
61
+	--------
62
+	GEN_LOCK_T_PREFERRED - defined if using  arrays of gen_lock_t is as good as
63
+	                      using a lock set (gen_lock_set_t). 
64
+						  In general is better to have the locks "close" or 
65
+						  inside the protected data structure rather then 
66
+						  having a separate array or lock set. However in some
67
+						  case (e.g. SYSV_LOCKS) is better to use lock sets,
68
+						  either due to lock number limitations, excesive 
69
+						  performance or memory overhead. In this cases
70
+						  GEN_LOCK_T_PREFERRED will not be defined.
71
+	GEN_LOCK_T_UNLIMITED - defined if there is no system imposed limit on
72
+	                       the number of locks (other then the memory).
73
+	GEN_LOCK_SET_T_UNLIMITED
74
+	                      - like above but for the size of a lock set.
68 75
 
69 76
 WARNING: - lock_set_init may fail for large number of sems (e.g. sysv). 
70 77
          - signals are not treated! (some locks are "awakened" by the signals)
... ...
@@ -257,7 +264,10 @@ tryagain:
257 264
 /* lock sets */
258 265
 
259 266
 #if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || defined(USE_POSIX_SEM)
260
-#define GEN_LOCK_T_PREFERED
267
+#define GEN_LOCK_T_PREFERRED
268
+#define GEN_LOCK_T_PREFERED  /* backwards compat. */
269
+#define GEN_LOCK_T_UNLIMITED
270
+#define GEN_LOCK_SET_T_UNLIMITED
261 271
 
262 272
 struct gen_lock_set_t_ {
263 273
 	long size;
... ...
@@ -281,7 +291,12 @@ inline static gen_lock_set_t* lock_set_init(gen_lock_set_t* s)
281 291
 #define lock_set_release(set, i) lock_release(&set->locks[i])
282 292
 
283 293
 #elif defined(USE_SYSV_SEM)
284
-#undef GEN_LOCK_T_PREFERED
294
+#undef GEN_LOCK_T_PREFERRED
295
+#undef GEN_LOCK_T_PREFERED  /* backwards compat. */
296
+#undef GEN_LOCK_T_UNLIMITED
297
+#undef GEN_LOCK_SET_T_UNLIMITED
298
+#define GEN_LOCK_T_LIMITED
299
+#define GEN_LOCK_SET_T_LIMITED
285 300
 
286 301
 struct gen_lock_set_t_ {
287 302
 	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 52
 gen_lock_t dummy_membar_lock;
52 53
 #endif
53 54
 
54
-#ifdef ATOMIC_OPS_USE_LOCK
55
+#ifdef ATOMIC_OPS_USE_LOCK_SET
56
+gen_lock_set_t* _atomic_lock_set=0;
57
+gen_lock_set_t dummy_atomic_lock_set;
58
+gen_lock_t locks_array[_ATOMIC_LS_SIZE];
59
+#elif defined ATOMIC_OPS_USE_LOCK
55 60
 gen_lock_t* _atomic_lock=0;
56 61
 gen_lock_t dummy_atomic_lock;
62
+#endif /* ATOMIC_OPS_USE_LOCK / _SET */
57 63
 
58
-#endif
59 64
 
60 65
 
61 66
 
... ...
@@ -100,7 +105,13 @@ static char* flags=
100 105
 	"no_asm_membar(slow) "
101 106
 #endif
102 107
 #ifndef HAVE_ASM_INLINE_ATOMIC_OPS
103
-	"no_asm_atomic_ops "
108
+	"no_asm_atomic_ops"
109
+#ifdef ATOMIC_OPS_USE_LOCK_SET
110
+	":use_lock_set"
111
+#elif defined ATOMIC_OPS_USE_LOCK
112
+	":use_lock"
113
+#endif
114
+	" "
104 115
 #endif
105 116
 #ifdef TYPE
106 117
 	STR(TYPE) " "
... ...
@@ -147,7 +158,7 @@ static char* flags=
147 158
 
148 159
 #define VERIFY(ops, y) \
149 160
 	ops ; \
150
-	CHECK_ERR( ops, get_val(v), y)
161
+	CHECK_ERR( ops, y, get_val(v))
151 162
 
152 163
 
153 164
 int main(int argc, char** argv)
... ...
@@ -170,7 +181,16 @@ int main(int argc, char** argv)
170 181
 	_membar_lock; /* start with the lock "taken" so that we can safely use
171 182
 					 unlock/lock sequences on it later */
172 183
 #endif
173
-#ifdef ATOMIC_OPS_USE_LOCK
184
+#ifdef ATOMIC_OPS_USE_LOCK_SET
185
+	/* init the lock (emulate atomic_ops.c) */
186
+	dummy_atomic_lock_set.locks=&locks_array[0];
187
+	_atomic_lock_set=&dummy_atomic_lock_set;
188
+	if (lock_set_init(_atomic_lock_set)==0){
189
+		fprintf(stderr, "ERROR: failed to initialize atomic_lock\n");
190
+		_atomic_lock_set=0;
191
+		goto error;
192
+	}
193
+#elif defined ATOMIC_OPS_USE_LOCK
174 194
 	/* init the lock (emulate atomic_ops.c) */
175 195
 	_atomic_lock=&dummy_atomic_lock;
176 196
 	if (lock_init(_atomic_lock)==0){
... ...
@@ -241,7 +261,9 @@ int main(int argc, char** argv)
241 261
 #ifdef MEMBAR_USES_LOCK
242 262
 	lock_destroy(__membar_lock);
243 263
 #endif
244
-#ifdef ATOMIC_OPS_USE_LOCK
264
+#ifdef ATOMIC_OPS_USE_LOCK_SET
265
+	lock_set_destroy(_atomic_lock_set);
266
+#elif defined ATOMIC_OPS_USE_LOCK
245 267
 	lock_destroy(_atomic_lock);
246 268
 #endif
247 269
 	return 0;
... ...
@@ -250,7 +272,10 @@ error:
250 272
 	if (__membar_lock)
251 273
 		lock_destroy(__membar_lock);
252 274
 #endif
253
-#ifdef ATOMIC_OPS_USE_LOCK
275
+#ifdef ATOMIC_OPS_USE_LOCK_SET
276
+	if (_atomic_lock_set)
277
+		lock_set_destroy(_atomic_lock_set);
278
+#elif defined ATOMIC_OPS_USE_LOCK
254 279
 	if (_atomic_lock)
255 280
 		lock_destroy(_atomic_lock);
256 281
 #endif