blob: dd70b96287727157173ee4b0c7ed1a9f80b3e1a8 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <debug.h>
24#include <list.h>
25#include <malloc.h>
26#include <err.h>
27#include <kernel/dpc.h>
28#include <kernel/thread.h>
29#include <kernel/event.h>
30
31struct dpc {
32 struct list_node node;
33
34 dpc_callback cb;
35 void *arg;
36};
37
38static struct list_node dpc_list = LIST_INITIAL_VALUE(dpc_list);
39static event_t dpc_event;
40
41static int dpc_thread_routine(void *arg);
42
43void dpc_init(void)
44{
Parth Dixit826f5752016-08-16 17:23:32 +053045 thread_t *thr;
46
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070047 event_init(&dpc_event, false, 0);
48
Parth Dixit826f5752016-08-16 17:23:32 +053049 thr = thread_create("dpc", &dpc_thread_routine, NULL, DPC_PRIORITY, DEFAULT_STACK_SIZE);
50 if (!thr)
51 {
52 panic("failed to create dpc thread\n");
53 }
54 thread_resume(thr);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070055}
56
57status_t dpc_queue(dpc_callback cb, void *arg, uint flags)
58{
59 struct dpc *dpc;
60
61 dpc = malloc(sizeof(struct dpc));
62
63 dpc->cb = cb;
64 dpc->arg = arg;
65 enter_critical_section();
66 list_add_tail(&dpc_list, &dpc->node);
67 event_signal(&dpc_event, (flags & DPC_FLAG_NORESCHED) ? false : true);
68 exit_critical_section();
69
70 return NO_ERROR;
71}
72
73static int dpc_thread_routine(void *arg)
74{
75 for (;;) {
76 event_wait(&dpc_event);
77
78 enter_critical_section();
79 struct dpc *dpc = list_remove_head_type(&dpc_list, struct dpc, node);
80 if (!dpc)
81 event_unsignal(&dpc_event);
82 exit_critical_section();
83
84 if (dpc) {
85// dprintf("dpc calling %p, arg %p\n", dpc->cb, dpc->arg);
86 dpc->cb(dpc->arg);
87
88 free(dpc);
89 }
90 }
Travis Geiselbrecht887061f2008-09-05 01:47:07 -070091
92 return 0;
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070093}
94
95