Browse code

- timer: proper includes for no fast lock case - Makefile: mips gcc params fixes - atomic_* updates [work in progress]

Andrei Pelinescu-Onciul authored on 13/03/2006 20:20:48
Showing 5 changed files
... ...
@@ -726,27 +726,26 @@ ifeq	($(ARCH), mips)
726 726
 		# if gcc 
727 727
 ifeq		($(CC_NAME), gcc)
728 728
 				#common stuff
729
-				CFLAGS=-O9 -funroll-loops  -Wcast-align $(PROFILE) \
729
+				CFLAGS=-O9 -funroll-loops  $(PROFILE) \
730 730
 					-Wall 
731 731
 			#if gcc 4.0+
732 732
 ifeq			($(CC_SHORTVER), 4.x)
733
-					CFLAGS+=-mcpu=r3000 -minline-all-stringops \
733
+					CFLAGS+=-march=r3000 -minline-all-stringops \
734 734
 							-ftree-vectorize
735 735
 else
736 736
 			#if gcc 3.4+
737 737
 ifeq			($(CC_SHORTVER), 3.4)
738
-					CFLAGS+= -mcpu=r3000
738
+					CFLAGS+= -march=r3000
739 739
 else
740 740
 			#if gcc 3.0
741 741
 ifeq			($(CC_SHORTVER), 3.0)
742
-					CFLAGS+= -mcpu=r3000
743
-							#-mcpu=athlon
742
+					CFLAGS+= -march=r3000
744 743
 else
745 744
 ifeq			($(CC_SHORTVER), 2.9x) #older gcc version (2.9[1-5])
746 745
 $(warning 			Old gcc detected ($(CC_SHORTVER)), use  gcc 3.0.x \
747 746
 					for better results)
748 747
 					
749
-					CFLAGS+=-mcpu=r3000
748
+					CFLAGS+=-march=r3000
750 749
 else
751 750
 				#really old version
