Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#5372 closed Bugs (fixed)

spinlock fails on iPad2

Reported by: Trantec@… Owned by: Peter Dimov
Milestone: To Be Determined Component: smart_ptr
Version: Boost 1.41.0 Severity: Showstopper
Keywords: Cc:

Description

Hi,

I found an issue with the spinlock in spinlock_gcc.hpp.

In our Project and in my testcode it doesn't work with multithreading on the iPad2 multicored arm processor.

Here is the code which you can use to verify it:


void doSomething(boost::weak_ptr<int> weakp, int* number) {

int i = 0; while(++i<10000) {

{

/* That works: the result is a number that equals to 40000*/ /*sync_fetch_and_add(number, 1);*/

/* Here the result is a number much smaller then 40000*/ /*boost::detail::atomic_increment(number);*/

/* This crashes after some time since the use_count is becoming zero*/ /*boost::shared_ptr<int> p=weakp.lock();*/

}

}

}*/

int main(int argc, char *argv[]) {

int iNumber = 0; boost::shared_ptr<int> p(new int(0)); boost::shared_array<boost::thread> t; t.reset(new boost::thread[4]); for(size_t i=0; i<4; i++) {

t[i] = boost::thread(boost::bind(&doSomething, p, &iNumber));

}

t[0].join(); t[1].join(); t[2].join(); t[3].join();

}


If you got any questions, don't hasitate writing an email to me :)

Greetings,

Sascha

Change History (18)

comment:1 by Trantec@…, 12 years ago

Component: Nonesmart_ptr
Owner: set to Peter Dimov
Version: Boost 1.45.0Boost 1.41.0

comment:2 by Peter Dimov, 12 years ago

Not good. So the iPad2 version of GCC supports the sync instrinsics? We'll need to enable their use in shared_ptr then - previous GCCs for iPhone didn't have them. How can we detect that the sync intrinsics work?

comment:3 by Peter Dimov, 12 years ago

Severity: ProblemShowstopper

comment:4 by Peter Dimov, 12 years ago

What is your GCC version, and does it define the predefined macro __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4?

comment:5 by anonymous, 12 years ago

Sorry for the delayed answer.

I think the marko "_GLIBCXX_ATOMIC_BUILTINS" has to be defined. At least in the settings of my project it is. (see link: http://iphone.galloway.me.uk/2010/05/iphone-sdk-bug-hunting-gcc-atomic-builtins/).

I'm using Gcc 4.2, it seams this is the first version to support atomic builtins...

comment:6 by Peter Dimov, 12 years ago

Thanks, this is useful info. Can you please run the following tests for me (each of those is independent of the others):

  1. Modify spinlock_gcc_arm.hpp to contain memory barrier instructions as follows:
  • change "swp %0, %1, [%2]": in try_lock to "swp %0, %1, [%2]\n\tdmb":
  • change __asm__ __volatile__( "" ::: "memory" ); in unlock to __asm__ __volatile__( "dmb" ::: "memory" );

and see if this fixes things (this will need to be compiled in ARMv7 mode)?

  1. Modify spinlock.hpp to use spinlock_sync.hpp instead of spinlock_gcc_arm.hpp and see if this fixes things?
  1. Modify sp_counted_base.hpp to use sp_counted_base_sync.hpp instead of sp_counted_base_spin.hpp and see if this fixes shared_ptr? (In this case the spinlock will be still broken but we'll apply 1 or 2 to it as well.)

comment:7 by anonymous, 11 years ago

What came of this?

comment:8 by Peter Dimov, 11 years ago

Nothing, I'm afraid. I'm still awaiting answers to my questions above. I don't have access to an iPad 2 (or another dual core ARM device) development environment myself.

comment:9 by anonymous, 11 years ago

We have verified that there is a genuine spinlock bug on both the iPad2 and Motorola Atrix. Our workaround was to use the pthread-based implementation. Once we get past our current shared-pointer-induced crisis, we will test your patch and report the results.

comment:10 by Phil Endecott, 11 years ago

As described in this thread:

http://thread.gmane.org/gmane.comp.lib.boost.devel/217094/focus=217107

I'm using a shared_ptr implementation based on boost.atomic; I've just posted that code in ticket #5625. This seems to be working reliably in a couple of iPad 2 apps, and should be much more performant than your current pthread workaround.

comment:11 by anonymous, 11 years ago

Please, someone get this man an iPad2! :)

comment:12 by anonymous, 11 years ago

I'm confused. I cannot reproduce this error on my iPad2. The testcode outputs the expected results. Tested with 1.46.1 & 1.47. What am I doing wrong?

comment:13 by marcus.alanen@…, 11 years ago

Hello,

we've also had crashing bugs on a multithreaded game with shared_ptrs on iPad2 - very mysterious of course, and not easily testable.

I tried adding dmb instructions as per suggestion 1 above by pdimov, and after some casual testing so far we haven't had any issues. However, this is of course no guarantee, but I hope this at least gives you something.

Best Regards,

Marcus Alanen

Tribeflame

comment:14 by Peter Dimov, 11 years ago

(In [75389]) Add ARM memory barriers. Refs #5372.

comment:15 by Peter Dimov, 11 years ago

Status: newassigned

comment:16 by anonymous, 11 years ago

instead of retaining the swp instruction (which is deprecated with ARMv7 also because of performance problems), wouldn't it be better to use the gcc builtins where available (resp. the corresponding ldrex/strex assembler instructions where not)?

comment:17 by Peter Dimov, 11 years ago

Resolution: fixed
Status: assignedclosed

(In [75753]) Merge [75389] to release. Fixes #5372.

comment:18 by shreyas.bme@…, 11 years ago

Hi,

I am getting following error for iPhone. I am using Boost 1_45_0

I will appreciate for your reply.

Following is error logs...from Xcode

ld: bad codegen, pointer diff in boost::signals::connection::connection(boost::signals::connection const&)to global weak symbol boost::detail::spinlock_pool<1>::pool_ for architecture armv7 collect2: ld returned 1 exit status Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/g++-4.2 failed with exit code 1

Note: See TracTickets for help on using tickets.