Merge: mongodb: Fixed failing test for `aggregate` method.
[nit.git] / lib / pthreads / pthreads.nit
index 13b9e95..91ebb94 100644 (file)
@@ -38,17 +38,8 @@ in "C" `{
        // TODO protect with: #ifdef WITH_LIBGC
        // We might have to add the next line to gc_chooser.c too, especially
        // if we get an error like "thread not registered with GC".
-       #ifdef __APPLE__
-               #include "TargetConditionals.h"
-               #if TARGET_OS_IPHONE == 1
-                       #define IOS
-               #endif
-       #endif
-
-       #if !defined(__ANDROID__) && !defined(IOS)
-               #define GC_THREADS
-               #include <gc.h>
-       #endif
+       #define GC_THREADS
+       #include <gc.h>
 `}
 
 redef class Sys
@@ -90,6 +81,61 @@ redef class Sys
        end
 end
 
+
+# An atomic Int
+extern class AtomicInt in "C" `{ int* `}
+       new(i: Int)`{
+               int* v = malloc(sizeof(int));
+               return v;
+       `}
+
+       # Get the value and increment it by `i`
+       fun get_and_increment_by(i: Int): Int `{
+               return __sync_fetch_and_add(self, i);
+       `}
+
+       # Get the value and decrement it by `i`
+       fun get_and_decrement_by(i: Int): Int `{
+               return __sync_fetch_and_sub(self, i);
+       `}
+
+       # Get the value and increment it
+       fun get_and_increment: Int `{
+               return __sync_fetch_and_add(self, 1);
+       `}
+
+       # Get the value and decrement it
+       fun get_and_decrement: Int `{
+               return __sync_fetch_and_sub(self, 1);
+       `}
+
+       # Increment by `i` and get the new value
+       fun increment_by_and_get(i: Int): Int `{
+               return __sync_add_and_fetch(self, i);
+       `}
+
+       # Decrement by `i` and get the new value
+       fun decrement_by_and_get(i: Int): Int `{
+               return __sync_sub_and_fetch(self, i);
+       `}
+
+       # Increment the value and get the new one
+       fun increment_and_get: Int `{
+               return __sync_add_and_fetch(self, 1);
+       `}
+
+       # Decrement the value and get the new one
+       fun decrement_and_get: Int `{
+               return __sync_sub_and_fetch(self,1);
+       `}
+
+       # Get the current value
+       fun value: Int `{
+               return *self;
+       `}
+
+end
+
 private extern class NativePthread in "C" `{ pthread_t * `}
 
        new create(nit_thread: Thread) import Thread.main_intern `{