BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / bus_managers / scsi / dpc.cpp
blobc6ae926db5d42f099c0b3168d7ab597f3e5f477c
1 /*
2 ** Copyright 2002/03, Thomas Kurschel. All rights reserved.
3 ** Distributed under the terms of the Haiku License.
4 */
6 /*
7 Part of Open SCSI bus manager
9 DPC handling (deferred procedure calls).
11 DPC are executed by the service thread of the bus
12 (see busses.c).
15 #include "scsi_internal.h"
17 #include <string.h>
18 #include <stdlib.h>
21 status_t
22 scsi_alloc_dpc(scsi_dpc_info **dpc)
24 SHOW_FLOW0(3, "");
26 *dpc = (scsi_dpc_info *)malloc(sizeof(scsi_dpc_info));
27 if (*dpc == NULL)
28 return B_NO_MEMORY;
30 memset(*dpc, 0, sizeof(scsi_dpc_info));
31 return B_OK;
35 status_t
36 scsi_free_dpc(scsi_dpc_info *dpc)
38 SHOW_FLOW0(3, "");
40 free(dpc);
41 return B_OK;
45 status_t
46 scsi_schedule_dpc(scsi_bus_info *bus, scsi_dpc_info *dpc, /*int flags,*/
47 void (*func)(void *arg), void *arg)
49 SHOW_FLOW(3, "bus=%p, dpc=%p", bus, dpc);
50 acquire_spinlock_irq(&bus->dpc_lock);
52 dpc->func = func;
53 dpc->arg = arg;
55 if (!dpc->registered) {
56 dpc->registered = true;
57 dpc->next = bus->dpc_list;
58 bus->dpc_list = dpc;
59 } else
60 SHOW_FLOW0(3, "already registered - ignored");
62 release_spinlock_irq(&bus->dpc_lock);
64 // this is called in IRQ context, so scheduler is not allowed
65 release_sem_etc(bus->start_service, 1, B_DO_NOT_RESCHEDULE);
66 return B_OK;
70 /** execute pending DPCs */
72 bool
73 scsi_check_exec_dpc(scsi_bus_info *bus)
75 SHOW_FLOW(3, "bus=%p, dpc_list=%p", bus, bus->dpc_list);
76 acquire_spinlock_irq(&bus->dpc_lock);
78 if (bus->dpc_list) {
79 scsi_dpc_info *dpc;
80 void (*dpc_func)(void *);
81 void *dpc_arg;
83 dpc = bus->dpc_list;
84 bus->dpc_list = dpc->next;
86 dpc_func = dpc->func;
87 dpc_arg = dpc->arg;
88 dpc->registered = false;
90 release_spinlock_irq(&bus->dpc_lock);
92 dpc_func(dpc_arg);
93 return true;
96 release_spinlock_irq(&bus->dpc_lock);
97 return false;