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/NonCopyable.h"
30 
31 namespace rtos {
32 /** \addtogroup rtos */
33 /** @{*/
34 /**
35  * \defgroup rtos_Semaphore Semaphore class
36  * @{
37  */
38 
39 /** The Semaphore class is used to manage and protect access to a set of shared resources.
40  *
41  * @note
42  * Memory considerations: The semaphore control structures will be created on current thread's stack, both for the mbed OS
43  * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
44  */
45 class Semaphore : private mbed::NonCopyable<Semaphore> {
46 public:
47  /** Create and Initialize a Semaphore object used for managing resources.
48  @param count number of available resources; maximum index value is (count-1). (default: 0).
49 
50  @note You cannot call this function from ISR context.
51  */
52  Semaphore(int32_t count = 0);
53 
54  /** Create and Initialize a Semaphore object used for managing resources.
55  @param count number of available resources
56  @param max_count maximum number of available resources
57 
58  @note You cannot call this function from ISR context.
59  */
60  Semaphore(int32_t count, uint16_t max_count);
61 
62  /** Wait until a Semaphore resource becomes available.
63  @param millisec timeout value. (default: osWaitForever).
64  @return number of available tokens, before taking one; or -1 in case of incorrect parameters
65 
66  @note You may call this function from ISR context if the millisec parameter is set to 0.
67  */
68  int32_t wait(uint32_t millisec = osWaitForever);
69 
70  /** Wait until a Semaphore resource becomes available.
71  @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
72  @return number of available tokens, before taking one; or -1 in case of incorrect parameters
73  @note the underlying RTOS may have a limit to the maximum wait time
74  due to internal 32-bit computations, but this is guaranteed to work if the
75  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
76  the acquire attempt will time out earlier than specified.
77 
78  @note You cannot call this function from ISR context.
79  */
80  int32_t wait_until(uint64_t millisec);
81 
82  /** Release a Semaphore resource that was obtain with Semaphore::wait.
83  @return status code that indicates the execution status of the function:
84  @a osOK the token has been correctly released.
85  @a osErrorResource the maximum token count has been reached.
86  @a osErrorParameter internal error.
87 
88  @note You may call this function from ISR context.
89  */
90  osStatus release(void);
91 
92  /** Semaphore destructor
93  *
94  * @note You cannot call this function from ISR context.
95  */
96  ~Semaphore();
97 
98 private:
99  void constructor(int32_t count, uint16_t max_count);
100 
101  osSemaphoreId_t _id;
102  mbed_rtos_storage_semaphore_t _obj_mem;
103 };
104 /** @}*/
105 /** @}*/
106 }
107 #endif
108 
109 
int32_t wait_until(uint64_t millisec)
Wait until a Semaphore resource becomes available.
osStatus release(void)
Release a Semaphore resource that was obtain with Semaphore::wait.
The Semaphore class is used to manage and protect access to a set of shared resources.
Definition: Semaphore.h:45
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.
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.