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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since e2281af was 150d4d6, checked in by Sebastian Huber <sebastian.huber@…>, on 10/24/13 at 09:11:32

Move content to new <machine/rtems-bsd-support.h>

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