2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * This file is part of the bluedroid stack.
29 * Author: Adam Dunkels <adam@sics.se>
35 /** Create a new mutex
36 * @param mutex pointer to the mutex to create
37 * @return a new mutex */
39 osi_mutex_new(osi_mutex_t *pxMutex)
43 *pxMutex = xSemaphoreCreateMutex();
45 if (*pxMutex != NULL) {
49 //LWIP_DEBUGF(THREAD_SAFE_DEBUG, ("osi_mutex_new: m=%p\n", *pxMutex));
55 * @param mutex the mutex to lock */
57 osi_mutex_lock(osi_mutex_t *pxMutex)
59 while (xSemaphoreTake(*pxMutex, portMAX_DELAY) != pdPASS);
63 osi_mutex_trylock(osi_mutex_t *pxMutex)
65 if (xSemaphoreTake(*pxMutex, 0) == pdPASS) return 0;
70 * @param mutex the mutex to unlock */
72 osi_mutex_unlock(osi_mutex_t *pxMutex)
74 xSemaphoreGive(*pxMutex);
77 /** Delete a semaphore
78 * @param mutex the mutex to delete */
80 osi_mutex_free(osi_mutex_t *pxMutex)
82 //LWIP_DEBUGF(THREAD_SAFE_DEBUG, ("osi_mutex_free: m=%p\n", *pxMutex));
83 vQueueDelete(*pxMutex);
86 /*-----------------------------------------------------------------------------------*/
87 // Creates and returns a new semaphore. The "count" argument specifies
88 // the initial state of the semaphore. TBD finish and test
90 osi_sem_new(osi_sem_t *sem, uint8_t count)
93 vSemaphoreCreateBinary(*sem);
96 if (count == 0) { // Means it can't be taken
97 xSemaphoreTake(*sem, 1);
108 /*-----------------------------------------------------------------------------------*/
109 // Signals a semaphore
111 osi_sem_signal(osi_sem_t *sem)
113 xSemaphoreGive(*sem);
116 /*-----------------------------------------------------------------------------------*/
118 Blocks the thread while waiting for the semaphore to be
119 signaled. If the "timeout" argument is non-zero, the thread should
120 only be blocked for the specified time (measured in
123 If the timeout argument is non-zero, the return value is the number of
124 milliseconds spent waiting for the semaphore to be signaled. If the
125 semaphore wasn't signaled within the specified time, the return value is
126 OSI_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
127 (i.e., it was already signaled), the function may return zero.
129 Notice that lwIP implements a function with a similar name,
130 osi_sem_wait(), that uses the osi_arch_sem_wait() function.
133 osi_sem_wait(osi_sem_t *sem, uint32_t timeout)
135 portTickType StartTime, EndTime, Elapsed;
136 unsigned long ulReturn;
138 StartTime = xTaskGetTickCount();
141 if (xSemaphoreTake(*sem, timeout / portTICK_RATE_MS) == pdTRUE) {
142 EndTime = xTaskGetTickCount();
143 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
151 ulReturn = OSI_ARCH_TIMEOUT;
153 } else { // must block without a timeout
154 while (xSemaphoreTake(*sem, portMAX_DELAY) != pdTRUE);
156 EndTime = xTaskGetTickCount();
157 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
166 return ulReturn ; // return time blocked
169 /*-----------------------------------------------------------------------------------*/
170 // Deallocates a semaphore
172 osi_sem_free(osi_sem_t *sem)
174 vSemaphoreDelete(*sem);
177 /*-----------------------------------------------------------------------------------*/
178 // Initialize osi arch
184 /*-----------------------------------------------------------------------------------*/
188 return xTaskGetTickCount();
192 void osi_delay_ms(uint32_t ms)
194 vTaskDelay(ms/portTICK_RATE_MS);