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

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

SLEEP(8): New implementation

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