VAR_t POSCALL nosRegDel(const char *keyname) { REGELEM_t re, rl; VAR_t i; posSemaGet(reglist_sema_g); for (re = reglist_syselem_g[REGTYPE_USER], rl = NULL; re != NULL; rl = re, re = re->next) { if (!IS_DELETED(re)) { for (i=0; i<NOS_MAX_REGKEYLEN; ++i) { if (re->name[i] != keyname[i]) break; if ((keyname[i] == 0) || (i == NOS_MAX_REGKEYLEN-1)) { MARK_DELETED(re); n_remove(re, rl, REGTYPE_USER); posSemaSignal(reglist_sema_g); return E_OK; } } } } posSemaSignal(reglist_sema_g); return -E_FAIL; }
/* This function is called from two tasks. * It increments and prints a global counter variable. * The function uses a semaphore to protect the shared variable, * so only one task is allowed to modify the variable at the time. */ void incrementCounter(int tasknbr) { int c; /* wait for exclusive access to counter variable */ posSemaGet(semaphore); /* load variable */ c = counter; /* change copy of variable */ c++; /* waste some time */ posTaskSleep(MS(800)); /* store changed variable */ counter = c; /* print some text */ nosPrintf2("task%i: counter = %i\n", tasknbr, counter); /* quit exclusive access to the counter variable */ posSemaSignal(semaphore); }
void POSCALL nos_regDelSysKey(NOSREGTYPE_t type, NOSGENERICHANDLE_t handle, REGELEM_t re) { REGELEM_t rl = REEUNKNOWN; posSemaGet(reglist_sema_g); if (re == NULL) { for (re = reglist_syselem_g[type], rl = NULL; re != NULL; rl = re, re = re->next) { if (!IS_DELETED(re) && (re->handle.generic == handle)) break; } } if (re != NULL) { if (!IS_DELETED(re)) { MARK_DELETED(re); n_remove(re, rl, type); } } posSemaSignal(reglist_sema_g); }
/* This function is called from up to three tasks. * The function contains a section of code that is protected * by a semphore, so that only a maximum number of tasks is allowed * to execute the code simultanously. In this example program * the semaphore is initialized to 2, the counter variable will * count between 1 and 2, and the output is printed by all three * tasks. Example printout: * task2: counter = 2 * task3: counter = 1 * task1: counter = 2 * task3: counter = 2 */ void sharedResource(int tasknbr) { INT_t c; /* wait for shared resource */ posSemaGet(semaphore); /* --------------------------------------------------- */ /* increment global counter */ posAtomicAdd(&counter, 1); /* waste some time (this allows other tasks to enter this section of code, too) */ posTaskSleep(MS(800)); /* Get and print current counter value. * The counter value shows the number of tasks * that are currently executing this section of code. */ c = posAtomicGet(&counter); nosPrintf2("task%i: counter = %i\n", tasknbr, c); /* decrement global counter */ posAtomicSub(&counter, 1); /* --------------------------------------------------- */ /* quit access to shared resource */ posSemaSignal(semaphore); }
int uart_putchar(uint8_t ch) { posSemaGet(tx->lock); tx->buf[tx->head] = ch; tx->head = (tx->head + 1) & (UART_BUF_SIZE - 1); ENABLE_TXE_INTERRUPT; return 0; }
REGELEM_t POSCALL nos_regNewSysKey(NOSREGTYPE_t type, const char* name) { REGELEM_t re = NULL; posSemaGet(reglist_sema_g); (void) n_newKey(type, name, &re); posSemaSignal(reglist_sema_g); return re; }
VAR_t POSCALL nosGetNameByHandle(NOSGENERICHANDLE_t handle, char *buffer, VAR_t bufsize, NOSREGTYPE_t what) { NOSREGTYPE_t rt; REGELEM_t re = NULL; VAR_t status = -E_NOTFOUND; VAR_t i; char c; posSemaGet(reglist_sema_g); if (what != REGTYPE_SEARCHALL) { if (what <= MAX_REGTYPE) { re = n_findKeyByHandle(what, handle); } else { status = -E_ARG; } } else for (rt = MIN_REGTYPE; rt <= MAX_REGTYPE; ++rt) { #if NOSCFG_FEATURE_USERREG if (rt != REGTYPE_USER) #endif { re = n_findKeyByHandle(rt, handle); if (re != NULL) break; } } if (re != NULL) { if (IS_VISIBLE(re)) { status = E_OK; if ((buffer != NULL) && (bufsize > 0)) { for (i=0; (i<bufsize-1) && (i<NOS_MAX_REGKEYLEN); ++i) { c = re->name[i]; if (c == 0) break; buffer[i] = c; } buffer[i] = 0; } } } posSemaSignal(reglist_sema_g); return status; }
static void task_timer(void* arg) { (void) arg; for (;;) { /* wait for semaphore to be signalled */ posSemaGet(timersem_g); /* Set flag. The flag number will be printed out by the flag task. */ posFlagSet(flags_g, 3); } }
VAR_t POSCALL nosRegQueryElem(NOSREGQHANDLE_t qh, NOSGENERICHANDLE_t *genh, char* namebuf, VAR_t bufsize) { REGQUERY_t rq = (REGQUERY_t) qh; REGELEM_t re; VAR_t i; if ((rq == NULL) || (genh == NULL) || (namebuf == NULL)) return -E_ARG; posSemaGet(reglist_sema_g); if (rq->queryElem == NULL) { re = reglist_syselem_g[rq->type]; } else { re = rq->queryElem->next; DEC_REFCOUNT(rq->queryElem, rq->type); } for(;;) { rq->queryElem = re; if (re == NULL) { posSemaSignal(reglist_sema_g); return -E_NOMORE; } if (IS_VISIBLE(re)) break; re = re->next; } INC_REFCOUNT(re); *genh = re->handle.generic; for (i=0; (i < NOS_MAX_REGKEYLEN) && (i < (bufsize-1)) && (re->name[i] != 0); ++i) { namebuf[i] = re->name[i]; } namebuf[i] = 0; posSemaSignal(reglist_sema_g); return E_OK; }
void POSCALL nosRegQueryEnd(NOSREGQHANDLE_t qh) { REGQUERY_t rq = (REGQUERY_t) qh; if (rq != NULL) { if (rq->queryElem != NULL) { posSemaGet(reglist_sema_g); DEC_REFCOUNT(rq->queryElem, rq->type); posSemaSignal(reglist_sema_g); } nosMemFree(rq); } }
NOSGENERICHANDLE_t POSCALL nosGetHandleByName(NOSREGTYPE_t objtype, const char *objname) { REGELEM_t re; if ((objtype > MAX_REGTYPE) || (objname ==NULL)) return NULL; if (n_strlen(objname) > NOS_MAX_REGKEYLEN) return NULL; posSemaGet(reglist_sema_g); re = n_findKeyByName(objtype, objname); posSemaSignal(reglist_sema_g); if (re == NULL) return NULL; return IS_VISIBLE(re) ? re->handle.generic : NULL; }
VAR_t POSCALL nosRegGet(const char *keyname, KEYVALUE_t *keyvalue) { REGELEM_t re; if ((keyname == NULL) || (keyvalue == NULL)) return -E_ARG; posSemaGet(reglist_sema_g); re = n_findKeyByName(REGTYPE_USER, keyname); if (re != NULL) { if (IS_VISIBLE(re)) { *keyvalue = re->handle.ukv; } else { re = NULL; } } posSemaSignal(reglist_sema_g); return (re == NULL) ? -E_FAIL : E_OK; }
/* This function is executed by the second task that is set up * in the function firsttask() by a call to nosTaskCreate() */ void task2(void *arg) { POSSEMA_t sem; /* The handle to the semaphore is passed as * argument to this task. Please see the call * to the function nosTaskCreate() for details. */ sem = (POSSEMA_t) arg; for(;;) { /* Pend on semaphore. * The semaphore is signalled in the ISR. */ posSemaGet(sem); /* We got a signal from the ISR. * Print now the counter. */ nosPrintf1("interrupt signalled, counter = %i\n", isrCounter); } }
/* This function is called from two tasks. * It increments and prints a global counter variable. * The function uses a semaphore to protect the shared variable, * so only one task is allowed to modify the variable at the time. */ void incrementCounter(int tasknbr) { /* temporary variable */ int c; posSemaGet(semaphore); /* get the variable value*/ c = counter; /* increase the temporary variable*/ c++; /* wait the 500ms */ posTaskSleep(MS(500)); /* swap the variables */ counter = c; /* print the status */ nosPrintf("task%i: counter = %i\n", tasknbr, counter); /* flag state change control */ if(tasknbr == taskCount){ tasknbr = 1; } int tempOfIndexValue = tasknbr; tempOfIndexValue = tempOfIndexValue+1; /* flag updated */ posFlagSet(flagset, tempOfIndexValue); /* next the semaphore */ posSemaSignal(semaphore); }
static void task_semas(void *arg) { static INT_t cntr = 0; char buf[5]; buf[1] = 'S'; buf[2] = (char) ((int)arg); buf[3] = ' '; buf[4] = '\0'; (void) arg; for (;;) { /* try to get semaphore */ posSemaGet(sema_g); cntr++; /* sleep one second */ posTaskSleep(HZ); /* print information, how many tasks are in this function */ buf[0] = '0' + (char) cntr; print(buf); if ((cntr < 1) || (cntr > 2)) { print("SEMAPHORE-ERROR"); } cntr--; /* release semaphore again */ posSemaSignal(sema_g); } }
VAR_t POSCALL nosRegSet(const char *keyname, KEYVALUE_t keyvalue) { REGELEM_t re; VAR_t status = E_OK; if (keyname == NULL) return -E_ARG; posSemaGet(reglist_sema_g); re = n_findKeyByName(REGTYPE_USER, keyname); if (re == NULL) { status = n_newKey(REGTYPE_USER, keyname, &re); } if (status == E_OK) { re->handle.ukv = keyvalue; MARK_VISIBLE(re); } posSemaSignal(reglist_sema_g); return status; }