1 /**
2  * @file lv_sdl2.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_os.h"
10 
11 #if LV_USE_OS == LV_OS_SDL2
12 
13 #include <errno.h>
14 #include "../misc/lv_log.h"
15 
16 /*********************
17  *      DEFINES
18  *********************/
19 
20 /**********************
21  *      TYPEDEFS
22  **********************/
23 
24 /**********************
25  *  STATIC PROTOTYPES
26  **********************/
27 static int generic_callback(void * user_data);
28 
29 /**********************
30  *  STATIC VARIABLES
31  **********************/
32 
33 /**********************
34  *      MACROS
35  **********************/
36 
37 /**********************
38  *   GLOBAL FUNCTIONS
39  **********************/
40 
lv_thread_init(lv_thread_t * thread,const char * const name,lv_thread_prio_t prio,void (* callback)(void *),size_t stack_size,void * user_data)41 lv_result_t lv_thread_init(lv_thread_t * thread, const char * const name, lv_thread_prio_t prio,
42                            void (*callback)(void *), size_t stack_size, void * user_data)
43 {
44     LV_UNUSED(prio);
45     thread->callback = callback;
46     thread->user_data = user_data;
47     thread->thread = SDL_CreateThreadWithStackSize(generic_callback, name, stack_size, thread);
48     if(thread->thread == NULL) {
49         LV_LOG_ERROR("Error: %s", SDL_GetError());
50         return LV_RESULT_INVALID;
51     }
52     return LV_RESULT_OK;
53 }
54 
lv_thread_delete(lv_thread_t * thread)55 lv_result_t lv_thread_delete(lv_thread_t * thread)
56 {
57     int ret;
58     SDL_WaitThread(thread->thread, &ret);
59     if(ret != 0) {
60         LV_LOG_ERROR("Error: %d", ret);
61         return LV_RESULT_INVALID;
62     }
63 
64     return LV_RESULT_OK;
65 }
66 
lv_mutex_init(lv_mutex_t * mutex)67 lv_result_t lv_mutex_init(lv_mutex_t * mutex)
68 {
69     *mutex = SDL_CreateMutex();
70 
71     if(*mutex == NULL) {
72         LV_LOG_ERROR("Error: %s", SDL_GetError());
73         return LV_RESULT_INVALID;
74     }
75     else {
76         return LV_RESULT_OK;
77     }
78 }
79 
lv_mutex_lock(lv_mutex_t * mutex)80 lv_result_t lv_mutex_lock(lv_mutex_t * mutex)
81 {
82     int ret = SDL_LockMutex(*mutex);
83     if(ret) {
84         LV_LOG_ERROR("Error: %d", ret);
85         return LV_RESULT_INVALID;
86     }
87     else {
88         return LV_RESULT_OK;
89     }
90 }
91 
lv_mutex_lock_isr(lv_mutex_t * mutex)92 lv_result_t lv_mutex_lock_isr(lv_mutex_t * mutex)
93 {
94     int ret = SDL_LockMutex(*mutex);
95     if(ret) {
96         LV_LOG_ERROR("Error: %d", ret);
97         return LV_RESULT_INVALID;
98     }
99     else {
100         return LV_RESULT_OK;
101     }
102 }
103 
lv_mutex_unlock(lv_mutex_t * mutex)104 lv_result_t lv_mutex_unlock(lv_mutex_t * mutex)
105 {
106     int ret = SDL_UnlockMutex(*mutex);
107     if(ret) {
108         LV_LOG_ERROR("Error: %d", ret);
109         return LV_RESULT_INVALID;
110     }
111     else {
112         return LV_RESULT_OK;
113     }
114 }
115 
lv_mutex_delete(lv_mutex_t * mutex)116 lv_result_t lv_mutex_delete(lv_mutex_t * mutex)
117 {
118     SDL_DestroyMutex(*mutex);
119     return LV_RESULT_OK;
120 }
121 
lv_thread_sync_init(lv_thread_sync_t * sync)122 lv_result_t lv_thread_sync_init(lv_thread_sync_t * sync)
123 {
124     sync->mutex = SDL_CreateMutex();
125     if(sync->mutex == NULL) {
126         LV_LOG_ERROR("Error: %s", SDL_GetError());
127         return LV_RESULT_INVALID;
128     }
129     sync->cond = SDL_CreateCond();
130     if(sync->cond == NULL) {
131         LV_LOG_ERROR("Error: %s", SDL_GetError());
132         return LV_RESULT_INVALID;
133     }
134     sync->v = false;
135     return LV_RESULT_OK;
136 }
137 
lv_thread_sync_wait(lv_thread_sync_t * sync)138 lv_result_t lv_thread_sync_wait(lv_thread_sync_t * sync)
139 {
140     SDL_LockMutex(sync->mutex);
141     while(!sync->v) {
142         SDL_CondWait(sync->cond, sync->mutex);
143     }
144     sync->v = false;
145     SDL_UnlockMutex(sync->mutex);
146     return LV_RESULT_OK;
147 }
148 
lv_thread_sync_signal(lv_thread_sync_t * sync)149 lv_result_t lv_thread_sync_signal(lv_thread_sync_t * sync)
150 {
151     SDL_LockMutex(sync->mutex);
152     sync->v = true;
153     SDL_CondSignal(sync->cond);
154     SDL_UnlockMutex(sync->mutex);
155 
156     return LV_RESULT_OK;
157 }
158 
lv_thread_sync_delete(lv_thread_sync_t * sync)159 lv_result_t lv_thread_sync_delete(lv_thread_sync_t * sync)
160 {
161     SDL_DestroyMutex(sync->mutex);
162     SDL_DestroyCond(sync->cond);
163     return LV_RESULT_OK;
164 }
165 
lv_thread_sync_signal_isr(lv_thread_sync_t * sync)166 lv_result_t lv_thread_sync_signal_isr(lv_thread_sync_t * sync)
167 {
168     LV_UNUSED(sync);
169     return LV_RESULT_INVALID;
170 }
171 
172 /**********************
173  *   STATIC FUNCTIONS
174  **********************/
175 
generic_callback(void * user_data)176 static int generic_callback(void * user_data)
177 {
178     lv_thread_t * thread = user_data;
179     thread->callback(thread->user_data);
180     return 0;
181 }
182 
183 #endif /*LV_USE_OS == LV_OS_SDL2*/
184