Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate from OSSpinLock to os_unfair_lock()?

As of macOS 10.12, OSSpinLock has been deprecated. The XCode error messages urge me to use os_unfair_lock_unlock() instead.

As a legacy of some open source stuff I'm relying on, I'm using RegexKitLite from 2010.

How can I convert the spin lock type? Simple unlocking and locking I can manage, but these comparisons are giving me headache:

if(rkl_cacheSpinLock != (OSSpinLock)0) { ... }

rkl_cacheSpinLock is of type os_unfair_lock and has been initialized. OSSpinLock seems to be of type int, so this if-statement obviously won't work.

Could anyone point me towards the right way of approaching this? I'm not too familiar with C, and don't really understand arithmetics of pointers.

EDIT

After learning a bit about C, I came to understood typecasting. I came up with a solution that seems to work. My understanding of OS functionality on this level is nonexistent. The os_unfair_lock is not too well documented for dummies, but it looks like I didn't break anything.

if (rkl_cacheSpinLock._os_unfair_lock_opaque != 0) { ... }

like image 895
Tritonal Avatar asked Aug 28 '19 16:08

Tritonal


1 Answers

Seems like no one ever took the time to answer your question so here goes:

#include <os/lock.h>

void foo() 
{
    os_unfair_lock lock = OS_UNFAIR_LOCK_INIT;
    os_unfair_lock_lock(&lock);

    /* Your critical section here */

    os_unfair_lock_unlock(&lock);
}

Please refer to the documentation for more details on restrictions regarding where you should lock and unlock (tldr: it has to be same thread that locks and unlocks).

like image 89
Mr_Pouet Avatar answered Sep 20 '22 13:09

Mr_Pouet