Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 Daniel Stenberg |
| 11 | * |
| 12 | * All files in this archive are subject to the GNU General Public License. |
| 13 | * See the file COPYING in the source tree root for full license agreement. |
| 14 | * |
| 15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 16 | * KIND, either express or implied. |
| 17 | * |
| 18 | ****************************************************************************/ |
| 19 | |
Daniel Stenberg | febb52f | 2005-07-14 21:46:07 +0000 | [diff] [blame] | 20 | #include "autoconf.h" |
| 21 | |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <pthread.h> |
| 24 | |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 25 | #include "kernel.h" |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 26 | #include <sys/time.h> |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 27 | |
Daniel Stenberg | febb52f | 2005-07-14 21:46:07 +0000 | [diff] [blame] | 28 | #ifdef ROCKBOX_HAS_SIMSOUND |
| 29 | #include "sound.h" |
| 30 | #endif |
| 31 | |
Daniel Stenberg | 973b1a8 | 2002-06-14 12:44:51 +0000 | [diff] [blame] | 32 | long current_tick = 0; |
Jens Arnold | b51f7df | 2005-11-21 23:55:39 +0000 | [diff] [blame] | 33 | extern void sim_tick_tasks(void); |
Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 34 | |
| 35 | static void msleep(int msec) |
| 36 | { |
| 37 | struct timeval delay; |
| 38 | |
| 39 | delay.tv_sec = msec / 1000; |
Jens Arnold | 441e512 | 2005-05-11 20:04:43 +0000 | [diff] [blame] | 40 | delay.tv_usec = (msec % 1000) * 1000; |
Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 41 | select(0, NULL, NULL, NULL, &delay); /* portable sub-second sleep */ |
| 42 | } |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 43 | |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 44 | /* |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 45 | * This is not a target thread, so it does not fall under the 1 thread at a |
| 46 | * time thing. |
| 47 | */ |
| 48 | static void update_tick_thread() |
| 49 | { |
Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 50 | struct timeval start, now; |
| 51 | long new_tick; |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 52 | |
| 53 | gettimeofday(&start, NULL); |
| 54 | while (1) |
| 55 | { |
Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 56 | msleep(5); /* check twice per simulated target tick */ |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 57 | gettimeofday(&now, NULL); |
Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 58 | new_tick = (now.tv_sec - start.tv_sec) * HZ |
| 59 | + (now.tv_usec - start.tv_usec) / (1000000/HZ); |
| 60 | if (new_tick > current_tick) |
| 61 | { |
Jens Arnold | b51f7df | 2005-11-21 23:55:39 +0000 | [diff] [blame] | 62 | sim_tick_tasks(); |
Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 63 | current_tick = new_tick; |
Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 64 | } |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | /* |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 69 | * We emulate the target threads by using pthreads. We have a mutex that only |
| 70 | * allows one thread at a time to execute. It forces each thread to yield() |
| 71 | * for the other(s) to run. |
| 72 | */ |
| 73 | |
| 74 | pthread_mutex_t mp; |
| 75 | |
| 76 | void init_threads(void) |
| 77 | { |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 78 | pthread_t tick_tid; |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 79 | |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 80 | pthread_mutex_init(&mp, NULL); |
| 81 | /* get mutex to only allow one thread running at a time */ |
| 82 | pthread_mutex_lock(&mp); |
| 83 | |
Daniel Stenberg | febb52f | 2005-07-14 21:46:07 +0000 | [diff] [blame] | 84 | /* start a tick thread */ |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 85 | pthread_create(&tick_tid, NULL, (void *(*)(void *)) update_tick_thread, |
| 86 | NULL); |
Daniel Stenberg | febb52f | 2005-07-14 21:46:07 +0000 | [diff] [blame] | 87 | |
| 88 | #ifdef ROCKBOX_HAS_SIMSOUND /* start thread that plays PCM data */ |
| 89 | { |
| 90 | pthread_t sound_tid; |
| 91 | pthread_create(&sound_tid, NULL, |
| 92 | (void *(*)(void *)) sound_playback_thread, |
| 93 | NULL); |
| 94 | } |
| 95 | #endif |
| 96 | |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 97 | } |
| 98 | /* |
| 99 | int pthread_create(pthread_t *new_thread_ID, |
| 100 | const pthread_attr_t *attr, |
| 101 | void * (*start_func)(void *), void *arg); |
| 102 | */ |
| 103 | |
Daniel Stenberg | 973b1a8 | 2002-06-14 12:44:51 +0000 | [diff] [blame] | 104 | void yield(void) |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 105 | { |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 106 | pthread_mutex_unlock(&mp); /* return */ |
Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 107 | msleep(1); /* prevent busy loop */ |
| 108 | pthread_mutex_lock(&mp); /* get it again */ |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 111 | void newfunc(void (*func)(void)) |
| 112 | { |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 113 | pthread_mutex_lock(&mp); |
Daniel Stenberg | 973b1a8 | 2002-06-14 12:44:51 +0000 | [diff] [blame] | 114 | func(); |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 115 | pthread_mutex_unlock(&mp); |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |
Linus Nielsen Feltzing | a19acbd | 2005-07-12 10:05:13 +0000 | [diff] [blame] | 119 | int create_thread(void (*fp)(void), void* sp, int stk_size) |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 120 | { |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 121 | pthread_t tid; |
| 122 | int i; |
| 123 | int error; |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 124 | |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 125 | /* we really don't care about these arguments */ |
| 126 | (void)sp; |
| 127 | (void)stk_size; |
| 128 | error = pthread_create(&tid, |
| 129 | NULL, /* default attributes please */ |
| 130 | (void *(*)(void *)) newfunc, /* function to start */ |
| 131 | fp /* start argument */); |
| 132 | if(0 != error) |
| 133 | fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error); |
| 134 | else |
| 135 | fprintf(stderr, "Thread %ld is running\n", (long)tid); |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 136 | |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 137 | yield(); |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 138 | |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 139 | return error; |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 140 | } |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 141 | |
Linus Nielsen Feltzing | a6142ab | 2004-06-10 13:29:52 +0000 | [diff] [blame] | 142 | void sim_sleep(int ticks) |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 143 | { |
Jens Arnold | 6c19c85 | 2005-03-07 20:04:05 +0000 | [diff] [blame] | 144 | pthread_mutex_unlock(&mp); /* return */ |
Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 145 | msleep((1000/HZ) * ticks); |
| 146 | pthread_mutex_lock(&mp); /* get it again */ |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 147 | } |
Daniel Stenberg | 22b7701 | 2005-02-22 12:19:12 +0000 | [diff] [blame] | 148 | |