1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
24 * This file provides APIs related to mutex and semaphores.
35 #endif /* __cplusplus */
37 typedef struct ca_mutex_internal *ca_mutex;
38 typedef struct ca_cond_internal *ca_cond;
41 * Enums for ca_cond_wait_for return values
45 CA_WAIT_SUCCESS = 0, /**< Condition Signal */
46 CA_WAIT_INVAL = -1, /**< Invalid Condition */
47 CA_WAIT_TIMEDOUT = -2 /**< Condition Timed Out */
53 * @return Reference to newly created mutex, otherwise NULL.
56 ca_mutex ca_mutex_new(void);
61 * @param mutex The mutex to be locked
64 void ca_mutex_lock(ca_mutex mutex);
67 * Checks if the mutex can be locked.
69 * @param mutex The mutex to be locked
71 * @return true if the mutex is not locked currently, otherwise false.
74 bool ca_mutex_trylock(ca_mutex mutex);
79 * @param mutex The mutex to be unlocked
82 void ca_mutex_unlock(ca_mutex mutex);
87 * @param mutex The mutex to be freed
90 bool ca_mutex_free(ca_mutex mutex);
93 * Creates new condition.
95 * @return Reference to newly created @ca_cond, otherwise NULL.
98 ca_cond ca_cond_new(void);
101 * One of threads is woken up if multiple threads are waiting for @cond.
103 * @param cond The condtion to be signaled
106 void ca_cond_signal(ca_cond cond);
109 * All of threads are woken up if multiple threads are waiting for @cond.
111 * @param cond The condtion to be signaled
114 void ca_cond_broadcast(ca_cond cond);
117 * Waits until this thread woken up on @cond.
119 * @param cond The condtion to be wait for to signal
120 * @param mutex The mutex which is currently locked from calling thread
123 void ca_cond_wait(ca_cond cond, ca_mutex mutex);
126 * Waits until this thread woken up on @cond,
127 * but not longer than the interval specified by microseconds.
128 * The mutex is unlocked before falling asleep and locked again before resuming.
129 * If microseconds is 0, ca_cond_wait_for() acts like ca_cond_wait().
131 * @param cond The condtion to be wait for to signal
132 * @param mutex The mutex which is currently locked from calling thread
133 * @param microseconds relative time for waiting, microseconds
135 * @return CA_WAIT_SUCCESS if the condition was signaled.
136 * CA_WAIT_TIMEDDOUT if wait period exceeded
137 * CA_WAIT_INVAL for invalid parameters
140 CAWaitResult_t ca_cond_wait_for(ca_cond cond, ca_mutex mutex, uint64_t microseconds);
143 * Free the condition.
145 * @param cond The condition to be freed
148 void ca_cond_free(ca_cond cond);
152 #endif /* __cplusplus */
154 #endif /* CA_MUTEX_H_ */