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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 6604dc8 was 6604dc8, checked in by Sebastian Huber <sebastian.huber@…>, on 09/25/14 at 08:48:14

Delete rtems_bsd_thread_chain

Rely on RTEMS object registration for threads.

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