int k_init (int nrTask, int nrSem, int nrMsg) { if (k_running) // are you a fool ??? return (NULL); k_task = nrTask + 1; // +1 due to dummy k_sem = nrSem + nrMsg + 1; // due to that every msgQ has a builtin semaphore k_msg = nrMsg; task_pool = (struct k_t *) malloc (k_task * sizeof (struct k_t)); sem_pool = (struct k_t *) malloc (k_sem * sizeof (struct k_t)); send_pool = (struct k_msg_t *) malloc (k_msg * sizeof (struct k_msg_t)); // we dont accept any errors if ((task_pool == NULL) || (sem_pool == NULL) || (send_pool == NULL)) { k_err_cnt++; goto leave; } // init AQ as empty double chained list pAQ = &AQ; pAQ->next = pAQ->pred = pAQ; pAQ->prio = QHD_PRIO; pDmy = k_crt_task (dummy_task, DMY_PRIO, dmy_stk, DMY_STK_SZ); pSleepSem = k_crt_sem (0, 2000); leave: return k_err_cnt; }
int k_init (int nrTask, int nrSem, int nrMsg) { if (k_running) // are you a fool ??? { return (-666); } k_task = nrTask + 1; // +1 due to dummy k_sem = nrSem + nrMsg + 1; // due to that every msgQ has a builtin semaphore k_msg = nrMsg + 1; // to align so first user msgQ has index 1 nr_send++; // to align so we waste one but ... better equal access task_pool = (struct k_t *) malloc (k_task * sizeof (struct k_t)); sem_pool = (struct k_t *) malloc (k_sem * sizeof (struct k_t)); send_pool = (struct k_msg_t *) malloc (k_msg * sizeof (struct k_msg_t)); // we dont accept any errors if ((task_pool == NULL) || (sem_pool == NULL) || (send_pool == NULL)) { k_err_cnt++; goto leave; } // init AQ as empty double chained list pAQ = &AQ; pAQ->next = pAQ->pred = pAQ; pAQ->prio = QHD_PRIO; // crt dummy //pDmy = k_crt_task (dummy_task, DMY_PRIO, dmy_stk, DMY_STK_SZ); pmain_el = task_pool; pmain_el->nr = 0; pmain_el->cnt2 = pmain_el->cnt3 = 0; nr_task++; pmain_el->prio = DMY_PRIO; // main is dummy prio_enQ (pAQ, pmain_el); pSleepSem = k_crt_sem (0, 2000); leave: return k_err_cnt; }
struct k_msg_t * k_crt_send_Q (int nr_el, int el_size, void *pBuf) { struct k_msg_t *pMsg; if (k_running) { return (NULL); } if (k_msg <= nr_send) { goto errexit; } if (k_sem <= nr_sem) { goto errexit; } pMsg = send_pool + nr_send; pMsg->nr = nr_send; // I am element nr nr_send in msgQ pool nr_send++; pMsg->sem = k_crt_sem (0, nr_el); if (pMsg->sem == NULL) { goto errexit; } pMsg->pBuf = (char *) pBuf; pMsg->r = pMsg->w = -1; pMsg->el_size = el_size; pMsg->nr_el = nr_el; pMsg->lost_msg = 0; pMsg->cnt = 0; // count nr elm in Q return (pMsg); errexit: k_err_cnt++; return (NULL); }
struct k_msg_t * k_crt_send_Q (int nr_el, int el_size, void *pBuf) { struct k_msg_t *pMsg; if (k_running) return (NULL); if (k_msg <= nr_send) goto errexit; if (k_sem <= nr_sem) goto errexit; pMsg = send_pool + nr_send; nr_send++; pMsg->sem = k_crt_sem (0, nr_el); if (pMsg->sem == NULL) goto errexit; pMsg->pBuf = (char *) pBuf; pMsg->r = pMsg->w = -1; pMsg->el_size = el_size; pMsg->nr_el = nr_el; pMsg->lost_msg = 0; pMsg->cnt = 0; return (pMsg); errexit: k_err_cnt++; return (NULL); }