blob: 184ac31e55035cb144255f6b184c1742ceb3075b [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{
45 event_init(&dpc_event, false, 0);
46
47 thread_resume(thread_create("dpc", &dpc_thread_routine, NULL, DPC_PRIORITY, DEFAULT_STACK_SIZE));
48}
49
50status_t dpc_queue(dpc_callback cb, void *arg, uint flags)
51{
52 struct dpc *dpc;
53
54 dpc = malloc(sizeof(struct dpc));
55
56 dpc->cb = cb;
57 dpc->arg = arg;
58 enter_critical_section();
59 list_add_tail(&dpc_list, &dpc->node);
60 event_signal(&dpc_event, (flags & DPC_FLAG_NORESCHED) ? false : true);
61 exit_critical_section();
62
63 return NO_ERROR;
64}
65
66static int dpc_thread_routine(void *arg)
67{
68 for (;;) {
69 event_wait(&dpc_event);
70
71 enter_critical_section();
72 struct dpc *dpc = list_remove_head_type(&dpc_list, struct dpc, node);
73 if (!dpc)
74 event_unsignal(&dpc_event);
75 exit_critical_section();
76
77 if (dpc) {
78// dprintf("dpc calling %p, arg %p\n", dpc->cb, dpc->arg);
79 dpc->cb(dpc->arg);
80
81 free(dpc);
82 }
83 }
Travis Geiselbrecht887061f2008-09-05 01:47:07 -070084
85 return 0;
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070086}
87
88