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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since ab415f9 was ab415f9, checked in by Sebastian Huber <sebastian.huber@…>, on 10/09/13 at 06:56:49

Use extension to attach a struct thread to threads

Add test thread01.

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