Thank you to anyone who has already donated - your generous donations helped make three months of treatment possible.
My brother Nate continues to fight stage IV Hodgkin's lymphoma. He's just 31, with a wife and baby girl. They have no active income (since he's been unable to return to work), no insurance, and cannot afford the treatment he needs. Nate and his family need your help. Please consider a donation, every dollar helps. Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
diff --git a/firmware/target/hosted/sdl/button-sdl.c b/firmware/target/hosted/sdl/button-sdl.c index 8375151..29c697f 100644 static bool event_handler(SDL_Event *event) case SDL_USEREVENT: return true; break; + case SDL_USEREVENT+1: + sim_io_thread_fn((SDL_UserEvent*)event); + break; #ifdef HAVE_DYNAMIC_LCD_SIZE case SDL_VIDEORESIZE: /* NOTE: SDL 1.2.15 has a bug where this event is not always received */ static bool event_handler(SDL_Event *event) return false; } +/* SDL_WaitEvent is not suitable for very fast event handling, as it + * internally calls SDL_Delay(10) internally + * http://gameprogrammer.com/fastevents/fastevents1.html + * + * We replace it with an equivalent SDL_PollEvent mechanism which can + * be interrupted with gui_message_force_timeout() to not wait the full 10ms */ +static SDL_cond *ev_cond, *ev_wait; +void gui_message_push_event(SDL_Event *ev) +{ + while (!ev_cond || !ev_wait) SDL_Delay(10); + + SDL_mutex *m = SDL_CreateMutex(); + while(SDL_PushEvent(ev) == -1) + SDL_CondWait(ev_cond, m); + SDL_CondSignal(ev_cond); +} + void gui_message_loop(void) { SDL_Event event; bool quit; + int result; + + SDL_mutex *m = SDL_CreateMutex(); + ev_cond = SDL_CreateCond(); + ev_wait = SDL_CreateCond(); + + SDL_LockMutex(m); do { /* wait for the next event */ - if(SDL_WaitEvent(&event) == 0) { - printf("SDL_WaitEvent() error\n"); + SDL_CondWaitTimeout(ev_cond, m, 10); + result = SDL_PollEvent(&event); + if (result == 0) { + continue; + } + else if (result < 0) { + printf("SDL_PollEvent() error\n"); return; /* error, out of here */ } + else + SDL_CondSignal(ev_wait); sim_enter_irq_handler(); quit = event_handler(&event); sim_exit_irq_handler(); - } while(!quit); + + SDL_UnlockMutex(m); } + static void button_event(int key, bool pressed) { int new_btn = 0; diff --git a/uisimulator/common/io.c b/uisimulator/common/io.c index 6f9f4b2..1dc7959 100644 #include "dir-win32.h" #endif +#include <SDL.h> #include <fcntl.h> -#ifdef HAVE_SDL_THREADS -#include "thread-sdl.h" -#else -#define sim_thread_unlock() NULL -#define sim_thread_lock(a) -#endif #include "thread.h" #include "kernel.h" #include "debug.h" enum io_dir struct sim_io { - struct mutex sim_mutex; /* Rockbox mutex */ + struct semaphore sem; /* Rockbox semaphore */ int cmd; /* The command to perform */ int ready; /* I/O ready flag - 1= ready */ int fd; /* The file to read/write */ - void *buf; /* The buffer to read/write */ + const void *buf; /* The buffer to read/write */ size_t count; /* Number of bytes to read/write */ - size_t accum; /* Acculated bytes transferred */ + size_t result; /* Bytes transferred */ }; -static struct sim_io io; - int ata_init(void) { /* Initialize the rockbox kernel objects on a rockbox thread */ - mutex_init(&io.sim_mutex); - io.accum = 0; return 1; } int ata_spinup_time(void) return HZ; } -static ssize_t io_trigger_and_wait(enum io_dir cmd) + +int sim_io_thread_fn(SDL_UserEvent *ev) { - void *mythread = NULL; + struct sim_io *io = ev->data1; ssize_t result; - if (io.count > IO_YIELD_THRESHOLD || - (io.accum += io.count) >= IO_YIELD_THRESHOLD) - { - /* Allow other rockbox threads to run */ - io.accum = 0; - mythread = sim_thread_unlock(); - } + //~ printf("%s() enter\n", __func__); - switch (cmd) + switch (io->cmd) { case IO_READ: - result = read(io.fd, io.buf, io.count); + result = read(io->fd, (void*)io->buf, io->count); break; case IO_WRITE: - result = write(io.fd, io.buf, io.count); + result = write(io->fd, io->buf, io->count); break; /* shut up gcc */ default: result = -1; } - /* Regain our status as current */ - if (mythread != NULL) - { - sim_thread_lock(mythread); - } + io->result = result; + semaphore_release(&io->sem); + + //~ printf("%s() exit\n", __func__); - return result; + return 0; +} + +ssize_t io_trigger_and_wait(enum io_dir dir, int fd, const void *buf, size_t count) +{ + struct sim_io io; + SDL_UserEvent ev; + + io.cmd = dir; + io.fd = fd; + io.buf = buf; + io.count = count; + + semaphore_init(&io.sem, 1, 0); + + //~ printf("createThread()\n"); + + memset(&ev, 0, sizeof(ev)); + ev.type = SDL_USEREVENT+1; + ev.data1 = &io; + gui_message_push_event((SDL_Event*)&ev); + + //~ printf("wait()\n"); + + semaphore_wait(&io.sem, TIMEOUT_BLOCK); + + //~ printf("wait() ret\n"); + return io.result; } #if !defined(__PCTOOL__) && !defined(APPLICATION) int sim_creat(const char *name, mode_t mode) ssize_t sim_read(int fd, void *buf, size_t count) { - ssize_t result; - - mutex_lock(&io.sim_mutex); - - /* Setup parameters */ - io.fd = fd; - io.buf = buf; - io.count = count; - - result = io_trigger_and_wait(IO_READ); - - mutex_unlock(&io.sim_mutex); - - return result; + /* direct call for small amounts */ + if (count < IO_YIELD_THRESHOLD) + return read(fd, buf, count); + return io_trigger_and_wait(IO_READ, fd, buf, count); } ssize_t sim_write(int fd, const void *buf, size_t count) { - ssize_t result; - - mutex_lock(&io.sim_mutex); - - io.fd = fd; - io.buf = (void*)buf; - io.count = count; - - result = io_trigger_and_wait(IO_WRITE); - - mutex_unlock(&io.sim_mutex); - - return result; + /* direct call for small amounts */ + if (count < IO_YIELD_THRESHOLD) + return write(fd, buf, count); + return io_trigger_and_wait(IO_WRITE, fd, buf, count); } int sim_mkdir(const char *name) |