summaryrefslogtreecommitdiffstats
path: root/cpukit/score/cpu/arm/rtems/score/cpusmplock.h
blob: 46592f432b21dea8b19c1d0f5d36d2c2c0e810ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
 * @file
 *
 * @ingroup ScoreSMPLockARM
 *
 * @brief ARM SMP Lock Implementation
 */

/*
 * Copyright (c) 2013 embedded brains GmbH
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.com/license/LICENSE.
 */

#ifndef _RTEMS_SCORE_ARM_SMPLOCK_H
#define _RTEMS_SCORE_ARM_SMPLOCK_H

#include <rtems/score/cpu.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/**
 * @defgroup ScoreSMPLockARM ARM SMP Locks
 *
 * @ingroup ScoreSMPLock
 *
 * A ticket lock implementation is used.
 *
 * @{
 */

typedef struct {
  uint32_t next_ticket;
  uint32_t now_serving;
} CPU_SMP_lock_Control;

#define CPU_SMP_LOCK_INITIALIZER { 0, 0 }

static inline void _CPU_SMP_lock_Initialize( CPU_SMP_lock_Control *lock )
{
  lock->next_ticket = 0;
  lock->now_serving = 0;
}

static inline void _CPU_SMP_lock_Acquire( CPU_SMP_lock_Control *lock )
{
  uint32_t my_ticket;
  uint32_t next_ticket;
  uint32_t status;

  __asm__ volatile (
    "1: ldrex %[my_ticket], [%[next_ticket_addr]]\n"
    "add %[next_ticket], %[my_ticket], #1\n"
    "strex %[status], %[next_ticket], [%[next_ticket_addr]]\n"
    "teq %[status], #0\n"
    "bne 1b"
    : [my_ticket] "=&r" (my_ticket),
      [next_ticket] "=&r" (next_ticket),
      [status] "=&r" (status)
    : [next_ticket_addr] "r" (&lock->next_ticket)
    : "cc", "memory"
  );

  while ( my_ticket != lock->now_serving ) {
    _ARM_Wait_for_event();
  }

  _ARM_Data_memory_barrier();
}

static inline void _CPU_SMP_lock_Release( CPU_SMP_lock_Control *lock )
{
  _ARM_Data_memory_barrier();
  ++lock->now_serving;
  _ARM_Data_synchronization_barrier();
  _ARM_Send_event();
}

#define _CPU_SMP_lock_ISR_disable_and_acquire( lock, isr_cookie ) \
  do { \
    _CPU_ISR_Disable( isr_cookie ); \
    _CPU_SMP_lock_Acquire( lock ); \
  } while (0)

#define _CPU_SMP_lock_Release_and_ISR_enable( lock, isr_cookie ) \
  do { \
    _CPU_SMP_lock_Release( lock ); \
    _CPU_ISR_Enable( isr_cookie ); \
  } while (0)

/**@}*/

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* _RTEMS_SCORE_ARM_SMPLOCK_H */