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.
38 #endif /* __cplusplus */
40 typedef struct oc_mutex_internal *oc_mutex;
41 typedef struct oc_cond_internal *oc_cond;
42 typedef struct oc_thread_internal *oc_thread;
45 * Enums for oc_cond_wait_for return values.
49 OC_WAIT_SUCCESS = 0, /**< Condition Signal. */
50 OC_WAIT_INVAL = -1, /**< Invalid Condition. */
51 OC_WAIT_TIMEDOUT = -2 /**< Condition Timed Out. */
56 OC_THREAD_SUCCESS = 0,
57 OC_THREAD_ALLOCATION_FAILURE = 1,
58 OC_THREAD_CREATE_FAILURE = 2,
59 OC_THREAD_INVALID = 3,
60 OC_THREAD_WAIT_FAILURE = 4,
61 OC_THREAD_INVALID_PARAMETER = 5,
62 OC_THREAD_CANCEL_FAILURE = 6
66 * Allocates, and starts a new thread
68 * @param[out] t The thread that will refer to a newly allocated, and started thread
69 * @param[in] start_routine The function that will execute in a new thread
70 * @param[in] arg The information passed to the start_routine
71 * @return OCThreadResult_t An enumeration of possible outcomes
72 * @retval OC_THREAD_SUCCESS If a thread was successfully allocated and started.
73 * @retval OC_THREAD_ALLOCATION_FAILURE If a thread was unable to be allocated
74 * @retval OC_THREAD_CREATE_FAILURE If a thread was unable to be started
78 OCThreadResult_t oc_thread_new(oc_thread *t, void *(*start_routine)(void *), void *arg);
80 OCThreadResult_t oc_thread_new(oc_thread *t, void *(*start_routine)(void *), void *arg,
81 const char *task_name, int stack_size);
85 * Frees a thread previously allocated with oc_thread_new()
87 * @param[in] t The thread to be unallocated
88 * @return OCThreadResult_t An enumeration of possible outcomes
89 * @retval OC_THREAD_SUCCESS If a thread was successfully unallocated
90 * @retval OC_THREAD_INVALID_PARAMETER If param t is NULL
93 OCThreadResult_t oc_thread_free(oc_thread t);
96 * Block until a thread's execution has been completed
98 * @param[in] t The thread to be waited on
99 * @return OCThreadResult_t An enumeration of possible outcomes
100 * @retval OC_THREAD_SUCCESS If the thread successfully completed execution
101 * @retval OC_THREAD_WAIT_FAILURE If a problem occured while waiting for execution of the thread to complete
104 OCThreadResult_t oc_thread_wait(oc_thread t);
108 * Cancel the thread without block
110 * @param[in] t The thread to be canceled on
111 * @return OCThreadResult_t An enumeration of possible outcomes
112 * @retval OC_THREAD_SUCCESS If the thread successfully completed execution
113 * @retval OC_THREAD_CANCEL_FAILURE If a problem occured while canceling
116 OCThreadResult_t oc_thread_cancel(oc_thread t);
122 * @return Reference to newly created mutex, otherwise NULL.
125 oc_mutex oc_mutex_new(void);
130 * @param mutex The mutex to be locked.
133 void oc_mutex_lock(oc_mutex mutex);
138 * @param mutex The mutex to be unlocked.
141 void oc_mutex_unlock(oc_mutex mutex);
146 * @param mutex The mutex to be freed.
147 * @return bool to indicate success or failure
148 * @retval true if mutex was freed successfully
149 * @retval false if mutex parameter is invalid
152 bool oc_mutex_free(oc_mutex mutex);
155 * Creates new condition.
157 * @return Reference to newly created oc_cond, otherwise NULL.
160 oc_cond oc_cond_new(void);
163 * One of threads is woken up if multiple threads are waiting for cond.
165 * @param cond The condtion to be signaled.
168 void oc_cond_signal(oc_cond cond);
171 * All of threads are woken up if multiple threads are waiting for cond.
173 * @param cond The condtion to be signaled.
176 void oc_cond_broadcast(oc_cond cond);
179 * Waits until this thread woken up on cond.
181 * @param cond The condtion to be wait for to signal.
182 * @param mutex The mutex which is currently locked from calling thread.
185 void oc_cond_wait(oc_cond cond, oc_mutex mutex);
188 * Waits until this thread woken up on cond,
189 * but not longer than the interval specified by microseconds.
190 * The mutex is unlocked before falling asleep and locked again before resuming.
191 * If microseconds is 0, oc_cond_wait_for() acts like oc_cond_wait().
193 * @param cond The condtion to be wait for to signal.
194 * @param mutex The mutex which is currently locked from calling thread.
195 * @param microseconds relative time for waiting, microseconds.
197 * @return OC_WAIT_SUCCESS if the condition was signaled,
198 * OC_WAIT_TIMEDOUT if wait period exceeded,
199 * OC_WAIT_INVAL for invalid parameters.
202 OCWaitResult_t oc_cond_wait_for(oc_cond cond, oc_mutex mutex, uint64_t microseconds);
205 * Free the condition.
207 * @param cond The condition to be freed.
210 void oc_cond_free(oc_cond cond);
214 #endif /* __cplusplus */
216 #endif /* OC_THREAD_H_ */