Mistake on this page?
Report an issue in GitHub or email us
Semaphore.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 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 "rtos/mbed_rtos_types.h"
27 #include "rtos/mbed_rtos1_types.h"
28 #include "rtos/mbed_rtos_storage.h"
29 #include "platform/mbed_toolchain.h"
30 #include "platform/NonCopyable.h"
31 
32 namespace rtos {
33 /** \addtogroup rtos-public-api */
34 /** @{*/
35 
36 /**
37  * \defgroup rtos_Semaphore Semaphore class
38  * @{
39  */
40 
41 /** The Semaphore class is used to manage and protect access to a set of shared resources.
42  *
43  * @note
44  * Memory considerations: The semaphore control structures will be created on current thread's stack, both for the mbed OS
45  * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
46  */
47 class Semaphore : private mbed::NonCopyable<Semaphore> {
48 public:
49  /** Create and Initialize a Semaphore object used for managing resources.
50  @param count number of available resources; maximum index value is (count-1). (default: 0).
51 
52  @note You cannot call this function from ISR context.
53  */
54  Semaphore(int32_t count = 0);
55 
56  /** Create and Initialize a Semaphore object used for managing resources.
57  @param count number of available resources
58  @param max_count maximum number of available resources
59 
60  @note You cannot call this function from ISR context.
61  */
62  Semaphore(int32_t count, uint16_t max_count);
63 
64  /** Wait until a Semaphore resource becomes available.
65 
66  @deprecated Do not use this function. This function has been replaced with acquire(), try_acquire() and try_acquire_for() functions.
67 
68  @param millisec timeout value. (default: osWaitForever).
69  @return number of available tokens, before taking one; or -1 in case of incorrect parameters
70 
71  @note You may call this function from ISR context if the millisec parameter is set to 0.
72  */
73  MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with acquire, try_acquire() and try_acquire_for() functions")
74  int32_t wait(uint32_t millisec = osWaitForever);
75 
76  /** Wait until a Semaphore resource becomes available.
77 
78  @deprecated Do not use this function. This function has been replaced with try_acquire_until().
79 
80  @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
81  @return number of available tokens, before taking one; or -1 in case of incorrect parameters
82  @note the underlying RTOS may have a limit to the maximum wait time
83  due to internal 32-bit computations, but this is guaranteed to work if the
84  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
85  the acquire attempt will time out earlier than specified.
86 
87  @note You cannot call this function from ISR context.
88  */
89  MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with try_acquire_until()")
90  int32_t wait_until(uint64_t millisec);
91 
92  /** Wait until a Semaphore resource becomes available.
93 
94  @note You cannot call this function from ISR context.
95  */
96  void acquire();
97 
98  /** Try to acquire a Semaphore resource, and return immediately
99  @return true if a resource was acquired, false otherwise.
100  @note equivalent to try_acquire_for(0)
101 
102  @note You may call this function from ISR context.
103  */
104  bool try_acquire();
105 
106  /** Wait until a Semaphore resource becomes available.
107  @param millisec timeout value.
108  @return true if a resource was acquired, false otherwise.
109 
110  @note You may call this function from ISR context if the millisec parameter is set to 0.
111  */
112  bool try_acquire_for(uint32_t millisec);
113 
114  /** Wait until a Semaphore resource becomes available.
115  @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
116  @return true if a resource was acquired, false otherwise.
117  @note the underlying RTOS may have a limit to the maximum wait time
118  due to internal 32-bit computations, but this is guaranteed to work if the
119  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
120  the acquire attempt will time out earlier than specified.
121 
122  @note You cannot call this function from ISR context.
123  */
124  bool try_acquire_until(uint64_t millisec);
125 
126  /** Release a Semaphore resource that was obtain with Semaphore::acquire.
127  @return status code that indicates the execution status of the function:
128  @a osOK the token has been correctly released.
129  @a osErrorResource the maximum token count has been reached.
130  @a osErrorParameter internal error.
131 
132  @note You may call this function from ISR context.
133  */
134  osStatus release(void);
135 
136  /** Semaphore destructor
137  *
138  * @note You cannot call this function from ISR context.
139  */
140  ~Semaphore();
141 
142 private:
143  void constructor(int32_t count, uint16_t max_count);
144 
145 #if MBED_CONF_RTOS_PRESENT
146  int32_t _wait(uint32_t millisec);
147 
148  osSemaphoreId_t _id;
150 #else
151  static bool semaphore_available(void *);
152  int32_t _count;
153  uint16_t _max_count;
154 #endif
155 };
156 /** @}*/
157 /** @}*/
158 }
159 #endif
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.
Semaphore Control Block.
Definition: rtx_os.h:198
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:47
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:169
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.
Definition: TaskBase.h:25
~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.