752 751
 $(warning			You are using an old and unsupported gcc \
... ...
@@ -36,7 +36,7 @@
36 36
 #include "atomic_ops.h"
37 37
 
38 38
 #ifdef ATOMIC_USE_LOCK
39
-gen_lock_t* atomic_lock;
39
+gen_lock_t* _atomic_lock;
40 40
 #endif
41 41
 
42 42
 
... ...
@@ -47,14 +47,14 @@ int atomic_ops_init()
47 47
 	
48 48
 	ret=0;
49 49
 #ifdef ATOMIC_USE_LOCK
50
-	if ((atomic_lock=lock_alloc())==0){
50
+	if ((_atomic_lock=lock_alloc())==0){
51 51
 		ret=-1;
52 52
 		goto end;
53 53
 	}
54
-	if (lock_init(atomic_lock)==0){
54
+	if (lock_init(_atomic_lock)==0){
55 55
 		ret=-1;
56
-		lock_destroy(atomic_lock);
57
-		atomic_lock=0;
56
+		lock_destroy(_atomic_lock);
57
+		_atomic_lock=0;
58 58
 		goto end;
59 59
 	}
60 60
 end:
... ...
@@ -43,6 +43,8 @@
43 43
  * ------------------
44 44
  *  type: atomic_t
45 45
  *
46
+ * not including memory barriers:
47
+ *
46 48
  *  void atomic_set(atomic_t* v, long i)      -      v->val=i
47 49
  *  long atomic_get(atomic_t* v)              -       return v->val
48 50
  *  void atomic_inc(atomic_t* v)
... ...
@@ -51,7 +53,18 @@
51 53
  *  long atomic_dec_and_test(atomic_t* v)     - returns 1 if the result is 0
52 54
  *  void atomic_or (atomic_t* v, long mask)   - v->val|=mask 
53 55
  *  void atomic_and(atomic_t* v, long mask)   - v->val&=mask
54
- *  
56
+ * 
57
+ * same ops, but with builtin memory barriers:
58
+ *
59
+ *  void mb_atomic_set(atomic_t* v, long i)      -      v->val=i
60
+ *  long mb_atomic_get(atomic_t* v)              -       return v->val
61
+ *  void mb_atomic_inc(atomic_t* v)
62
+ *  void mb_atomic_dec(atomic_t* v)
63
+ *  long mb_atomic_inc_and_test(atomic_t* v)  - returns 1 if the result is 0
64
+ *  long mb_atomic_dec_and_test(atomic_t* v)  - returns 1 if the result is 0
65
+ *  void mb_atomic_or(atomic_t* v, long mask - v->val|=mask 
66
+ *  void mb_atomic_and(atomic_t* v, long mask)- v->val&=mask
67
+ * 
55 68
  */
56 69
 /* 
57 70
  * History:
... ...
@@ -79,6 +92,8 @@ int atomic_ops_init();
79 92
 
80 93
 #if defined(__CPU_i386) || defined(__CPU_x86_64)
81 94
 
95
+#define HAVE_ASM_INLINE_ATOMIC_OPS
96
+
82 97
 #ifdef NOSMP
83 98
 #define __LOCK_PREF 
84 99
 #else
... ...
@@ -86,14 +101,13 @@ int atomic_ops_init();
86 101
 #endif
87 102
 
88 103
 
89
-
90 104
 /* memory barriers */
91 105
 
92 106
 #ifdef NOSMP
93 107
 
94
-#define membar()
95
-#define membar_read()
96
-#define membar_write()
108
+#define membar()	asm volatile ("" : : : "memory")
109
+#define membar_read()	membar()
110
+#define membar_write()	membar()
97 111
 
98 112
 #else
99 113
 
... ...
@@ -101,7 +115,7 @@ int atomic_ops_init();
101 115
  *  oostore ready write barriers */
102 116
 #define X86_OOSTORE 
103 117
 
104
-/* membar, mfence, lfence, sfence available only on newer cpus, so for now
118
+/* membar: lfence, mfence, sfence available only on newer cpus, so for now
105 119
  * stick to lock addl */
106 120
 #define membar() \
107 121
 	asm volatile( \
... ...
@@ -115,7 +129,7 @@ int atomic_ops_init();
115 129
 /* out of order store version */
116 130
 #define membar_write()	membar()
117 131
 #else
118
-/* no oostore, most x86 cpus => do no thing, just a gcc do_not_cache barrier*/
132
+/* no oostore, most x86 cpus => do nothing, just a gcc do_not_cache barrier*/
119 133
 #define membar_write()	asm volatile ("" : : : "memory")
120 134
 #endif
121 135
 
... ...
@@ -177,8 +191,10 @@ inline static long atomic_dec_and_test(atomic_t* var)
177 191
 
178 192
 #elif defined __CPU_mips2
179 193
 
194
+#define HAVE_ASM_INLINE_ATOMIC_OPS
195
+
180 196
 #ifdef NOSMP
181
-#define membar()
197
+#define membar() asm volatile ("" : : : memory) /* gcc do not cache barrier*/
182 198
 #define membar_read()  membar()
183 199
 #define membar_write() membar()
184 200
 #else
... ...
@@ -262,34 +278,32 @@ ATOMIC_FUNC_DECL_CT(dec_and_test, "subu %2, %1, %3", 1, long, (ret-1)==0 )
262 278
 ATOMIC_FUNC_DECL1(and, "and %2, %1, %3", void, /* no return */ )
263 279
 ATOMIC_FUNC_DECL1(or,  "or  %2, %1, %3", void,  /* no return */ )
264 280
 
265
-#else /* no known cpu */
281
+#endif /* __CPU_xxx  => no known cpu */
282
+
283
+
284
+#ifndef HAVE_ASM_INLINE_ATOMIC_OPS
266 285
 
267 286
 #include "locking.h"
268 287
 
269 288
 #define ATOMIC_USE_LOCK
270 289
 
271
-extern gen_lock_t* atomic_lock;
290
+extern gen_lock_t* _atomic_lock;
272 291
 
273 292
 
293
+#define atomic_lock    lock_get(_atomic_lock)
294
+#define atomic_unlock  lock_release(_atomic_lock)
274 295
 
275
-#ifdef NOSMP
276
-#define smp_atomic_lock
277
-#define smp_atomic_unlock
278
-#else
279
-#define smp_atomic_lock    lock_get(atomic_lock)
280
-#define smp_atomic_unlock  lock_release(atomic_lock)
281
-#endif
282 296
 
283 297
 /* memory barriers 
284 298
  *  not a known cpu -> fall back lock/unlock: safe but costly  (it should 
285 299
  *  include a memory barrier effect) */
286
-
287 300
 #define membar() \
288 301
 	do{\
289
-		smp_atomic_lock; \
290
-		smp_atomic_unlock; \
302
+		atomic_lock; \
303
+		atomic_unlock; \
291 304
 	} while(0)
292 305
 
306
+
293 307
 #define membar_write() membar()
294 308
 
295 309
 #define membar_read()  membar()
... ...
@@ -299,32 +313,32 @@ extern gen_lock_t* atomic_lock;
299 313
 
300 314
 #define atomic_inc(var) \
301 315
 	do{ \
302
-		smp_atomic_lock; \
316
+		atomic_lock; \
303 317
 		(var)->val++;\
304
-		smp_atomic_unlock;\
318
+		atomic_unlock;\
305 319
 	}while(0)
306 320
 
307 321
 
308 322
 #define atomic_dec(var) \
309 323
 	do{ \
310
-		smp_atomic_lock; \
324
+		atomic_lock; \
311 325
 		(var)->val--; \
312
-		smp_atomic_unlock; \
326
+		atomic_unlock; \
313 327
 	}while(0)
314 328
 
315 329
 
316 330
 #define atomic_and(var, i) \
317 331
 	do{ \
318
-		smp_atomic_lock; \
332
+		atomic_lock; \
319 333
 		(var)->val&=i; \
320
-		smp_atomic_unlock; \
334
+		atomic_unlock; \
321 335
 	}while(0)
322 336
 
323 337
 #define atomic_or(var, i) \
324 338
 	do{ \
325
-		smp_atomic_lock; \
339
+		atomic_lock; \
326 340
 		(var)->val|=i; \
327
-		smp_atomic_unlock; \
341
+		atomic_unlock; \
328 342
 	}while(0)
329 343
 
330 344
 
... ...
@@ -334,10 +348,10 @@ inline static long atomic_inc_and_test(atomic_t* var)
334 348
 {
335 349
 	long ret;
336 350
 	
337
-	smp_atomic_lock;
351
+	atomic_lock;
338 352
 	var->val++;
339 353
 	ret=var->val;
340
-	smp_atomic_unlock;
354
+	atomic_unlock;
341 355
 	
342 356
 	return (ret==0);
343 357
 }
... ...
@@ -348,15 +362,39 @@ inline static long atomic_dec_and_test(atomic_t* var)
348 362
 {
349 363
 	long ret;
350 364
 	
351
-	smp_atomic_lock;
365
+	atomic_lock;
352 366
 	var->val++;
353 367
 	ret=var->val;
354
-	smp_atomic_unlock;
368
+	atomic_unlock;
355 369
 	
356 370
 	return (ret==0);
357 371
 }
358 372
 
359 373
 
360
-#endif /* if __CPU_xx */
374
+/* memory barrier versions, the same as "normal" versions, except fot
375
+ * the set/get
376
+ * (the * atomic_lock/unlock part should act as a barrier)
377
+ */
378
+
379
+#define mb_atomic_set(v, i) \
380
+	do{ \
381
+		membar(); \
382
+		atomic_set(v, i); \
383
+	}while(0)
384
+
385
+#define mb_atomic_get(v) \
386
+	do{ \
387
+		membar(); \
388
+		atomic_get(v); \
389
+	}while(0)
390
+
391
+#define mb_atomic_inc(v)	atomic_inc(v)
392
+#define mb_atomic_dec(v)	atomic_dec(v)
393
+#define mb_atomic_or(v, m)	atomic_or(v, m)
394
+#define mb_atomic_and(v, m)	atomic_and(v, m)
395
+#define mb_atomic_inc_and_test(v)	atomic_inc_and_test(v)
396
+#define mb_atomic_dec_and_test(v)	atomic_dec_and_test(v)
397
+
398
+#endif /* if HAVE_ASM_INLINE_ATOMIC_OPS */
361 399
 
362 400
 #endif
... ...
@@ -1,7 +1,7 @@
1 1
 /*
2 2
  *
3 3
  *  simple atomic ops testing program
4
- *  (no paralles stuff)
4
+ *  (no paralel stuff, just see if the opcodes are "legal")
5 5
  * 
6 6
  *  Compile with: gcc -D__CPU_i386 -O3 on x86 machines and
7 7
  *                gcc -mips2 -O2 -D__CPU_mips2  on mips machines.
... ...
@@ -56,6 +56,15 @@
56 56
 #endif
57 57
 #include "locking.h"
58 58
 
59
+#ifdef HAVE_SCHED_YIELD
60
+#include <sched.h>
61
+#else
62
+#include <unistd.h>
63
+	/* fake sched_yield */
64
+#ifndef sched_yield()
65
+#define sched_yield()	sleep(0)
66
+#endif
67
+#endif
59 68
 
60 69
 
61 70