optee的同步机制总结

来源:互联网 发布:大数据 方便面 编辑:程序博客网 时间:2024/05/29 16:59
optee有三种同步机制分别是spin-lock/mutex/condvar
1:spin-lock举例如下:
spin-lock 是一个unsigned int类型的变量,总共有三个函数
| Function | Purpose |
|----------|---------|
| `cpu_spin_lock()` | Locks a spin-lock |
| `cpu_spin_trylock()` | Locks a spin-lock if unlocked and returns `0` else the spin-lock is unchanged and the function returns `!0`|
| `cpu_spin_unlock()` | Unlocks a spin-lock |


static unsigned int rng_lock = SPINLOCK_UNLOCK;
    cpu_spin_lock(&rng_lock);
    ret = random.byte[pos];
    pos = (pos + 1) % 8;
    cpu_spin_unlock(&rng_lock);

2:mutex举例如下:
mutex 是一个struct mutex或者mutex_init()来初始化一个mutex变量
相关函数有四个
可以使用MUTEX_INITIALIZER和mutex_init()来初始化一个mutex变量
| Function | Purpose |
|----------|---------|
|`mutex_lock()` | Locks a mutex. If the mutex is unlocked this is a fast operation, else the function issues an RPC to wait in normal world. |
| `mutex_unlock()` | Unlocks a mutex. If there is no waiters this is a fast operation, else the function issues an RPC to wake up a waiter in normal world. |
| `mutex_trylock()` | Locks a mutex if unlocked and returns `true` else the mutex is unchanged and the function returns `false`. |
| `mutex_destroy()` | Asserts that the mutex is unlocked and there is no waiters, after this the memory used by the mutex can be freed. |
3:condvar举例如下:
condvar 用struct condvar来表示
可以使用CONDVAR_INITIALIZER和condvar_init()来初始化一个condvar变量,相关函数有三个

| Function | Purpose |
|----------|---------|
| `condvar_wait()` | Atomically unlocks the supplied mutex and waits in normal world via an RPC for the condition variable to be signaled, when the function returns the mutex is locked again. |
| `condvar_signal()` | Wakes up one waiter of the condition variable (waiting in `condvar_wait()`) |
| `condvar_broadcast()` | Wake up all waiters of the condition variable. |
原创粉丝点击