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);
69 * @param mutex The mutex to be unlocked.
72 void ca_mutex_unlock(ca_mutex mutex);
77 * @param mutex The mutex to be freed.
80 bool ca_mutex_free(ca_mutex mutex);
83 * Creates new condition.
85 * @return Reference to newly created ::ca_cond, otherwise NULL.
88 ca_cond ca_cond_new(void);
91 * One of threads is woken up if multiple threads are waiting for cond.
93 * @param cond The condtion to be signaled.
96 void ca_cond_signal(ca_cond cond);
99 * All of threads are woken up if multiple threads are waiting for cond.
101 * @param cond The condtion to be signaled.
104 void ca_cond_broadcast(ca_cond cond);
107 * Waits until this thread woken up on cond.
109 * @param cond The condtion to be wait for to signal.
110 * @param mutex The mutex which is currently locked from calling thread.
113 void ca_cond_wait(ca_cond cond, ca_mutex mutex);
116 * Waits until this thread woken up on cond,
117 * but not longer than the interval specified by microseconds.
118 * The mutex is unlocked before falling asleep and locked again before resuming.
119 * If microseconds is 0, ca_cond_wait_for() acts like ca_cond_wait().
121 * @param cond The condtion to be wait for to signal.
122 * @param mutex The mutex which is currently locked from calling thread.
123 * @param microseconds relative time for waiting, microseconds.
125 * @return ::CA_WAIT_SUCCESS if the condition was signaled,
126 * ::CA_WAIT_TIMEDOUT if wait period exceeded,
127 * ::CA_WAIT_INVAL for invalid parameters.
130 CAWaitResult_t ca_cond_wait_for(ca_cond cond, ca_mutex mutex, uint64_t microseconds);
133 * Free the condition.
135 * @param cond The condition to be freed.
138 void ca_cond_free(ca_cond cond);
142 #endif /* __cplusplus */
144 #endif /* CA_MUTEX_H_ */