LPC1788微控制器的原子测试和设置

时间:2014-09-02 14:47:04

标签: c arm microcontroller thumb lpc

我正在使用恩智浦LPC1788微控制器,并且我在C中开发了一个多线程应用程序。在我的应用程序的一部分中,我定义了一个自定义链表数据结构。由于同时访问特定列表,我之前遇到了我的程序问题,我似乎通过实现"锁定获取"方法和"锁定释放"列表的方法,线程可以在访问列表之前调用它。

我是通过添加一个' sema'数据成员到列表结构:

typedef struct linked_list
{
  list_node_t *head;
  list_node_t *tail;
  uint32_t len;
  NODE_ITEM_TYPE_T itemType;
  uint32_t itemSize;
  uint8_t sema;
} linked_list_t;

我的"锁定获取"方法如下:

void LIST_AcquireLock(linked_list_t *list)
{
  while(list->sema);
  list->sema = 1;
}

我的"锁定释放"方法如下:

void LIST_ReleaseLock(linked_list_t *list)
{
  list->sema = 0;
}

一般情况下,这似乎工作正常,因为我的应用程序涉及每秒数千次添加和删除项目到列表,我没有注意到任何与并发访问相关的错误。

然而,为了更加确信这是有效的,我想知道是否有任何方法来实现测试和设置方法。 LPC1788依赖于特定于Cortex-M3微控制器的Thumb指令集版本,可以在{9}}或{9}}页面上找到。{/ p> 但是,仔细研究一下,我找不到像测试和设置指令那样的东西。我可能会忽视它。

理想情况下,我希望有这样的事情:

void LIST_AcquireLock(linked_list_t *list)
{
  do{
    while(list->sema);
  } while(TestAndSet(list->sema));
}

修改

根据Nemo的回答,我尝试了以下方法:

void LIST_AcquireLock(linked_list_t *list)
{
  // Wait until lock seems free.
  while(list->sema);

  // Make sure lock is actually free.
  do {

    // If the semaphore is locked, we continue.
    // OTHERWISE we try to lock it ourselves.
    if(__LDREXB(&(list->sema))) continue;

    // If __STREXB returns 1, then another thread might have accessed that
    // memory location and we can't be sure the lock operation is atomic,
    // so try the locking procedure again.
  } while(__STREXB(1, &(list->sema)));
}

如果它有用,这是相应的汇编代码:

LIST_AcquireLock:
??LIST_AcquireLock_0:
       0x56de: 0x7d01         LDRB      R1, [R0, #0x14]
       0x56e0: 0x2900         CMP       R1, #0
       0x56e2: 0xd1fc         BNE.N     ??LIST_AcquireLock_0    ; 0x56de
??LIST_AcquireLock_1:
       0x56e4: 0xf110 0x0114  ADDS.W    R1, R0, #20             ; 0x14
       0x56e8: 0xe8d1 0x1f4f  LDREXB    R1, [R1]
       0x56ec: 0xb2c9         UXTB      R1, R1
       0x56ee: 0x2900         CMP       R1, #0
??LIST_AcquireLock_2:
       0x56f0: 0xf110 0x0114  ADDS.W    R1, R0, #20             ; 0x14
       0x56f4: 0x2201         MOVS      R2, #1
       0x56f6: 0xe8c1 0x2f43  STREXB    R3, R2, [R1]
       0x56fa: 0x2b00         CMP       R3, #0
       0x56fc: 0xd1f2         BNE.N     ??LIST_AcquireLock_1    ; 0x56e4
       0x56fe: 0x4770         BX        LR

我在复制并发访问问题时遇到了问题(假设这是我遇到的并发问题)所以我不确定这是否有效。

1 个答案:

答案 0 :(得分:1)

ARM使用"加载链接/存储专用"原子操作的范例。有关详细信息,请参阅您关联的this questionSection 39.2.4.8 of the user manual

[更新]

根据提供的the code in the link @HansPassant,我建议你的日常工作稍作修改:

void LIST_AcquireLock(linked_list_t *list)
{
  // Wait until lock seems free.
  //while(list->sema); // unnecessary

  // Make sure lock is actually free.
  do {

    // If the semaphore is locked, we continue.
    // OTHERWISE we try to lock it ourselves.
    if(__LDREXB(&(list->sema))) continue;

    // If __STREXB returns 1, then another thread might have accessed that
    // memory location and we can't be sure the lock operation is atomic,
    // so try the locking procedure again.
  } while(__STREXB(1, &(list->sema)));

  // Ensure CPU does not reorder any memory accesses across lock acquisition.
  __DMB();
}

__DMB()可能与非常简单的ARM内核无关,但在更复杂的内核上肯定需要它。现代CPU具有复杂的内存模型。