uclinux-2008R1-RC8(bf561)到VDSP5的移植(29):spinlock

來源:互聯網
上載者:User

spinlock是用於線程間同步的自旋鎖,由於我們希望使用BF561的兩個核,因此它就顯得極為重要。

1 定義

在核心中聲明一個spinlock可以使用DEFINE_SPINLOCK這個宏定義,它的定義在include/linux/spinlock_types.h中:

#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)

其中spinlock_t是在include/linux/spinlock_types.h中定義的一個結構體:

typedef struct {
 raw_spinlock_t raw_lock;
#if defined(CONFIG_PREEMPT) && defined(CONFIG_SMP)
 unsigned int break_lock;
#endif
#ifdef CONFIG_DEBUG_SPINLOCK
 unsigned int magic, owner_cpu;
 void *owner;
#endif
#ifdef CONFIG_DEBUG_LOCK_ALLOC
 struct lockdep_map dep_map;
#endif
} spinlock_t;

因為我們只定義了CONFIG_SMP,因此這個結構體實際就是:

typedef struct {
 raw_spinlock_t raw_lock;
} spinlock_t;

raw_spinlock_t的定義在include/asm/spinlock_types.h中:

typedef struct {
 volatile unsigned int lock;
} raw_spinlock_t;

在此之前,我們只是簡單地將其設定為volatile unsigned int的類型,而現在,我們則希望藉助於VDSP的庫進行核間同步,因此將其定義改為:

typedef struct {
 testset_t lock;
} raw_spinlock_t;

因為testset_t自動就是volatile類型的,在此就不用聲明了。

2 VDSP對核間同步的支援

以下內容來自VDSP的協助:

Synchronization Functions
VisualDSP++ 5.0 provides functionality for synchronization. There are two compiler intrinsics (built-in functions) and three locking routines.
The compiler intrinsics are:
#include <ccblkfn.h>
int testset(char *);
void untestset(char *);
The testset() intrinsic generates a native TESTSET instruction, which can perform atomic updates on a memory location. The intrinsic returns the result of the CC flag produced by the TESTSET instruction. Refer to the instruction set reference for details.
The untestset() intrinsic clears the memory location set by the
testset() intrinsic. This intrinsic is recommended in place of a normal memory write because the untestset() intrinsic acts as a stronger barrier to code movement during optimization.
The three locking routines are:
#include <ccblkfn.h>
void adi_acquire_lock(testset_t *);
int adi_try_lock(testset_t *);
void adi_release_lock(testset_t *);
The adi_acquire_lock() routine repeatedly attempts to claim the lock by issuing testset() until successful, whereupon it returns to the caller. In contrast, the adi_try_lock() routine makes a single attempt—if it successfully claims the lock, it returns nonzero, otherwise it returns zero.
The adi_release_lock() routine releases the lock obtained by either adi_acquire_lock() or adi_try_lock(). It assumes that the lock was already claimed and makes no attempt to verify that its caller is in fact the current owner of the lock. None of these intrinsics or functions disable interrupts—that is left to the caller’s discretion.

3 spin_lock

spin_lock的定義在include/linux/spinlock.h中:

#define spin_lock(lock)  _spin_lock(lock)

_spin_lock的定義在include/linux/spinlock_api_smp.h中:

void __lockfunc _spin_lock(spinlock_t *lock)  __acquires(lock);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.