gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: __sync_add_and_fetch in objc block for global variable on ARM


From: Stefan Bidigaray
Subject: Re: __sync_add_and_fetch in objc block for global variable on ARM
Date: Wed, 8 Apr 2015 08:08:39 -0400

The ARM platform does not do atomic operations. That's a limitation of the architecture, not the compiler.

Read this, it's an interesting discussion on the issue with some suggestions: http://stackoverflow.com/questions/11894059/atomic-operations-in-arm

On Apr 8, 2015 4:41 AM, "Alexey Perevalov" <address@hidden> wrote:
Hello community,

I faced with bug in multithread environment in objective C code which using dispatch_async and block,
__sync_add_and_fetch increments global variable. But in case of many..many threads> 5, after every
__sync_add_and_fetch got damaged


...
int32_t count = 0;

...

int
main(int argc, char *argv[])
{
   for (i = 1; i < 32; ++i) {
     ...
        char* name;
        asprintf(&name, "test.overcommit.%d", i);

        dispatch_queue_t queue = dispatch_queue_create(name, NULL);
        free(name);
        dispatch_set_target_queue(queue, dispatch_get_global_queue(0,
                    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)));

        /* async queue */
        dispatch_async(queue, ^{
           __sync_add_and_fetch(&count, 1); //<< Here count is corrupted in case of number of threads> ~5
           printf("count addr %p, value %d\n", &count, count);
           fflush(stdout);
        });

   }
   ...
   dispatch_main();
}

in case of count is local variable in scope of main function and has __block attribute, all is fine.

I'm using
clang version 3.3 (tags/RELEASE_33/final)
Target: armv7l-unknown-linux-gnueabi
Thread model: posix
libBlockRuntime 0.3
libdispatch for linux 1.2
CPU is ARMv7,
in disas I see dmb ish instruction, but I don't know is it enough.

I understand, my clang is out of date. Moving to new version could be painful )
Maybe somebody knows, was that bug fixed?

BR,
Alexey

_______________________________________________
Gnustep-dev mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/gnustep-dev

reply via email to

[Prev in Thread] Current Thread [Next in Thread]