blob: e37373dc546be25fe56ccfe56ee28ee43e69b962 [file] [log] [blame]
Daniel Stenberg604cce72002-06-10 10:17:54 +00001/***************************************************************************
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 Stenberg5fbeb6d2002-06-14 12:33:51 +000023#include "kernel.h"
24#include <poll.h>
25
Daniel Stenberg973b1a82002-06-14 12:44:51 +000026long current_tick = 0;
Daniel Stenberg5fbeb6d2002-06-14 12:33:51 +000027
Daniel Stenberg604cce72002-06-10 10:17:54 +000028/*
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
34pthread_mutex_t mp;
35
36void init_threads(void)
37{
38 pthread_mutex_init(&mp, NULL);
Daniel Stenberg5fbeb6d2002-06-14 12:33:51 +000039 /* get mutex to only allow one thread running at a time */
40 pthread_mutex_lock(&mp);
41
Daniel Stenberg973b1a82002-06-14 12:44:51 +000042 current_tick = time(NULL); /* give it a boost from start! */
Daniel Stenberg604cce72002-06-10 10:17:54 +000043}
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 Stenberg973b1a82002-06-14 12:44:51 +000050void yield(void)
Daniel Stenberg604cce72002-06-10 10:17:54 +000051{
Daniel Stenberg973b1a82002-06-14 12:44:51 +000052 current_tick+=3;
Daniel Stenberg604cce72002-06-10 10:17:54 +000053 pthread_mutex_unlock(&mp); /* return */
54 pthread_mutex_lock(&mp); /* get it again */
55}
56
Daniel Stenberg5fbeb6d2002-06-14 12:33:51 +000057void newfunc(void (*func)(void))
58{
Daniel Stenberg973b1a82002-06-14 12:44:51 +000059 yield();
60 func();
Daniel Stenberg5fbeb6d2002-06-14 12:33:51 +000061}
62
63
Daniel Stenberg604cce72002-06-10 10:17:54 +000064int 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 Stenberg5fbeb6d2002-06-14 12:33:51 +000074 NULL, /* default attributes please */
75 (void *(*)(void *)) newfunc, /* function to start */
76 fp /* start argument */);
Daniel Stenberg604cce72002-06-10 10:17:54 +000077 if(0 != error)
Daniel Stenberg973b1a82002-06-14 12:44:51 +000078 fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
Daniel Stenberg604cce72002-06-10 10:17:54 +000079 else
Daniel Stenberg973b1a82002-06-14 12:44:51 +000080 fprintf(stderr, "Thread %ld is running\n", (long)tid);
Daniel Stenberg604cce72002-06-10 10:17:54 +000081
Daniel Stenberg5fbeb6d2002-06-14 12:33:51 +000082 yield();
Daniel Stenberg604cce72002-06-10 10:17:54 +000083
84 return error;
85}
Daniel Stenberg5fbeb6d2002-06-14 12:33:51 +000086
87/* ticks is HZ per second */
Linus Nielsen Feltzinga6142ab2004-06-10 13:29:52 +000088void sim_sleep(int ticks)
Daniel Stenberg5fbeb6d2002-06-14 12:33:51 +000089{
Daniel Stenberg973b1a82002-06-14 12:44:51 +000090 current_tick+=5;
Daniel Stenberg5fbeb6d2002-06-14 12:33:51 +000091 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}