1

我正在使用 NXP 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 指令集版本,可在此处或第 918 页的用户手册中找到该版本。

但是,通过它,我找不到像测试和设置指令这样的东西。我可能只是忽略了它。

理想情况下,我想要这样的东西:

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

我无法重现并发访问问题(假设这是我遇到的并发问题),所以我不确定这是否有效。

4

1 回答 1

1

ARM 对原子操作使用“加载链接/存储独占”范例。有关详细信息,请参阅此问题和您链接的用户手册的第 39.2.4.8 节。

[更新]

根据@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 具有复杂的内存模型。

于 2014-09-02T14:56:48.050 回答