//===================================================================== // // VUser.c Date: 2004/12/13 // // Copyright (c) 2004-2010 Simon Southwell. // // This file is part of VProc. // // $Id$ // $Source$ // //===================================================================== // // Top level VP user thread routines. Sets up connection to // queue and calls relevant user function for node number. // //===================================================================== #include #ifdef _WIN32 #include #else #include #endif #include "VProc.h" #include "VUser.h" static void VUserInit (int node); static void msleep(int ms) { #ifdef _WIN32 Sleep(ms); #else usleep(ms * 1000); #endif } ///////////////////////////////////////////////////////////// // Entry point for new user process. Creates a new thread // calling VUserInit(). // int VUser (int node) { pthread_t thread; pthread_attr_t tattr; int status; int idx, jdx; debug_io_printf("VUser(): node %d\n", node); // Interrupt table initialisation for (jdx = 0; jdx < 8; jdx++) ns[node]->VInt_table[jdx] = NULL; ns[node]->VUserCB = NULL; debug_io_printf("VUser(): initialised interrupt table node %d\n", node); //pthread_attr_init(&tattr); //pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM); // Set off the user code thread if (status = pthread_create(&thread, NULL, (pThreadFunc_t)VUserInit, (void *)node)) { debug_io_printf("VUser(): pthread_create returned %d\n", status); return 1; } debug_io_printf("VUser(): spawned user thread for node %d\n", node); // Detach the thread (we won't wait for it) //pthread_detach(thread); //debug_io_printf("VUser(): detached user thread for node %d\n", node); return 0; } ///////////////////////////////////////////////////////////// // New thread initialisation procedure. Synchronises with // simulation before calling user procedure. // static void VUserInit (int node) { handle_t hdl; pVUserMain_t VUserMain_func; char funcname[DEFAULT_STR_BUF_SIZE]; int status; msleep(node*10); debug_io_printf("VUserInit(%d)\n", node); // Get function pointer of user entry routine sprintf(funcname, "%s%d", "VUserMain", node); if ((VUserMain_func = (pVUserMain_t) dlsym(RTLD_DEFAULT, funcname)) == NULL) { io_printf("***Error: failed to find user code symbol %s (VUserInit)\n", funcname); exit(1); } debug_io_printf("VUserInit(): got user function (%s) for node %d (%x)\n", funcname, node, VUserMain_func); // Wait for first message from simulator debug_io_printf("VUserInit(): waiting for first message semaphore rcv[%d]\n", node); if ((status = sem_wait(&(ns[node]->rcv))) == -1) { io_printf("***Error: bad sem_post status (%d) on node %d (VUserInit)\n", status, node); exit(1); } debug_io_printf("VUserInit(): calling user code for node %d\n", node); // Call user program debug_io_printf("VUserInit(): calling VUserMain%d\n", node); VUserMain_func(); } ///////////////////////////////////////////////////////////// // Message exchange routine. Handles all messages to and from // simulation process (apart from initialisation. Each sent // message has a reply. Interrupt messages require that // the original IO message reply is waited for again. // static void VExch (psend_buf_t psbuf, prcv_buf_t prbuf, uint32 node) { int status; // Send message to simulator ns[node]->send_buf = *psbuf; debug_io_printf("VExch(): setting snd[%d] semaphore\n", node); if ((status = sem_post(&(ns[node]->snd))) == -1) { io_printf("***Error: bad sem_post status (%d) on node %d (VExch)\n", status, node); exit(1); } do { // Wait for response message from simulator debug_io_printf("VExch(): waiting for rcv[%d] semaphore\n", node); sem_wait(&(ns[node]->rcv)); *prbuf = ns[node]->rcv_buf; // Check if this is an interrupt if (prbuf->interrupt > 0) { debug_io_printf("VExch(): node %d processing interrupt (%d)\n", node, prbuf->interrupt); if (prbuf->interrupt >= 8) { io_printf("***Error: invalid interrupt level %d (VExch)\n", prbuf->interrupt); exit(1); } if (ns[node]->VInt_table[prbuf->interrupt] == NULL) { io_printf("***Error: interrupt to unregistered level %d on node %d (VExch)\n", prbuf->interrupt, node); exit(1); } // Call user registered interrupt function psbuf->ticks = (*(ns[node]->VInt_table[prbuf->interrupt]))(); ns[node]->send_buf = *psbuf; debug_io_printf("VExch(): interrupt send_buf[node].ticks = %d\n", ns[node]->send_buf.ticks); // Send new message to simulation debug_io_printf("VExch(): setting snd[%d] semaphore (interrupt)\n", node); if ((status = sem_post(&(ns[node]->snd))) == -1) { io_printf("***Error: bad sem_post status (%d) on node %d (VExch)\n", status, node); exit(1); } } // If the response was an interrupt, go back and wait for IO message response. // (This could be in the same cycle as the interrupt) } while (prbuf->interrupt > 0); debug_io_printf("VExch(): returning to user code from node %d\n", node); } ///////////////////////////////////////////////////////////// // Invokes a write message exchange // int VWrite (unsigned int Addr, unsigned int Data, int Delta, uint32 node) { rcv_buf_t rbuf; send_buf_t sbuf; sbuf.addr = Addr; sbuf.data_out = Data; sbuf.rw = V_WRITE; sbuf.ticks = Delta ? DELTA_CYCLE : 0; VExch(&sbuf, &rbuf, node); return rbuf.data_in ; } ///////////////////////////////////////////////////////////// // Invokes a read message exchange // int VRead (unsigned int Addr, unsigned int *rdata, int Delta, uint32 node) { rcv_buf_t rbuf; send_buf_t sbuf; sbuf.addr = Addr; sbuf.data_out = 0; sbuf.rw = V_READ; sbuf.ticks = Delta ? DELTA_CYCLE : 0; VExch(&sbuf, &rbuf, node); *rdata = rbuf.data_in; return 0; } ///////////////////////////////////////////////////////////// // Invokes a array message exchange // int VSwap (unsigned int Addr, void *data, int Delta, uint32 node) { rcv_buf_t rbuf; send_buf_t sbuf; sbuf.addr = Addr; sbuf.data_out = 0; sbuf.data_p = data; sbuf.rw = V_SWAP; sbuf.ticks = Delta ? DELTA_CYCLE : 0;; VExch(&sbuf, &rbuf, node); return 0; } ///////////////////////////////////////////////////////////// // Invokes a tick message exchange // int VTick (unsigned int ticks, uint32 node) { rcv_buf_t rbuf; send_buf_t sbuf; sbuf.addr = 0; sbuf.data_out = 0; sbuf.rw = V_IDLE; sbuf.ticks = ticks; VExch(&sbuf, &rbuf, node); return 0; } ///////////////////////////////////////////////////////////// // Registers a user function as an interrupt callback // void VRegInterrupt (int level, pVUserInt_t func, uint32 node) { debug_io_printf("VRegInterrupt(): at node %d, registering interrupt level %d\n", node, level); if (level < MIN_INTERRUPT_LEVEL || level >= MAX_INTERRUPT_LEVEL) { io_printf("***Error: attempt to register an out of range interrupt level (VRegInterrupt)\n"); exit(1); } ns[node]->VInt_table[level] = func; } ///////////////////////////////////////////////////////////// // Registers a user function as a callback against // $vprocuser // void VRegUser (pVUserCB_t func, uint32 node) { debug_io_printf("VRegFinish(): at node %d, registering finish callback function\n", node); ns[node]->VUserCB = func; }