1 /* This file contains a simple message queue implementation to support both
2 * the singlethread and the multithreaded driver implementation.
5 * Oct 27, 2011 rewritten to use sys/queue.h (D.C. van Moolenbroek)
6 * Aug 27, 2011 integrated into libblockdriver (A. Welzel)
9 #include <minix/blockdriver_mt.h>
10 #include <sys/queue.h>
21 STAILQ_ENTRY(mq_cell
) next
;
24 static struct mq_cell pool
[MQ_SIZE
];
25 static STAILQ_HEAD(queue
, mq_cell
) queue
[MAX_DEVICES
];
26 static STAILQ_HEAD(free_list
, mq_cell
) free_list
;
28 /*===========================================================================*
30 *===========================================================================*/
33 /* Initialize the message queues and message cells.
37 STAILQ_INIT(&free_list
);
39 for (i
= 0; i
< MAX_DEVICES
; i
++)
40 STAILQ_INIT(&queue
[i
]);
42 for (i
= 0; i
< MQ_SIZE
; i
++)
43 STAILQ_INSERT_HEAD(&free_list
, &pool
[i
], next
);
46 /*===========================================================================*
48 *===========================================================================*/
49 int mq_enqueue(device_id_t device_id
, const message
*mess
,
52 /* Add a message, including its IPC status, to the message queue of a device.
53 * Return TRUE iff the message was added successfully.
57 assert(device_id
>= 0 && device_id
< MAX_DEVICES
);
59 if (STAILQ_EMPTY(&free_list
))
62 cell
= STAILQ_FIRST(&free_list
);
63 STAILQ_REMOVE_HEAD(&free_list
, next
);
66 cell
->ipc_status
= ipc_status
;
68 STAILQ_INSERT_TAIL(&queue
[device_id
], cell
, next
);
73 /*===========================================================================*
75 *===========================================================================*/
76 int mq_dequeue(device_id_t device_id
, message
*mess
, int *ipc_status
)
78 /* Return and remove a message, including its IPC status, from the message
79 * queue of a thread. Return TRUE iff a message was available.
83 assert(device_id
>= 0 && device_id
< MAX_DEVICES
);
85 if (STAILQ_EMPTY(&queue
[device_id
]))
88 cell
= STAILQ_FIRST(&queue
[device_id
]);
89 STAILQ_REMOVE_HEAD(&queue
[device_id
], next
);
92 *ipc_status
= cell
->ipc_status
;
94 STAILQ_INSERT_HEAD(&free_list
, cell
, next
);