diff options
Diffstat (limited to 'uisimulator')
-rw-r--r-- | uisimulator/x11/thread.c | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/uisimulator/x11/thread.c b/uisimulator/x11/thread.c index b4a4ea13e7..ddaae313ae 100644 --- a/uisimulator/x11/thread.c +++ b/uisimulator/x11/thread.c | |||
@@ -20,6 +20,10 @@ | |||
20 | #include <stdio.h> | 20 | #include <stdio.h> |
21 | #include <pthread.h> | 21 | #include <pthread.h> |
22 | 22 | ||
23 | #include "kernel.h" | ||
24 | #include <poll.h> | ||
25 | |||
26 | |||
23 | /* | 27 | /* |
24 | * We emulate the target threads by using pthreads. We have a mutex that only | 28 | * We emulate the target threads by using pthreads. We have a mutex that only |
25 | * allows one thread at a time to execute. It forces each thread to yield() | 29 | * allows one thread at a time to execute. It forces each thread to yield() |
@@ -31,6 +35,9 @@ pthread_mutex_t mp; | |||
31 | void init_threads(void) | 35 | void init_threads(void) |
32 | { | 36 | { |
33 | pthread_mutex_init(&mp, NULL); | 37 | pthread_mutex_init(&mp, NULL); |
38 | /* get mutex to only allow one thread running at a time */ | ||
39 | pthread_mutex_lock(&mp); | ||
40 | |||
34 | } | 41 | } |
35 | /* | 42 | /* |
36 | int pthread_create(pthread_t *new_thread_ID, | 43 | int pthread_create(pthread_t *new_thread_ID, |
@@ -38,12 +45,19 @@ void init_threads(void) | |||
38 | void * (*start_func)(void *), void *arg); | 45 | void * (*start_func)(void *), void *arg); |
39 | */ | 46 | */ |
40 | 47 | ||
41 | void yield(void) | 48 | void (yield)(void) |
42 | { | 49 | { |
43 | pthread_mutex_unlock(&mp); /* return */ | 50 | pthread_mutex_unlock(&mp); /* return */ |
44 | pthread_mutex_lock(&mp); /* get it again */ | 51 | pthread_mutex_lock(&mp); /* get it again */ |
45 | } | 52 | } |
46 | 53 | ||
54 | void newfunc(void (*func)(void)) | ||
55 | { | ||
56 | yield(); | ||
57 | func(); | ||
58 | } | ||
59 | |||
60 | |||
47 | int create_thread(void* fp, void* sp, int stk_size) | 61 | int create_thread(void* fp, void* sp, int stk_size) |
48 | { | 62 | { |
49 | pthread_t tid; | 63 | pthread_t tid; |
@@ -54,16 +68,25 @@ int create_thread(void* fp, void* sp, int stk_size) | |||
54 | (void)sp; | 68 | (void)sp; |
55 | (void)stk_size; | 69 | (void)stk_size; |
56 | error = pthread_create(&tid, | 70 | error = pthread_create(&tid, |
57 | NULL, /* default attributes please */ | 71 | NULL, /* default attributes please */ |
58 | fp, /* function to start */ | 72 | (void *(*)(void *)) newfunc, /* function to start */ |
59 | NULL /* start argument */); | 73 | fp /* start argument */); |
60 | if(0 != error) | 74 | if(0 != error) |
61 | fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error); | 75 | fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error); |
62 | else | 76 | else |
63 | fprintf(stderr, "Thread %ld is running\n", tid); | 77 | fprintf(stderr, "Thread %ld is running\n", (long)tid); |
64 | 78 | ||
65 | /* get mutex to only allow one thread running at a time */ | 79 | yield(); |
66 | pthread_mutex_lock(&mp); | ||
67 | 80 | ||
68 | return error; | 81 | return error; |
69 | } | 82 | } |
83 | |||
84 | /* ticks is HZ per second */ | ||
85 | void x11_sleep(int ticks) | ||
86 | { | ||
87 | pthread_mutex_unlock(&mp); /* return */ | ||
88 | /* portable subsecond "sleep" */ | ||
89 | poll((void *)0, 0, ticks * 1000/HZ); | ||
90 | |||
91 | pthread_mutex_lock(&mp); /* get it again */ | ||
92 | } | ||