Contiki 3.x
synchronization.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Eistec AB.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This file is part of the Mulle platform port of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Synchronization primitives for Cortex-M3/M4 processors.
36  * \author
37  * Joakim Gebart <joakim.gebart@eistec.se>
38  */
39 
40 #ifndef CORTEX_M_SYNCHRONIZATION_H_
41 #define CORTEX_M_SYNCHRONIZATION_H_
42 
43 #include <stdint.h>
44 #include "core_cmInstr.h"
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 /*
51  * See also:
52  * ARM Application Note 321: ARM Cortex-M Programming Guide to Memory Barrier Instructions
53  * 4.19. Semaphores and Mutual Exclusives (Mutex) - unicore and multicore
54  */
55 
56 /** Lock variable typedef */
57 typedef volatile uint8_t lock_t;
58 
59 /** Blocking access to lock variable */
60 void lock_acquire(lock_t *Lock_Variable);
61 
62 /** Non-blocking access to lock variable */
63 int lock_try_acquire(lock_t *Lock_Variable);
64 
65 /** Release a lock after having acquired it using lock_acquire or lock_try_acquire. */
66 void lock_release(lock_t *Lock_Variable);
67 
68 /** Safely increment a counter variable, without race condition issues regarding
69  * the read-modify-write sequence. */
70 static inline void exclusive_increment(volatile uint32_t* value) {
71  int status;
72  int tmp;
73  do {
74  /* Load exclusive */
75  tmp = __LDREXW(value);
76 
77  /* increment counter */
78  ++tmp;
79 
80  /* Try to write the new value */
81  status = __STREXW(tmp, value);
82  } while (status != 0); /* retry until load-store cycle was exclusive. */
83 }
84 
85 /** Safely decrement a counter variable, without race condition issues regarding
86  * the read-modify-write sequence. */
87 static inline void exclusive_decrement(volatile uint32_t* value) {
88  int status;
89  int tmp;
90  do {
91  /* Load exclusive */
92  tmp = __LDREXW(value);
93 
94  /* decrement counter */
95  --tmp;
96 
97  /* Try to write the new value */
98  status = __STREXW(tmp, value);
99  } while (status != 0); /* retry until load-store cycle was exclusive. */
100 }
101 
102 #ifdef __cplusplus
103 } /* extern "C" */
104 #endif
105 
106 #endif /* !defined(CORTEX_M_SYNCHRONIZATION_H_) */
int lock_try_acquire(lock_t *Lock_Variable)
Non-blocking access to lock variable.
void lock_release(lock_t *Lock_Variable)
Release a lock after having acquired it using lock_acquire or lock_try_acquire.
void lock_acquire(lock_t *Lock_Variable)
Blocking access to lock variable.
CMSIS Cortex-M Core Instruction Access Header File.
volatile uint8_t lock_t
Lock variable typedef.