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 | |
| 20 | #include <stdio.h> |
| 21 | #include <pthread.h> |
| 22 | |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 23 | #include "kernel.h" |
| 24 | #include <poll.h> |
| 25 | |
Daniel Stenberg | 973b1a8 | 2002-06-14 12:44:51 +0000 | [diff] [blame] | 26 | long current_tick = 0; |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 27 | |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 28 | /* |
| 29 | * We emulate the target threads by using pthreads. We have a mutex that only |
| 30 | * allows one thread at a time to execute. It forces each thread to yield() |
| 31 | * for the other(s) to run. |
| 32 | */ |
| 33 | |
| 34 | pthread_mutex_t mp; |
| 35 | |
| 36 | void init_threads(void) |
| 37 | { |
| 38 | pthread_mutex_init(&mp, NULL); |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 39 | /* get mutex to only allow one thread running at a time */ |
| 40 | pthread_mutex_lock(&mp); |
| 41 | |
Daniel Stenberg | 973b1a8 | 2002-06-14 12:44:51 +0000 | [diff] [blame] | 42 | current_tick = time(NULL); /* give it a boost from start! */ |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 43 | } |
| 44 | /* |
| 45 | int pthread_create(pthread_t *new_thread_ID, |
| 46 | const pthread_attr_t *attr, |
| 47 | void * (*start_func)(void *), void *arg); |
| 48 | */ |
| 49 | |
Daniel Stenberg | 973b1a8 | 2002-06-14 12:44:51 +0000 | [diff] [blame] | 50 | void yield(void) |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 51 | { |
Daniel Stenberg | 973b1a8 | 2002-06-14 12:44:51 +0000 | [diff] [blame] | 52 | current_tick+=3; |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 53 | pthread_mutex_unlock(&mp); /* return */ |
| 54 | pthread_mutex_lock(&mp); /* get it again */ |
| 55 | } |
| 56 | |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 57 | void newfunc(void (*func)(void)) |
| 58 | { |
Daniel Stenberg | 973b1a8 | 2002-06-14 12:44:51 +0000 | [diff] [blame] | 59 | yield(); |
| 60 | func(); |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 64 | int create_thread(void* fp, void* sp, int stk_size) |
| 65 | { |
| 66 | pthread_t tid; |
| 67 | int i; |
| 68 | int error; |
| 69 | |
| 70 | /* we really don't care about these arguments */ |
| 71 | (void)sp; |
| 72 | (void)stk_size; |
| 73 | error = pthread_create(&tid, |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 74 | NULL, /* default attributes please */ |
| 75 | (void *(*)(void *)) newfunc, /* function to start */ |
| 76 | fp /* start argument */); |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 77 | if(0 != error) |
Daniel Stenberg | 973b1a8 | 2002-06-14 12:44:51 +0000 | [diff] [blame] | 78 | fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error); |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 79 | else |
Daniel Stenberg | 973b1a8 | 2002-06-14 12:44:51 +0000 | [diff] [blame] | 80 | fprintf(stderr, "Thread %ld is running\n", (long)tid); |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 81 | |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 82 | yield(); |
Daniel Stenberg | 604cce7 | 2002-06-10 10:17:54 +0000 | [diff] [blame] | 83 | |
| 84 | return error; |
| 85 | } |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 86 | |
| 87 | /* ticks is HZ per second */ |
Linus Nielsen Feltzing | a6142ab | 2004-06-10 13:29:52 +0000 | [diff] [blame] | 88 | void sim_sleep(int ticks) |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 89 | { |
Daniel Stenberg | 973b1a8 | 2002-06-14 12:44:51 +0000 | [diff] [blame] | 90 | current_tick+=5; |
Daniel Stenberg | 5fbeb6d | 2002-06-14 12:33:51 +0000 | [diff] [blame] | 91 | pthread_mutex_unlock(&mp); /* return */ |
| 92 | /* portable subsecond "sleep" */ |
| 93 | poll((void *)0, 0, ticks * 1000/HZ); |
| 94 | |
| 95 | pthread_mutex_lock(&mp); /* get it again */ |
| 96 | } |