81 lines
2.0 KiB
C
81 lines
2.0 KiB
C
#include <stdint.h>
|
|
#include <exec/list.h>
|
|
#include <exec/sync.h>
|
|
#include <exec/task.h>
|
|
#include <exec/mem.h>
|
|
#include <exec/debcon.h>
|
|
#include <exec/port.h>
|
|
|
|
struct List ports;
|
|
|
|
void PortsInit() {
|
|
NewList(&ports, LP_BLOCK);
|
|
}
|
|
|
|
void NewPort(struct Port *port, uint8_t flags) {
|
|
port->flags = flags;
|
|
NewList(&port->messages, LP_BLOCK);
|
|
NewMutex(&port->mutex, SF_BLOCK);
|
|
}
|
|
|
|
void PutMsg(struct Port *port, struct Message *msg) {
|
|
Acquire(&port->mutex);
|
|
AddTail(&port->messages, &msg->node);
|
|
if (port->signal_task) {
|
|
Signal(port->signal_task, SIGNAL(port->signal_bit));
|
|
}
|
|
Release(&port->mutex);
|
|
}
|
|
|
|
struct Message *GetMsg(struct Port *port) {
|
|
return (struct Message *)RemHead(&port->messages);
|
|
}
|
|
|
|
struct Message *WaitPort(struct Port *port) {
|
|
for (;;) {
|
|
Acquire(&port->mutex);
|
|
struct Message *msg = (struct Message *)RemHead(&port->messages);
|
|
if (msg) {
|
|
Release(&port->mutex);
|
|
return msg;
|
|
}
|
|
if (port->signal_task != ThisTask()) {
|
|
port->signal_task = ThisTask();
|
|
port->signal_bit = AllocSignal(port->signal_bit);
|
|
DebconPrint("Warning: WaitPort changing port signal task/bit\n");
|
|
}
|
|
Release(&port->mutex);
|
|
Wait(SIGNAL(port->signal_bit));
|
|
}
|
|
}
|
|
|
|
void AssignPort(struct Port *port) {
|
|
Acquire(&port->mutex);
|
|
port->signal_task = ThisTask();
|
|
port->signal_bit = SIGNAL(AllocSignal(port->signal_bit));
|
|
Release(&port->mutex);
|
|
}
|
|
|
|
void AddPort(struct Port *port) {
|
|
Enqueue(&ports, (struct Node *)&port->node);
|
|
}
|
|
|
|
void ReplyMsg(struct Message *msg) {
|
|
if (msg->reply_port) {
|
|
msg->node.type = NT_REPLYMSG;
|
|
PutMsg(msg->reply_port, msg);
|
|
} else {
|
|
msg->node.type = NT_FREEMSG;
|
|
}
|
|
}
|
|
|
|
struct Port *CreatePort(char *name, int pri) {
|
|
struct Port *port = (struct Port *)AllocMem(sizeof(struct Port));
|
|
NewPort(port, 0);
|
|
port->node.name = name;
|
|
port->node.priority = pri;
|
|
if (name) {
|
|
AddPort(port);
|
|
}
|
|
return port;
|
|
} |