OSDN Git Service

Patch from M. R. Brown <mrbrown@0xd6.org> to fix pthread support
[uclinux-h8/uClibc.git] / libpthread / pthread.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * A simple clone based pthread implementation
4  *
5  * Copyright (C) 2001,2002 by Erik Andersen <andersee@debian.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #include <stdlib.h>
23 #include <sched.h>
24 #include <signal.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <pthread.h>
28
29 #define STACKSIZE 8096
30
31 #define CSIGNAL         0x000000ff      /* signal mask to be sent at exit */
32 #define CLONE_VM        0x00000100      /* set if VM shared between processes */
33 #define CLONE_FS        0x00000200      /* set if fs info shared between proces ses */
34 #define CLONE_FILES     0x00000400      /* set if open files shared between pro cesses */
35 #define CLONE_SIGHAND   0x00000800      /* set if signal handlers shared */
36
37
38
39 /* Lame home-grown clone based threading */
40 int pthread_mutex_init (pthread_mutex_t *mutex, const pthread_mutexattr_t *mutex_attr)
41 {
42     mutex->__m_lock.__spinlock = 1;
43         return 0;
44 }
45
46 int pthread_mutex_lock (pthread_mutex_t *mutex)
47 {
48         while (mutex->__m_lock.__spinlock == 0) {
49                 usleep(10000);
50         }
51         --(mutex->__m_lock.__spinlock);
52         return 0;
53 }
54
55 int pthread_mutex_unlock (pthread_mutex_t *mutex)
56 {
57     ++(mutex->__m_lock.__spinlock);
58         return 0;
59 }
60
61 int pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex)
62 {
63         ++(mutex->__m_lock.__spinlock);
64         while (cond->__c_lock.__spinlock == 0) {
65                 usleep(10000);
66         }
67         --(cond->__c_lock.__spinlock);
68         return 0;
69 }
70
71 int pthread_cond_signal(pthread_cond_t *cond)
72 {
73     ++(cond->__c_lock.__spinlock);
74         return 0;
75 }
76
77 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
78 {
79     cond->__c_lock.__spinlock = 1;
80         return 0;
81 }
82
83 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void* (*fn)(void *), void *data)
84 {
85         long retval;
86         void **newstack;
87         int (*clonefunc)(void *) = (int (*)(void *))(fn);
88
89         newstack = (void **) malloc(STACKSIZE);
90         if (!newstack)
91                 return -1;
92         newstack = (void **) (STACKSIZE + (char *) newstack);
93         *--newstack = data;
94         retval = clone(clonefunc, newstack, 
95                         CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD, data);  
96         if (retval < 0) {
97                 errno = -retval;
98                 *thread = 0;
99                 retval = -1;
100         } else {
101                 *thread = retval;
102                 retval = 0;
103         }
104         return retval;
105 }
106
107 int pthread_join (pthread_t thread, void **thread_return)
108 {
109         int retval;
110         /* Fixme -- wait for thread and get its return value */
111         retval = EXIT_SUCCESS;
112         if (thread_return)
113                 (int)*thread_return = retval;
114         _exit(retval);
115 }
116 link_warning(pthread_join, "pthread_join is a stub and does not behave properly");
117
118 void pthread_exit (void *retval)
119 {
120         _exit(*(int *)retval);
121 }