diff options
author | Daniel Stenberg <daniel@haxx.se> | 2002-06-10 10:17:54 +0000 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2002-06-10 10:17:54 +0000 |
commit | 604cce76cffcad3e890084f90b1199a0ce46e176 (patch) | |
tree | 7c325a731ed34b9b7219eecd9218d6e40a4fc9fe /uisimulator/x11/thread.c | |
parent | b97f032464f654f9bd4c45a3b09a52e6a3693ca0 (diff) | |
download | rockbox-604cce76cffcad3e890084f90b1199a0ce46e176.tar.gz rockbox-604cce76cffcad3e890084f90b1199a0ce46e176.zip |
add thread functionality, powered by pthreads
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@928 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'uisimulator/x11/thread.c')
-rw-r--r-- | uisimulator/x11/thread.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/uisimulator/x11/thread.c b/uisimulator/x11/thread.c new file mode 100644 index 0000000000..769de5bef0 --- /dev/null +++ b/uisimulator/x11/thread.c | |||
@@ -0,0 +1,69 @@ | |||
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 | |||
23 | /* | ||
24 | * 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() | ||
26 | * for the other(s) to run. | ||
27 | */ | ||
28 | |||
29 | pthread_mutex_t mp; | ||
30 | |||
31 | void init_threads(void) | ||
32 | { | ||
33 | pthread_mutex_init(&mp, NULL); | ||
34 | } | ||
35 | /* | ||
36 | int pthread_create(pthread_t *new_thread_ID, | ||
37 | const pthread_attr_t *attr, | ||
38 | void * (*start_func)(void *), void *arg); | ||
39 | */ | ||
40 | |||
41 | void yield(void) | ||
42 | { | ||
43 | pthread_mutex_unlock(&mp); /* return */ | ||
44 | pthread_mutex_lock(&mp); /* get it again */ | ||
45 | } | ||
46 | |||
47 | int create_thread(void* fp, void* sp, int stk_size) | ||
48 | { | ||
49 | pthread_t tid; | ||
50 | int i; | ||
51 | int error; | ||
52 | |||
53 | /* we really don't care about these arguments */ | ||
54 | (void)sp; | ||
55 | (void)stk_size; | ||
56 | error = pthread_create(&tid, | ||
57 | NULL, /* default attributes please */ | ||
58 | fp, /* function to start */ | ||
59 | NULL /* start argument */); | ||
60 | if(0 != error) | ||
61 | fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error); | ||
62 | else | ||
63 | fprintf(stderr, "Thread %d is running\n", tid); | ||
64 | |||
65 | /* get mutex to only allow one thread running at a time */ | ||
66 | pthread_mutex_lock(&mp); | ||
67 | |||
68 | return error; | ||
69 | } | ||