1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include "cpp_mutex.h"
6 
7 using namespace AOS;
8 
9 /**
10  * This function will create a mutex
11  * @param[in] name   name of the mutex
12  * @return the operation status, RHINO_SUCCESS is OK, others is error
13  */
create(const name_t * name)14 kstat_t Mutex::create(const name_t *name)
15 {
16     return krhino_mutex_create(&_mutex_def, name);
17 }
18 
19 /**
20  * This function will delete a mutex
21  * @param[in] NULL
22  * @return the operation status, RHINO_SUCCESS is OK, others is error
23  */
destroy(void)24 kstat_t Mutex::destroy(void)
25 {
26     return krhino_mutex_del(&_mutex_def);
27 }
28 
29 /**
30  * This function will lock mutex
31  * @param[in]  millisec  millisec to be wait for before lock
32  * @return  the operation status, RHINO_SUCCESS is OK, others is error
33  */
lock(uint32_t millisec)34 kstat_t Mutex::lock(uint32_t millisec)
35 {
36     tick_t ticks = 0;
37 
38     if (millisec == 0) {
39         ticks = RHINO_NO_WAIT;
40     } else if (millisec == Mutex_WAIT_FOREVER){
41         ticks = RHINO_WAIT_FOREVER;
42     } else {
43         ticks = krhino_ms_to_ticks(millisec);
44     }
45 
46     return krhino_mutex_lock(&_mutex_def, ticks);
47 }
48 
49 /**
50  * This function will unlock a mutex
51  * @param[in] NULL
52  * @return  the operation status, RHINO_SUCCESS is OK, others is error
53  */
unlock(void)54 kstat_t Mutex::unlock(void)
55 {
56     return krhino_mutex_unlock(&_mutex_def);
57 }
58 
59 /**
60  * This function will get a Mutex struct pointer
61  * @param[in]  none
62  * @return  Mutex struct pointer
63  */
self(void)64 kmutex_t *Mutex::self(void)
65 {
66     return &_mutex_def;
67 }
68