xref: /nrf52832-nimble/rt-thread/components/cplusplus/Mutex.h (revision 167494296f0543431a51b6b1b83e957045294e05)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2016/10/1      Bernard      The first version
9  */
10 
11 #pragma once
12 
13 #include <stdint.h>
14 #include <rtthread.h>
15 
16 namespace rtthread {
17 
18 /** The Mutex class is used to synchronise the execution of threads.
19  This is for example used to protect access to a shared resource.
20 */
21 class Mutex {
22 public:
23     /** Create and Initialize a Mutex object */
24     Mutex(const char* name = "mutex");
25     ~Mutex();
26 
27     /** Wait until a Mutex becomes available.
28       @param   millisec  timeout value or 0 in case of no time-out. (default: WaitForever)
29       @return  true if the mutex was acquired, false otherwise.
30      */
31     bool lock(int32_t millisec = -1);
32 
33     /** Try to lock the mutex, and return immediately
34       @return  true if the mutex was acquired, false otherwise.
35      */
36     bool trylock();
37 
38     /** Unlock the mutex that has previously been locked by the same thread
39      */
40     void unlock();
41 
42 private:
43     struct rt_mutex mID;
44 };
45 
46 }
47