How to migrate from OSSpinLock to os_unfair_lock()?

不想你离开。 提交于 2020-05-29 11:22:53

问题


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) { ... }


回答1:


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).



来源:https://stackoverflow.com/questions/57696275/how-to-migrate-from-osspinlock-to-os-unfair-lock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!