Implementation of connectivity abstraction feature Release v0.61
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / inc / umutex.h
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  ******************************************************************/
19 /**
20  * @file    umutex.h
21  * @brief   This file provides APIs related to mutex and semaphores
22  */
23
24 #ifndef __UMUTEX_H_
25 #define __UMUTEX_H_
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31
32 #include "cacommon.h"
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #endif /* __cplusplus */
38
39 typedef void *u_mutex;
40 typedef void *u_cond;
41
42 /**
43  * @fn  u_mutex_init
44  * @brief  Initializes the thread system for using other mutex related APIs
45  *
46  */
47 void u_mutex_init(void);
48
49 /**
50  * @fn  u_mutex_new
51  * @brief  Creates new mutex
52  *
53  * @return  Reference to newly created mutex, otherwise NULL.
54  *
55  * @see u_mutex_Init
56  */
57 u_mutex u_mutex_new(void);
58
59 /**
60  * @fn  u_mutex_lock
61  * @brief  Lock the mutex
62  *
63  * @param  mutex  The mutex to be locked
64  *
65  */
66 void u_mutex_lock(u_mutex mutex);
67
68 /**
69  * @fn  u_mutex_trylock
70  * @brief  Checks if the mutex can be locked
71  *
72  * @param  mutex  The mutex to be locked
73  *
74  * @return  CA_TRUE if the mutex is not locked currently, otherwise CA_FALSE.
75  *
76  */
77 CABool_t u_mutex_trylock(u_mutex mutex);
78
79 /**
80  * @fn  u_mutex_unlock
81  * @brief  Unlock the mutex
82  *
83  * @param  mutex  The mutex to be unlocked
84  *
85  */
86 void u_mutex_unlock(u_mutex mutex);
87
88 /**
89  * @fn  u_mutex_free
90  * @brief  Free the mutex
91  *
92  * @param  mutex  The mutex to be freed
93  *
94  */
95 void u_mutex_free(u_mutex mutex);
96
97 /**
98  * @fn  u_cond_new
99  * @brief  Creates new condition
100  *
101  * @return  Reference to newly created @u_cond, otherwise NULL.
102  *
103  * @see u_mutex_Init
104  */
105 u_cond u_cond_new(void);
106
107 /**
108  * @fn  u_cond_signal
109  * @brief  One of threads is woken up if multiple threads are waiting for @cond
110  *
111  * @param  cond  The condtion to be signaled
112  *
113  */
114 void u_cond_signal(u_cond cond);
115
116 /**
117  * @fn  u_cond_broadcast
118  * @brief  All of threads are woken up if multiple threads are waiting for @cond
119  *
120  * @param  cond  The condtion to be signaled
121  *
122  */
123 void u_cond_broadcast(u_cond cond);
124
125 /**
126  * @fn  u_cond_wait
127  * @brief  Waits untill this thread woken up on @cond
128  *
129  * @param  cond  The condtion to be wait for to signal
130  * @param  mutex  The mutex which is currently locked from calling thread
131  *
132  */
133 void u_cond_wait(u_cond cond, u_mutex mutex);
134
135 /**
136  * @fn  u_cond_wait
137  * @brief  Waits untill this thread woken up on @cond,
138  *      but not longer than until the time specified by milliseconds.
139  *      The mutex is unlocked before falling asleep and locked again before resuming.
140  *      If milliseconds is 0 or under, u_cond_timed_wait() acts like u_cond_wait().
141  *
142  * @param  cond  The condtion to be wait for to signal
143  * @param  mutex  The mutex which is currently locked from calling thread
144  * @param  microseconds  relative time for waiting, microseconds
145  *
146  */
147 void u_cond_timed_wait(u_cond cond, u_mutex mutex, int32_t microseconds);
148
149 /**
150  * @fn  u_cond_free
151  * @brief  Free the condition
152  *
153  * @param  cond  The condition to be freed
154  *
155  */
156 void u_cond_free(u_cond cond);
157
158 #ifdef __cplusplus
159 } /* extern "C" */
160 #endif /* __cplusplus */
161
162 #endif //__UMUTEX_H_