source: trunk/sys/libpthread/pthread.c @ 112

Last change on this file since 112 was 1, checked in by alain, 8 years ago

First import

File size: 4.9 KB
Line 
1/*
2 * pthread.c - threads creation/synchronization related functions
3 *
4 * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
5 * Copyright (c) 2011,2012 UPMC Sorbonne Universites
6 *
7 * This file is part of ALMOS.
8 *
9 * ALMOS is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2.0 of the License.
12 *
13 * ALMOS is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with ALMOS; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <sys/types.h>
24#include <errno.h>
25#include <stdio.h>
26#include <stdint.h>
27#include <assert.h>
28#include <stdlib.h>
29#include <sys/syscall.h>
30#include <cpu-syscall.h>
31#include <stdlib.h>
32#include <string.h>
33
34#include <pthread.h>
35
36pthread_mutex_t __printf_lock = PTHREAD_MUTEX_INITIALIZER;
37uint_t ___dmsg_lock = 0;
38uint_t ___dmsg_ok = 0;
39
40void __pthread_init(void)
41{
42        __pthread_keys_init();
43        __pthread_barrier_init();
44}
45
46void __pthread_tls_init(struct __pthread_tls_s *tls)
47{
48        memset(tls, 0, sizeof(*tls));
49        tls->signature = __PTHREAD_OBJECT_CREATED;
50        cpu_set_tls(tls);
51        cpu_syscall(&tls->attr,NULL,NULL,NULL,SYS_GETATTR);
52}
53
54void pthread_exit (void *retval)
55{
56        cpu_syscall(retval,NULL,NULL,NULL,SYS_EXIT);
57}
58
59static void* __pthread_start(void* (*entry_func)(void*), void *arg)
60{
61        void *retval;
62        __pthread_tls_t tls;
63        pthread_t tid;
64        struct __shared_s *shared;
65 
66        __pthread_tls_init(&tls);
67
68        shared          = arg;
69        shared->tid     = tls.attr.key;
70        shared->mailbox = 0;
71
72        __pthread_tls_set(&tls,__PT_TLS_SHARED, shared);
73
74#if 0
75        fprintf(stderr, "%s: tid %d, shared %x\n",
76                __FUNCTION__,
77                shared->tid,
78                (unsigned) shared);
79#endif
80
81        retval = entry_func(shared->arg);
82 
83        __pthread_keys_destroy();
84        tid = pthread_self();
85
86        if(tid != tls.attr.key)
87        {
88                fprintf(stderr,"tid %d, found %d\n", (uint32_t)tls.attr.key, (uint32_t)tid);
89                while(1);
90        }
91 
92        pthread_exit(retval);
93        return retval;          /* fake return */
94}
95
96static void __pthread_sigreturn(void)
97{
98        cpu_syscall(NULL,NULL,NULL,NULL,SYS_SIGRETURN);
99}
100
101
102int pthread_create (pthread_t *thread, pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
103{
104        register int retval;
105        pthread_attr_t _attr;
106        struct __shared_s *shared;
107
108        //___dmsg_ok = 1;
109
110        if(attr == NULL)
111        {
112                pthread_attr_init(&_attr);
113                attr = &_attr;
114        }
115
116        if(attr->stack_addr != NULL)
117        {
118                if((attr->sigstack_addr = valloc(1)) == NULL)
119                        return ENOMEM;
120
121                attr->sigstack_size = 2048;
122                shared = (struct __shared_s*)(attr->sigstack_addr + attr->sigstack_size + 64);
123        }
124        else
125        {
126                shared = malloc(sizeof(*shared));
127
128                if(shared == NULL)
129                        return ENOMEM;
130
131                //fprintf(stderr, "%s: shared @%x\n", __FUNCTION__, (unsigned) shared);
132        }
133 
134        shared->arg          = arg;
135        attr->entry_func     = (void*)__pthread_start;
136        attr->exit_func      = (void*)pthread_exit;
137        attr->sigreturn_func = (void*)__pthread_sigreturn;
138        attr->arg1           = (void*)start_routine;
139        attr->arg2           = shared;
140       
141        retval = (int)cpu_syscall((void*)thread,attr,NULL,NULL,SYS_CREATE);
142        return retval;
143}
144
145int pthread_join (pthread_t th, void **thread_return)
146{
147        register int retval;
148        retval = (int)cpu_syscall((void*)th,(void*)thread_return,NULL,NULL,SYS_JOIN);
149        return retval;
150}
151
152int pthread_detach(pthread_t thread)
153{
154        register int retval; 
155        retval = (int)cpu_syscall((void*)thread,NULL,NULL,NULL,SYS_DETACH);
156        return retval;
157}
158
159int pthread_migrate_np(pthread_attr_t *attr)
160{
161        register __pthread_tls_t *tls;
162        register int retval;
163        pthread_attr_t _attr;
164
165        if(attr == NULL)
166        {
167                pthread_attr_init(&_attr);
168                attr = &_attr;
169        }
170
171        retval = (int)cpu_syscall((void*)attr,NULL,NULL,NULL,SYS_MIGRATE);
172
173        if(retval == 0)
174        {
175                tls = cpu_get_tls();
176                cpu_syscall(&tls->attr,NULL,NULL,NULL,SYS_GETATTR);
177        }
178
179        return retval;
180}
181
182void pthread_yield (void)
183{
184        cpu_syscall(NULL,NULL,NULL,NULL,SYS_YIELD);
185}
186
187int pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
188{
189        if((once_control == NULL) || (init_routine == NULL))
190                return EINVAL;
191
192        if(cpu_spinlock_trylock(&once_control->lock))
193                return 0;
194
195        if(once_control->state != 0)
196        {
197                cpu_spinlock_unlock(&once_control->lock);
198                return 0;
199        }
200   
201        once_control->state = 1;
202        cpu_spinlock_unlock(&once_control->lock);
203
204        init_routine();
205
206        return 0;
207}
208
209pthread_t pthread_self (void)
210{
211        __pthread_tls_t *tls;
212
213        tls = cpu_get_tls();
214        assert((tls != NULL) && (tls->signature == __PTHREAD_OBJECT_CREATED));
215        return (pthread_t) tls->attr.key;
216}
217
218int pthread_equal (pthread_t thread1, pthread_t thread2)
219{
220        return thread1 == thread2;
221}
222
223int pthread_profiling_np(int cmd, pid_t pid, pthread_t tid)
224{
225        return (int)cpu_syscall((void*)cmd, (void*)pid, (void*)tid, NULL,SYS_PS);
226}
Note: See TracBrowser for help on using the repository browser.