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
diff --git a/uisimulator/common/io.c b/uisimulator/common/io.c
index 6f9f4b2..3b45425 100644
--- a/uisimulator/common/io.c
+++ b/uisimulator/common/io.c
@@ -46,13 +46,8 @@
 #include "dir-win32.h"
 #endif
 
+#include <SDL_thread.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"
@@ -220,22 +215,18 @@ 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;
 }
 
@@ -244,39 +235,57 @@ int ata_spinup_time(void)
     return HZ;
 }
 
-static ssize_t io_trigger_and_wait(enum io_dir cmd)
+static int sim_io_thread_fn(void *data)
 {
-    void *mythread = NULL;
+    struct sim_io *io = data;
     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 0;
+}
+
+ssize_t io_trigger_and_wait(enum io_dir dir, int fd, const void *buf, size_t count)
+{
+    struct sim_io io;
+    SDL_Thread *t;
+
+    io.cmd = dir;
+    io.fd = fd;
+    io.buf = buf;
+    io.count = count;
+
+    semaphore_init(&io.sem, 2, 0);
+
+    printf("createThread()\n");
+
+    t = SDL_CreateThread(sim_io_thread_fn, &io);
+    if (!t) return -1;
 
-    return result;
+    printf("wait()\n");
+
+    semaphore_wait(&io.sem, TIMEOUT_BLOCK);
+
+    printf("wait() ret\n");
+    return io.result;
 }
 
 #if !defined(__PCTOOL__) && !defined(APPLICATION)
@@ -445,37 +454,18 @@ 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)