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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since a271e91 was a271e91, checked in by Sebastian Huber <sebastian.huber@…>, on 04/17/14 at 07:45:03

Update due to chain API changes

  • Property mode set to 100644
File size: 8.5 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-kernel-space.h>
41#include <machine/rtems-bsd-thread.h>
42#include <machine/rtems-bsd-support.h>
43
44#include <rtems/bsd/sys/param.h>
45#include <rtems/bsd/sys/types.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/proc.h>
49#include <sys/kthread.h>
50#include <sys/malloc.h>
51#include <sys/selinfo.h>
52
53#include <rtems/score/objectimpl.h>
54#include <rtems/score/statesimpl.h>
55#include <rtems/score/threaddispatch.h>
56#include <rtems/score/thread.h>
57#include <rtems/score/threadimpl.h>
58#include <rtems/score/threadqimpl.h>
59
60RTEMS_CHAIN_DEFINE_EMPTY(rtems_bsd_thread_chain);
61
62static size_t rtems_bsd_extension_index;
63
64static CHAIN_DEFINE_EMPTY(rtems_bsd_thread_delay_start_chain);
65
66static bool rtems_bsd_thread_ready_to_start;
67
68struct thread *
69rtems_bsd_get_thread(const Thread_Control *thread)
70{
71        return thread->extensions[rtems_bsd_extension_index];
72}
73
74static Thread_Control *
75rtems_bsd_get_thread_by_id(rtems_id task_id)
76{
77        Thread_Control *thread;
78        Objects_Locations location;
79
80        thread = _Thread_Get(task_id, &location);
81        switch (location) {
82                case OBJECTS_LOCAL:
83                        _Objects_Put(&thread->Object);
84                        break;
85#if defined(RTEMS_MULTIPROCESSING)
86                case OBJECTS_REMOTE:
87                        _Thread_Dispatch();
88                        thread = NULL;
89                        break;
90#endif
91                default:
92                        thread = NULL;
93                        break;
94        }
95
96        return thread;
97}
98
99struct thread *
100rtems_bsd_thread_create(Thread_Control *thread, int wait)
101{
102        struct thread *td = malloc(sizeof(*td), M_TEMP, M_ZERO | wait);
103        struct sleepqueue *sq = malloc(sizeof(*sq), M_TEMP, wait);
104
105        if (td != NULL && sq != NULL) {
106                td->td_thread = thread;
107                td->td_sleepqueue = sq;
108
109                LIST_INIT(&sq->sq_free);
110
111                _Thread_queue_Initialize(
112                        &sq->sq_blocked,
113                        THREAD_QUEUE_DISCIPLINE_PRIORITY,
114                        STATES_WAITING_FOR_BSD_WAKEUP,
115                        EWOULDBLOCK
116                );
117        } else {
118                free(td, M_TEMP);
119                free(sq, M_TEMP);
120                td = NULL;
121        }
122
123        thread->extensions[rtems_bsd_extension_index] = td;
124
125        return td;
126}
127
128static struct thread *
129rtems_bsd_get_curthread(int wait)
130{
131        Thread_Control *executing = _Thread_Get_executing();
132        struct thread *td = rtems_bsd_get_thread(executing);
133
134        if (td == NULL) {
135                td = rtems_bsd_thread_create(executing, wait);
136        }
137
138        return td;
139}
140
141struct thread *
142rtems_bsd_get_curthread_or_wait_forever(void)
143{
144        return rtems_bsd_get_curthread(M_WAITOK);
145}
146
147struct thread *
148rtems_bsd_get_curthread_or_null(void)
149{
150        return rtems_bsd_get_curthread(0);
151}
152
153static bool
154rtems_bsd_is_bsd_thread(Thread_Control *thread)
155{
156        return thread->Object.name.name_u32 == BSD_TASK_NAME;
157}
158
159static bool
160rtems_bsd_extension_thread_create(
161        Thread_Control *executing,
162        Thread_Control *created
163)
164{
165        bool ok = true;
166
167        if (rtems_bsd_is_bsd_thread(created)) {
168                struct thread *td = rtems_bsd_thread_create(created, 0);
169
170                ok = td != NULL;
171                if (ok) {
172                        rtems_chain_append(&rtems_bsd_thread_chain, &td->td_node);
173                }
174        }
175
176        return ok;
177}
178
179static void
180rtems_bsd_extension_thread_delete(
181        Thread_Control *executing,
182        Thread_Control *deleted
183)
184{
185        struct thread *td = rtems_bsd_get_thread(deleted);
186
187        if (td != NULL) {
188                seltdfini(td);
189
190                if (rtems_bsd_is_bsd_thread(deleted)) {
191                        rtems_chain_extract(&td->td_node);
192                }
193
194                free(td->td_sleepqueue, M_TEMP);
195                free(td, M_TEMP);
196        }
197}
198
199static const rtems_extensions_table rtems_bsd_extensions = {
200        .thread_create = rtems_bsd_extension_thread_create,
201        .thread_delete = rtems_bsd_extension_thread_delete
202};
203
204static void
205rtems_bsd_threads_init_early(void *arg)
206{
207        rtems_id ext_id;
208        rtems_status_code sc;
209
210        (void) arg;
211
212        sc = rtems_extension_create(
213                BSD_TASK_NAME,
214                &rtems_bsd_extensions,
215                &ext_id
216        );
217        if (sc != RTEMS_SUCCESSFUL) {
218                BSD_PANIC("cannot create extension");
219        }
220
221        rtems_bsd_extension_index = rtems_object_id_get_index(ext_id);
222}
223
224static void
225rtems_bsd_threads_init_late(void *arg)
226{
227        Chain_Control *chain = &rtems_bsd_thread_delay_start_chain;
228        Chain_Node *node;
229
230        (void) arg;
231
232        while ((node = _Chain_Get_unprotected(chain)) != NULL) {
233                Thread_Control *thread = (Thread_Control *) node;
234                rtems_status_code sc;
235
236                sc = rtems_task_start(thread->Object.id, (rtems_task_entry)
237                    thread->Start.entry_point, thread->Start.numeric_argument);
238                BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
239        }
240
241        rtems_bsd_thread_ready_to_start = true;
242}
243
244SYSINIT(rtems_bsd_threads_early, SI_SUB_INTRINSIC, SI_ORDER_ANY,
245    rtems_bsd_threads_init_early, NULL);
246
247SYSINIT(rtems_bsd_threads_late, SI_SUB_RUN_SCHEDULER, SI_ORDER_ANY,
248    rtems_bsd_threads_init_late, NULL);
249
250static int
251rtems_bsd_thread_start(struct thread **td_ptr, void (*func)(void *), void *arg,
252    int flags, int pages, const char *fmt, va_list ap)
253{
254        int eno = 0;
255        rtems_status_code sc;
256        rtems_id task_id;
257
258        BSD_ASSERT(pages >= 0);
259
260        sc = rtems_task_create(
261                BSD_TASK_NAME,
262                BSD_TASK_PRIORITY_NORMAL,
263                BSD_MINIMUM_TASK_STACK_SIZE + (size_t) pages * PAGE_SIZE,
264                RTEMS_DEFAULT_MODES,
265                RTEMS_DEFAULT_ATTRIBUTES,
266                &task_id
267        );
268        if (sc == RTEMS_SUCCESSFUL) {
269                Thread_Control *thread = rtems_bsd_get_thread_by_id(task_id);
270                struct thread *td;
271
272                BSD_ASSERT(thread != NULL);
273
274                td = rtems_bsd_get_thread(thread);
275                BSD_ASSERT(td != NULL);
276
277                vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
278
279                if (rtems_bsd_thread_ready_to_start) {
280                        sc = rtems_task_start(task_id, (rtems_task_entry) func,
281                            (rtems_task_argument) arg);
282                        BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
283                } else {
284                        thread->Start.entry_point = (Thread_Entry) func;
285                        thread->Start.numeric_argument =
286                            (Thread_Entry_numeric_type) arg;
287                        _Chain_Append_unprotected(
288                            &rtems_bsd_thread_delay_start_chain,
289                            &thread->Object.Node);
290                }
291
292                if (td_ptr != NULL) {
293                        *td_ptr = td;
294                }
295        } else {
296                eno = ENOMEM;
297        }
298
299        return eno;
300}
301
302static __dead2 void
303rtems_bsd_thread_delete(void)
304{
305        rtems_task_delete(RTEMS_SELF);
306        BSD_PANIC("delete self failed");
307}
308
309void
310kproc_start(const void *udata)
311{
312        const struct kproc_desc *pd = udata;
313        int eno = kproc_create((void (*)(void *))pd->func, NULL, pd->global_procpp, 0, 0, "%s", pd->arg0);
314
315        BSD_ASSERT(eno == 0);
316}
317
318int
319kproc_create(void (*func)(void *), void *arg, struct proc **newpp, int flags, int pages, const char *fmt, ...)
320{
321        int eno = 0;
322        va_list ap;
323
324        va_start(ap, fmt);
325        eno = rtems_bsd_thread_start(newpp, func, arg, flags, pages, fmt, ap);
326        va_end(ap);
327
328        return eno;
329}
330
331void
332kproc_exit(int ecode)
333{
334        rtems_bsd_thread_delete();
335}
336
337void
338kthread_start(const void *udata)
339{
340        const struct kthread_desc *td = udata;
341        int eno = kthread_add((void (*)(void *)) td->func, NULL, NULL, td->global_threadpp, 0, 0, "%s", td->arg0);
342
343        BSD_ASSERT(eno == 0);
344}
345
346int
347kthread_add(void (*func)(void *), void *arg, struct proc *p, struct thread **newtdp, int flags, int pages, const char *fmt, ...)
348{
349        int eno = 0;
350        va_list ap;
351
352        va_start(ap, fmt);
353        eno = rtems_bsd_thread_start(newtdp, func, arg, flags, pages, fmt, ap);
354        va_end(ap);
355
356        return eno;
357}
358
359void
360kthread_exit(void)
361{
362        rtems_bsd_thread_delete();
363}
364
365int
366kproc_kthread_add(void (*func)(void *), void *arg, struct proc **procptr, struct thread **tdptr, int flags, int pages, const char * procname, const char *fmt, ...)
367{
368        int eno = 0;
369        va_list ap;
370
371        va_start(ap, fmt);
372        eno = rtems_bsd_thread_start(tdptr, func, arg, flags, pages, fmt, ap);
373        va_end(ap);
374
375        return eno;
376}
Note: See TracBrowser for help on using the repository browser.