Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If you are working on a native function or method, please take a moment to determine which of the following behaviors describe it:

1) Only called on AppKit thread

Use AWT_ASSERT_APPKIT_THREAD; (defined in ThreadUtilities.h)

2) Only NOT called on AppKit thread

Use AWT_ASSERT_NOT_APPKIT_THREAD; (defined in ThreadUtilities.h)

3) Called on any thread

Make sure any shared data (instance variables or global data) is accessed only as an atomic property, or behind a lock that will not block for an indefinite period of time.

...

Code Block
/System/Library/Frameworks/JavaVM.framework/Frameworks/JavaNativeFoundation.framework/Headers/JNFRunLoop.h

Native locks are evil

  • Using pthread_lock() or NSLock will almost always get you in trouble
    • These locks are not recursive like Java locks are
  • Using @synchronized in ObjC is slow
    • Since the lock storage is not in the object header itself (like it is in Java) a side-table must be consulted
  • If you need to serialize execution of a task in native, use a serial GCD dispatch_queue
    • They are quite cheap to use in the uncontended case
  • Avoid doing any work that will call back up into Java while holding a native lock or performing on a serial queue
    • It is very easy to deadlock with a Java semaphore and not realize it
      • These deadlocks are almost impossible to debug