/** * Initialize TX and RX message queues. * * @size: Size of the queue, applies to both TX and RX. */ void mq_init(int size) { /* Allocate memory for both TX and RX queues at once. */ txq = malloc(sizeof(struct mq_cb) * 2); if (!txq) { printf("Not enough memory!\n"); return; } rxq = txq + 1; txq->frames = malloc(sizeof(struct can_frame) * size * 2); if (!txq->frames) { printf("Not enough memory!\n"); free(txq); return; } rxq->frames = txq->frames + size; /* Initialize queues. */ txq->head = 0; txq->tail = 0; txq->size = size; sync_spinlock_init(&txq->lock); rxq->head = 0; rxq->tail = 0; rxq->size = size; sync_spinlock_init(&rxq->lock); }
int run(void) { const char *name = get_instance_name(); printf("%s: Waiting for client init...\n", name); setup_wait(); printf("%s: Initialising lock...\n", name); sync_spinlock_init((sync_spinlock_t*)&outgoing->lock); printf("%s: Notifying receiver...\n", name); init_emit(); printf("%s: Waiting for data...\n", name); int received = 0; while (!received) { sync_spinlock_lock((sync_spinlock_t*)&incoming->lock); if (incoming->full) { sync_spinlock_lock((sync_spinlock_t*)&outgoing->lock); strcpy((char*)outgoing->data, (char*)incoming->data); outgoing->full = 1; sync_spinlock_unlock((sync_spinlock_t*)&outgoing->lock); received = 1; } sync_spinlock_unlock((sync_spinlock_t*)&incoming->lock); } printf("%s: Done.\n", name); return 0; }
int run(void) { const char *s = "hello world"; const char *name = get_instance_name(); printf("%s: Initialising lock...\n", name); sync_spinlock_init((sync_spinlock_t*)&sock->lock); printf("%s: Notifying transport...\n", name); init_emit(); printf("%s: Writing \"%s\"...\n", name, s); sync_spinlock_lock((sync_spinlock_t*)&sock->lock); strcpy((char*)sock->data, s); sock->full = 1; sync_spinlock_unlock((sync_spinlock_t*)&sock->lock); printf("%s: Done.\n", name); return 0; }