瀏覽代碼

LibPthread: Implement pthread_mutex_trylock()

Andreas Kling 5 年之前
父節點
當前提交
babb726212
共有 1 個文件被更改,包括 9 次插入0 次删除
  1. 9 0
      Libraries/LibPthread/pthread.cpp

+ 9 - 0
Libraries/LibPthread/pthread.cpp

@@ -111,6 +111,15 @@ int pthread_mutex_lock(pthread_mutex_t* mutex)
     }
     }
 }
 }
 
 
+int pthread_mutex_trylock(pthread_mutex_t* mutex)
+{
+    auto* atomic = reinterpret_cast<Atomic<u32>*>(mutex);
+    u32 expected = false;
+    if (atomic->compare_exchange_strong(expected, true, AK::memory_order_acq_rel))
+        return 0;
+    return EBUSY;
+}
+
 int pthread_mutex_unlock(pthread_mutex_t* mutex)
 int pthread_mutex_unlock(pthread_mutex_t* mutex)
 {
 {
     auto* atomic = reinterpret_cast<Atomic<u32>*>(mutex);
     auto* atomic = reinterpret_cast<Atomic<u32>*>(mutex);