Browse Source

cmThread.c:Added OSX specific atomic operations.

master
kevin 11 years ago
parent
commit
b82a0d0edd
1 changed files with 83 additions and 5 deletions
  1. 83
    5
      cmThread.c

+ 83
- 5
cmThread.c View File

@@ -10,7 +10,10 @@
10 10
 
11 11
 #include <pthread.h>
12 12
 #include <unistd.h>  // usleep
13
-//#include <atomic_ops.h>
13
+
14
+#ifdef OS_OSX
15
+#include <libkern/OSAtomic.h>
16
+#endif
14 17
 
15 18
 cmThreadH_t cmThreadNullHandle = {NULL};
16 19
 
@@ -1254,34 +1257,94 @@ bool cmTs1p1cIsValid( cmTs1p1cH_t h )
1254 1257
 
1255 1258
 
1256 1259
 bool     cmThIntCAS(   int*      addr, int      old, int      new )
1257
-{ return __sync_bool_compare_and_swap(addr,old,new); }
1260
+{
1261
+#ifdef OS_OSX
1262
+  int rv = OSAtomicCompareAndSwap32Barrier(old,new,addr);
1263
+  return rv;
1264
+#endif
1265
+
1266
+#ifdef OS_LINUX 
1267
+  return __sync_bool_compare_and_swap(addr,old,new); 
1268
+#endif
1269
+}
1258 1270
 
1259 1271
 bool     cmThUIntCAS(  unsigned* addr, unsigned old, unsigned new )
1260
-{ return  __sync_bool_compare_and_swap(addr,old,new); }
1272
+{ 
1273
+#ifdef OS_OSX
1274
+  return OSAtomicCompareAndSwap32Barrier((int)old,(int)new,(int*)addr);
1275
+#endif
1276
+
1277
+#ifdef OS_LINUX 
1278
+  return __sync_bool_compare_and_swap(addr,old,new); 
1279
+#endif
1280
+}
1261 1281
 
1262 1282
 bool     cmThFloatCAS( float*    addr, float    old, float    new )
1263
-{ return  __sync_bool_compare_and_swap((unsigned*)addr, *(unsigned*)(&old),*(unsigned*)(&new)); }
1283
+{ 
1284
+#ifdef OS_OSX
1285
+  return  OSAtomicCompareAndSwap32Barrier(*(int*)(&old),*(int*)(&new),(int*)addr ); 
1286
+#endif
1287
+
1288
+#ifdef OS_LINUX
1289
+  return  __sync_bool_compare_and_swap((unsigned*)addr, *(unsigned*)(&old),*(unsigned*)(&new)); 
1290
+#endif
1291
+}
1264 1292
 
1265 1293
 bool     cmThPtrCAS(   void*    addr, void*    old, void*    neww )
1266 1294
 {
1295
+#ifdef OS_OSX
1296
+  // REMOVE THIS HACK AND USE OSAtomicCompareAndSwapPtrBarrier() WHEN
1297
+  // A 64 BIT BUILD IS POSSIBLE ON OS-X.
1298
+  typedef struct 
1299
+  {
1300
+    union 
1301
+    {
1302
+      void* addr;
1303
+      int   val;
1304
+    } u;
1305
+  } s_t;
1306
+
1307
+  s_t ov,nv;
1308
+  
1309
+  ov.u.addr = old;
1310
+  nv.u.addr = neww;
1311
+
1312
+  int rv = OSAtomicCompareAndSwap32Barrier(ov.u.val,nv.u.val,(int*)addr);
1313
+  //int rv = OSAtomicCompareAndSwapPtrBarrier(old,neww,&addr);
1314
+  return rv;
1315
+#endif
1316
+
1317
+#ifdef OS_LINUX
1267 1318
 #ifdef OS_64
1268 1319
   return  __sync_bool_compare_and_swap((long long*)addr, (long long)old, (long long)neww); 
1269 1320
 #else
1270 1321
   return  __sync_bool_compare_and_swap((int*)addr,(int)old,(int)neww); 
1271 1322
 #endif
1323
+#endif
1272 1324
 }
1273 1325
 
1274 1326
 
1275 1327
 
1276 1328
 void     cmThIntIncr(  int*      addr, int      incr )
1277 1329
 {
1330
+#ifdef OS_OSX
1331
+  OSAtomicAdd32Barrier(incr,addr);
1332
+#endif
1333
+
1334
+#ifdef OS_LINUX
1278 1335
   // ... could also use __sync_add_and_fetch() ...
1279 1336
   __sync_fetch_and_add(addr,incr);
1337
+#endif
1280 1338
 }
1281 1339
 
1282 1340
 void     cmThUIntIncr( unsigned* addr, unsigned incr )
1283 1341
 {
1342
+#ifdef OS_OSX
1343
+  OSAtomicAdd32Barrier((int)incr,(int*)addr);
1344
+#endif
1345
+#ifdef OS_LINUX
1284 1346
   __sync_fetch_and_add(addr,incr);
1347
+#endif
1285 1348
 }
1286 1349
 
1287 1350
 void     cmThFloatIncr(float*    addr, float    incr )
@@ -1298,12 +1361,24 @@ void     cmThFloatIncr(float*    addr, float    incr )
1298 1361
 
1299 1362
 void     cmThIntDecr(  int*  addr, int      decr )
1300 1363
 {
1364
+#ifdef OS_OSX
1365
+  OSAtomicAdd32Barrier(-decr,addr);
1366
+#endif
1367
+
1368
+#ifdef OS_LINUX
1301 1369
   __sync_fetch_and_sub(addr,decr);
1370
+#endif
1302 1371
 }
1303 1372
 
1304 1373
 void     cmThUIntDecr( unsigned* addr, unsigned decr )
1305 1374
 {
1375
+#ifdef OS_OSX
1376
+  OSAtomicAdd32Barrier(-((int)decr),(int*)addr);
1377
+#endif
1378
+
1379
+#ifdef OS_LINUX
1306 1380
   __sync_fetch_and_sub(addr,decr);
1381
+#endif
1307 1382
 }
1308 1383
 
1309 1384
 void     cmThFloatDecr(float*    addr, float    decr )
@@ -1897,7 +1972,10 @@ bool _cmTsMp1cCb0(void* param)
1897 1972
 {
1898 1973
   _cmTsMp1cCbParam_t* p = (_cmTsMp1cCbParam_t*)param;
1899 1974
 
1900
-  p->val = __sync_fetch_and_add(&_cmTsMp1cVal,1);
1975
+  //p->val = __sync_fetch_and_add(&_cmTsMp1cVal,1);
1976
+
1977
+  cmThUIntIncr(&_cmTsMp1cVal,1);
1978
+  p->val = _cmTsMp1cVal;
1901 1979
 
1902 1980
   // send the msg
1903 1981
   if( cmTsMp1cEnqueueMsg( p->qH, p, sizeof(_cmTsMp1cCbParam_t)) == kOkThRC )

Loading…
Cancel
Save