1 /*
2 * Copyright (C) 2013 SAMSUNG S.LSI
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18 #include <pthread.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "hal.h"
22 #include "osi.h"
23
24 #include <errno.h>
25 #include <time.h>
26
27 int osi_debug_level;
28 tOSI_INFO osi_info;
29
OSI_init(void)30 OSI_STATE OSI_init(void) {
31 int32_t index;
32
33 memset(&osi_info, 0, sizeof(osi_info));
34
35 /* Initialize lock */
36 pthread_mutex_init(&osi_info.mutex, NULL);
37
38 /* Initialize task information */
39 for (index = 0; index < OSI_MAX_TASK; index++) {
40 osi_info.task[index].state = OSI_FREE;
41 }
42
43 /* Initialize memory information */
44 osi_info.mem_max_cnt = OSI_MAX_MEM_POOL;
45 for (index = 0; index < OSI_MAX_MEM_POOL; index++) {
46 #ifdef OSI_USE_DYNAMIC_BUF
47 osi_info.mem[index] = (tOSI_MEM_HANDLER)malloc(OSI_MEM_POOL_SIZE);
48 if (osi_info.mem[index] == NULL) {
49 OSI_loge("%s : maximum conut of buffer is set to %d, (expected: %d)",
50 index, OSI_MAX_MEM_POOL);
51 osi_info.mem_max_cnt = index;
52 break;
53 }
54 osi_info.mem[index]->state = OSI_FREE;
55 #else
56 osi_info.mem[index].state = OSI_FREE;
57 #endif
58 }
59
60 /* Initialize queue information */
61 osi_info.queue_max_cnt = OSI_MAX_QUEUE;
62 for (index = 0; index < OSI_MAX_QUEUE; index++) {
63 osi_info.queue[index].state = OSI_FREE;
64 osi_info.queue[index].tail = OSI_QUEUE_SIZE;
65 }
66
67 /* Initialize timer information */
68 for (index = 0; index < OSI_MAX_TIMER; index++) {
69 osi_info.timer[index].state = OSI_FREE;
70 osi_info.timer[index].name = NULL;
71 osi_info.timer[index].callback = NULL;
72 osi_info.timer[index].callback_param = NULL;
73 }
74
75 return OSI_OK;
76 }
77
OSI_deinit()78 void OSI_deinit() {
79 int index;
80 #ifdef OSI_USE_DYNAMIC_BUF
81 for (index = 0; index < osi_info.mem_max_cnt; index++) {
82 if (osi_info.mem[index]) free(osi_info.mem[index]);
83 }
84 #endif
85
86 /* deinitialize timer */
87 for (index = 0; index < OSI_MAX_TIMER; index++) {
88 OSI_timer_free(&osi_info.timer[index]);
89 }
90 osi_info.usingTimer = 0;
91 }
92
osi_lock()93 void osi_lock() { pthread_mutex_lock(&osi_info.mutex); }
94
osi_unlock()95 void osi_unlock() { pthread_mutex_unlock(&osi_info.mutex); }
96
OSI_delay(uint32_t timeout)97 void OSI_delay(uint32_t timeout) {
98 struct timespec delay;
99 int err;
100
101 delay.tv_sec = timeout / 1000;
102 delay.tv_nsec = 1000 * 1000 * (timeout % 1000);
103
104 do {
105 err = nanosleep(&delay, &delay);
106 } while (err < 0 && errno == EINTR);
107 }
108