source: rtems-libbsd/rtemsbsd/rtems/rtems-bsd-thread.c @ 69b29a0

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 69b29a0 was 69b29a0, checked in by Sebastian Huber <sebastian.huber@…>, on 10/10/13 at 14:11:02

Disable user credentials and use default values

  • Property mode set to 100644
File size: 6.9 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_bsd_rtems
5 *
6 * @brief TODO.
7 */
8
9/*
10 * Copyright (c) 2009-2013 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40#include <machine/rtems-bsd-config.h>
41#include <machine/rtems-bsd-thread.h>
42
43#include <rtems/bsd/sys/param.h>
44#include <rtems/bsd/sys/types.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/proc.h>
48#include <sys/kthread.h>
49#include <sys/malloc.h>
50#include <sys/selinfo.h>
51
52#include <rtems/score/threadimpl.h>
53#include <rtems/score/objectimpl.h>
54
55RTEMS_CHAIN_DEFINE_EMPTY(rtems_bsd_thread_chain);
56
57static size_t rtems_bsd_extension_index;
58
59struct thread *
60rtems_bsd_get_thread(const Thread_Control *thread)
61{
62        return thread->extensions[rtems_bsd_extension_index];
63}
64
65static struct thread *
66rtems_bsd_get_thread_by_id(rtems_id task_id)
67{
68        struct thread *td = NULL;
69        Thread_Control *thread;
70        Objects_Locations location;
71
72        thread = _Thread_Get(task_id, &location);
73        switch (location) {
74                case OBJECTS_LOCAL:
75                        td = rtems_bsd_get_thread(thread);
76                        _Objects_Put(&thread->Object);
77                        break;
78#if defined(RTEMS_MULTIPROCESSING)
79                case OBJECTS_REMOTE:
80                        _Thread_Dispatch();
81                        break;
82#endif
83                default:
84                        break;
85        }
86
87        return td;
88}
89
90struct thread *
91rtems_bsd_thread_create(Thread_Control *thread, int wait)
92{
93        struct thread *td = malloc(sizeof(*td), M_TEMP, M_ZERO | wait);
94
95        if (td != NULL) {
96                td->td_thread = thread;
97        }
98
99        thread->extensions[rtems_bsd_extension_index] = td;
100
101        return td;
102}
103
104static struct thread *
105rtems_bsd_get_curthread(int wait)
106{
107        Thread_Control *executing = _Thread_Get_executing();
108        struct thread *td = rtems_bsd_get_thread(executing);
109
110        if (td == NULL) {
111                td = rtems_bsd_thread_create(executing, wait);
112        }
113
114        return td;
115}
116
117struct thread *
118rtems_bsd_get_curthread_or_wait_forever(void)
119{
120        return rtems_bsd_get_curthread(M_WAITOK);
121}
122
123struct thread *
124rtems_bsd_get_curthread_or_null(void)
125{
126        return rtems_bsd_get_curthread(0);
127}
128
129static bool
130rtems_bsd_is_bsd_thread(Thread_Control *thread)
131{
132        return thread->Object.name.name_u32 == BSD_TASK_NAME;
133}
134
135static bool
136rtems_bsd_extension_thread_create(
137        Thread_Control *executing,
138        Thread_Control *created
139)
140{
141        bool ok = true;
142
143        if (rtems_bsd_is_bsd_thread(created)) {
144                struct thread *td = rtems_bsd_thread_create(created, 0);
145
146                ok = td != NULL;
147                if (ok) {
148                        rtems_chain_append(&rtems_bsd_thread_chain, &td->td_node);
149                }
150        }
151
152        return ok;
153}
154
155static void
156rtems_bsd_extension_thread_delete(
157        Thread_Control *executing,
158        Thread_Control *deleted
159)
160{
161        struct thread *td = rtems_bsd_get_thread(deleted);
162
163        if (td != NULL) {
164                seltdfini(td);
165
166                if (rtems_bsd_is_bsd_thread(deleted)) {
167                        rtems_chain_explicit_extract(&rtems_bsd_thread_chain, &td->td_node);
168                }
169
170                free(td, M_TEMP);
171        }
172}
173
174static const rtems_extensions_table rtems_bsd_extensions = {
175        .thread_create = rtems_bsd_extension_thread_create,
176        .thread_delete = rtems_bsd_extension_thread_delete
177};
178
179static void
180rtems_bsd_threads_init(void *arg __unused)
181{
182        rtems_id ext_id;
183        rtems_status_code sc;
184
185        sc = rtems_extension_create(
186                BSD_TASK_NAME,
187                &rtems_bsd_extensions,
188                &ext_id
189        );
190        if (sc != RTEMS_SUCCESSFUL) {
191                BSD_PANIC("cannot create extension");
192        }
193
194        rtems_bsd_extension_index = rtems_object_id_get_index(ext_id);
195}
196
197SYSINIT(rtems_bsd_threads, SI_SUB_INTRINSIC, SI_ORDER_ANY, rtems_bsd_threads_init, NULL);
198
199static int
200rtems_bsd_thread_start(struct thread **td_ptr, void (*func)(void *), void *arg, int flags, int pages, const char *fmt, va_list ap)
201{
202        int eno = 0;
203        rtems_status_code sc;
204        rtems_id task_id;
205
206        BSD_ASSERT(pages >= 0);
207
208        sc = rtems_task_create(
209                BSD_TASK_NAME,
210                BSD_TASK_PRIORITY_NORMAL,
211                BSD_MINIMUM_TASK_STACK_SIZE + (size_t) pages * PAGE_SIZE,
212                RTEMS_DEFAULT_ATTRIBUTES,
213                RTEMS_DEFAULT_ATTRIBUTES,
214                &task_id
215        );
216        if (sc == RTEMS_SUCCESSFUL) {
217                struct thread *td = rtems_bsd_get_thread_by_id(task_id);
218
219                BSD_ASSERT(td != NULL);
220
221                vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
222
223                sc = rtems_task_start(task_id, (rtems_task_entry) func, (rtems_task_argument) arg);
224                BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
225
226                if (td_ptr != NULL) {
227                        *td_ptr = td;
228                }
229        } else {
230                eno = ENOMEM;
231        }
232
233        return eno;
234}
235
236static __dead2 void
237rtems_bsd_thread_delete(void)
238{
239        rtems_task_delete(RTEMS_SELF);
240        BSD_PANIC("delete self failed");
241}
242
243void
244kproc_start(const void *udata)
245{
246        const struct kproc_desc *pd = udata;
247        int eno = kproc_create((void (*)(void *))pd->func, NULL, pd->global_procpp, 0, 0, "%s", pd->arg0);
248
249        BSD_ASSERT(eno == 0);
250}
251
252int
253kproc_create(void (*func)(void *), void *arg, struct proc **newpp, int flags, int pages, const char *fmt, ...)
254{
255        int eno = 0;
256        va_list ap;
257
258        va_start(ap, fmt);
259        eno = rtems_bsd_thread_start(newpp, func, arg, flags, pages, fmt, ap);
260        va_end(ap);
261
262        return eno;
263}
264
265void
266kproc_exit(int ecode)
267{
268        rtems_bsd_thread_delete();
269}
270
271void
272kthread_start(const void *udata)
273{
274        const struct kthread_desc *td = udata;
275        int eno = kthread_add((void (*)(void *)) td->func, NULL, NULL, td->global_threadpp, 0, 0, "%s", td->arg0);
276
277        BSD_ASSERT(eno == 0);
278}
279
280int
281kthread_add(void (*func)(void *), void *arg, struct proc *p, struct thread **newtdp, int flags, int pages, const char *fmt, ...)
282{
283        int eno = 0;
284        va_list ap;
285
286        va_start(ap, fmt);
287        eno = rtems_bsd_thread_start(newtdp, func, arg, flags, pages, fmt, ap);
288        va_end(ap);
289
290        return eno;
291}
292
293void
294kthread_exit(void)
295{
296        rtems_bsd_thread_delete();
297}
298
299int
300kproc_kthread_add(void (*func)(void *), void *arg, struct proc **procptr, struct thread **tdptr, int flags, int pages, const char * procname, const char *fmt, ...)
301{
302        int eno = 0;
303        va_list ap;
304
305        va_start(ap, fmt);
306        eno = rtems_bsd_thread_start(tdptr, func, arg, flags, pages, fmt, ap);
307        va_end(ap);
308
309        return eno;
310}
Note: See TracBrowser for help on using the repository browser.