欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

optee中mutex的实现方式

发布时间:2025/3/21 编程问答 34 豆豆
生活随笔 收集整理的这篇文章主要介绍了 optee中mutex的实现方式 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

快速链接:
.
👉👉👉 个人博客笔记导读目录(全部) 👈👈👈


相关推荐:
optee中spinlock的实现原理详解


说明: 在默认的情况下,本文讲述的是armv8 aarch64体系,optee 3.12.0代码

在optee中,mutex的实现方式是依赖REE测的completion完成量的,具体流程图如下所示:

相关代码:

static void __mutex_lock(struct mutex *m, const char *fname, int lineno) {assert_have_no_spinlock();assert(thread_get_id_may_fail() != THREAD_ID_INVALID);assert(thread_is_in_normal_mode());mutex_lock_check(m);while (true) {uint32_t old_itr_status;bool can_lock;struct wait_queue_elem wqe;/** If the mutex is locked we need to initialize the wqe* before releasing the spinlock to guarantee that we don't* miss the wakeup from mutex_unlock().** If the mutex is unlocked we don't need to use the wqe at* all.*/old_itr_status = cpu_spin_lock_xsave(&m->spin_lock);can_lock = !m->state;if (!can_lock) {wq_wait_init(&m->wq, &wqe, false /* wait_read */);} else {m->state = -1; /* write locked */}cpu_spin_unlock_xrestore(&m->spin_lock, old_itr_status);if (!can_lock) {/** Someone else is holding the lock, wait in normal* world for the lock to become available.*/wq_wait_final(&m->wq, &wqe, m, fname, lineno);} elsereturn;} } static void __mutex_unlock(struct mutex *m, const char *fname, int lineno) {uint32_t old_itr_status;assert_have_no_spinlock();assert(thread_get_id_may_fail() != THREAD_ID_INVALID);mutex_unlock_check(m);old_itr_status = cpu_spin_lock_xsave(&m->spin_lock);if (!m->state)panic();m->state = 0;cpu_spin_unlock_xrestore(&m->spin_lock, old_itr_status);wq_wake_next(&m->wq, m, fname, lineno); }

总结

以上是生活随笔为你收集整理的optee中mutex的实现方式的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。