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  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #ifndef SEMAPHORE_H
24 #define SEMAPHORE_H
25 
26 #include <stdint.h>
27 #include <chrono>
28 #include "rtos/mbed_rtos_types.h"
29 #include "rtos/internal/mbed_rtos1_types.h"
30 #include "rtos/internal/mbed_rtos_storage.h"
31 #include "rtos/Kernel.h"
32 #include "platform/mbed_toolchain.h"
33 #include "platform/NonCopyable.h"
34 
35 namespace rtos {
36 /** \addtogroup rtos-public-api */
37 /** @{*/
38 
39 /**
40  * \defgroup rtos_Semaphore Semaphore class
41  * @{
42  */
43 
44 /** The Semaphore class is used to manage and protect access to a set of shared resources.
45  *
46  * @note
47  * Memory considerations: The semaphore control structures will be created on current thread's stack, both for the mbed OS
48  * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
49  */
50 class Semaphore : private mbed::NonCopyable<Semaphore> {
51 public:
52  /** Create and Initialize a Semaphore object used for managing resources.
53  @param count number of available resources; maximum index value is (count-1). (default: 0).
54 
55  @note You cannot call this function from ISR context.
56  */
57  Semaphore(int32_t count = 0);
58 
59  /** Create and Initialize a Semaphore object used for managing resources.
60  @param count number of available resources
61  @param max_count maximum number of available resources
62 
63  @note You cannot call this function from ISR context.
64  */
65  Semaphore(int32_t count, uint16_t max_count);
66 
67  /** Wait until a Semaphore resource becomes available.
68 
69  @note You cannot call this function from ISR context.
70  */
71  void acquire();
72 
73  /** Try to acquire a Semaphore resource, and return immediately
74  @return true if a resource was acquired, false otherwise.
75  @note equivalent to try_acquire_for(0)
76 
77  @note You may call this function from ISR context.
78  */
79  bool try_acquire();
80 
81  /** Wait until a Semaphore resource becomes available.
82  @param millisec timeout value.
83  @return true if a resource was acquired, false otherwise.
84 
85  @note You may call this function from ISR context if the millisec parameter is set to 0.
86  @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
87  */
88  MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
89  bool try_acquire_for(uint32_t millisec);
90 
91  /** Wait until a Semaphore resource becomes available.
92  @param rel_time timeout value.
93  @return true if a resource was acquired, false otherwise.
94 
95  @note You may call this function from ISR context if the rel_time parameter is set to 0.
96  */
97  bool try_acquire_for(Kernel::Clock::duration_u32 rel_time);
98 
99  /** Wait until a Semaphore resource becomes available.
100  @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
101  @return true if a resource was acquired, false otherwise.
102  @note the underlying RTOS may have a limit to the maximum wait time
103  due to internal 32-bit computations, but this is guaranteed to work if the
104  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
105  the acquire attempt will time out earlier than specified.
106 
107  @note You cannot call this function from ISR context.
108  @deprecated Pass a chrono time_point, not an integer millisecond count. For example use
109  `Kernel::Clock::now() + 5s` rather than `Kernel::get_ms_count() + 5000`.
110  */
111  MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono time_point, not an integer millisecond count. For example use `Kernel::Clock::now() + 5s` rather than `Kernel::get_ms_count() + 5000`.")
112  bool try_acquire_until(uint64_t millisec);
113 
114  /** Wait until a Semaphore resource becomes available.
115  @param millisec absolute timeout time, referenced to Kernel::Clock
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(Kernel::Clock::time_point abs_time);
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;
149  mbed_rtos_storage_semaphore_t _obj_mem;
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
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:50
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:162
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.