Mistake on this page?
Report an issue in GitHub or email us
Semaphore.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2012 ARM Limited
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 #ifndef SEMAPHORE_H
23 #define SEMAPHORE_H
24 
25 #include <stdint.h>
26 #include "cmsis_os2.h"
27 #include "mbed_rtos1_types.h"
28 #include "mbed_rtos_storage.h"
29 #include "platform/mbed_toolchain.h"
30 #include "platform/NonCopyable.h"
31 
32 namespace rtos {
33 /** \addtogroup rtos */
34 /** @{*/
35 /**
36  * \defgroup rtos_Semaphore Semaphore class
37  * @{
38  */
39 
40 /** The Semaphore class is used to manage and protect access to a set of shared resources.
41  *
42  * @note
43  * Memory considerations: The semaphore control structures will be created on current thread's stack, both for the mbed OS
44  * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
45  */
46 class Semaphore : private mbed::NonCopyable<Semaphore> {
47 public:
48  /** Create and Initialize a Semaphore object used for managing resources.
49  @param count number of available resources; maximum index value is (count-1). (default: 0).
50 
51  @note You cannot call this function from ISR context.
52  */
53  Semaphore(int32_t count = 0);
54 
55  /** Create and Initialize a Semaphore object used for managing resources.
56  @param count number of available resources
57  @param max_count maximum number of available resources
58 
59  @note You cannot call this function from ISR context.
60  */
61  Semaphore(int32_t count, uint16_t max_count);
62 
63  /** Wait until a Semaphore resource becomes available.
64 
65  @deprecated Do not use this function. This function has been replaced with acquire(), try_acquire() and try_acquire_for() functions.
66 
67  @param millisec timeout value. (default: osWaitForever).
68  @return number of available tokens, before taking one; or -1 in case of incorrect parameters
69 
70  @note You may call this function from ISR context if the millisec parameter is set to 0.
71  */
72  MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with acquire, try_acquire() and try_acquire_for() functions")
73  int32_t wait(uint32_t millisec = osWaitForever);
74 
75  /** Wait until a Semaphore resource becomes available.
76 
77  @deprecated Do not use this function. This function has been replaced with try_acquire_until().
78 
79  @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
80  @return number of available tokens, before taking one; or -1 in case of incorrect parameters
81  @note the underlying RTOS may have a limit to the maximum wait time
82  due to internal 32-bit computations, but this is guaranteed to work if the
83  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
84  the acquire attempt will time out earlier than specified.
85 
86  @note You cannot call this function from ISR context.
87  */
88  MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with try_acquire_until()")
89  int32_t wait_until(uint64_t millisec);
90 
91  /** Wait until a Semaphore resource becomes available.
92 
93  @note You cannot call this function from ISR context.
94  */
95  void acquire();
96 
97  /** Try to acquire a Semaphore resource, and return immediately
98  @return true if a resource was acquired, false otherwise.
99  @note equivalent to try_acquire_for(0)
100 
101  @note You may call this function from ISR context.
102  */
103  bool try_acquire();
104 
105  /** Wait until a Semaphore resource becomes available.
106  @param millisec timeout value.
107  @return true if a resource was acquired, false otherwise.
108 
109  @note You may call this function from ISR context if the millisec parameter is set to 0.
110  */
111  bool try_acquire_for(uint32_t millisec);
112 
113  /** Wait until a Semaphore resource becomes available.
114  @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
115  @return true if a resource was acquired, false otherwise.
116  @note the underlying RTOS may have a limit to the maximum wait time
117  due to internal 32-bit computations, but this is guaranteed to work if the
118  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
119  the acquire attempt will time out earlier than specified.
120 
121  @note You cannot call this function from ISR context.
122  */
123  bool try_acquire_until(uint64_t millisec);
124 
125  /** Release a Semaphore resource that was obtain with Semaphore::acquire.
126  @return status code that indicates the execution status of the function:
127  @a osOK the token has been correctly released.
128  @a osErrorResource the maximum token count has been reached.
129  @a osErrorParameter internal error.
130 
131  @note You may call this function from ISR context.
132  */
133  osStatus release(void);
134 
135  /** Semaphore destructor
136  *
137  * @note You cannot call this function from ISR context.
138  */
139  ~Semaphore();
140 
141 private:
142  void constructor(int32_t count, uint16_t max_count);
143 
144  int32_t _wait(uint32_t millisec);
145 
146  osSemaphoreId_t _id;
147  mbed_rtos_storage_semaphore_t _obj_mem;
148 };
149 /** @}*/
150 /** @}*/
151 }
152 #endif
153 
154 
int32_t wait_until(uint64_t millisec)
Wait until a Semaphore resource becomes available.
bool try_acquire_until(uint64_t millisec)
Wait until a Semaphore resource becomes available.
bool try_acquire_for(uint32_t millisec)
Wait until a Semaphore resource becomes available.
osStatus release(void)
Release a Semaphore resource that was obtain with Semaphore::acquire.
The Semaphore class is used to manage and protect access to a set of shared resources.
Definition: Semaphore.h:46
bool try_acquire()
Try to acquire a Semaphore resource, and return immediately.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:168
int32_t wait(uint32_t millisec=osWaitForever)
Wait until a Semaphore resource becomes available.
Semaphore(int32_t count=0)
Create and Initialize a Semaphore object used for managing resources.
~Semaphore()
Semaphore destructor.
void acquire()
Wait until a Semaphore resource becomes available.
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.