void SemA ( uint32_t parameter ) { _mqx_uint sem_result; void *Sem1_handle; /* create semaphore - sem.Sem1 */ sem_result = _sem_create("sem.Sem1", 1, 0); if (sem_result != MQX_OK) { /* semaphore sem.Sem1 not be created */ } /* endif */ /* open connection to semaphore sem.Sem1 */ sem_result = _sem_open("sem.Sem1",&Sem1_handle); if (sem_result != MQX_OK) { /* could not open sem.Sem1 */ } /* endif */ /* ** LOOP - */ while ( TRUE ) { /* wait for semaphore sem.Sem1 */ sem_result = _sem_wait_ticks(Sem1_handle, NO_TIMEOUT); if (sem_result != MQX_OK) { /* waiting on semaphore sem.Sem1 failed */ } /* semaphore obtained, perform work */ _time_delay_ticks(1); /* semaphore protected work done, release semaphore */ sem_result = _sem_post(Sem1_handle); } /* endwhile */ } /*end of task*/
void write_task ( uint_32 initial_data ) { pointer write_sem; pointer read_sem; pointer index_sem; /* open connections to all the semaphores */ if (_sem_open("sem.write", &write_sem) != MQX_OK) { printf("\nOpening write semaphore failed."); _task_block(); } if (_sem_open("sem.index", &index_sem) != MQX_OK) { printf("\nOpening index semaphore failed."); _task_block(); } if (_sem_open("sem.read", &read_sem) != MQX_OK) { printf("\nOpening read semaphore failed."); _task_block(); } while (TRUE) { /* wait for the semphores */ if (_sem_wait(write_sem, 0) != MQX_OK) { printf("\nWwaiting for Write semaphore failed"); _task_block(); } if (_sem_wait(index_sem, 0) != MQX_OK) { printf("\nWaiting for index semaphore failed"); _task_block(); } fifo.DATA[fifo.WRITE_INDEX++] = _task_get_id(); if (fifo.WRITE_INDEX >= ARRAY_SIZE) { fifo.WRITE_INDEX = 0; } /* Post the semaphores */ _sem_post(index_sem); _sem_post(read_sem); } }
int sem_post(sem_t *semp) { sem_t sem; if (!semp || !(sem = *semp)) { errno = EINVAL; return (-1); } _sem_post(sem); return (0); }
int sem_post(sem_t *s) { if (!s) return -1; if (_sem_post(s) > 0) { int i; for(i = 0; i < s->listeners; i++) { int pid = s->listener[i]; if (pid >= 0) { task_resume(pid); } } } return 0; }
void _sem_cleanup ( /* [IN] the task being destroyed */ TD_STRUCT_PTR td_ptr ) { /* Body */ SEM_CONNECTION_STRUCT_PTR sem_connection_ptr; SEM_CONNECTION_STRUCT_PTR connection_ptr; SEM_STRUCT_PTR sem_ptr; connection_ptr = _mem_get_next_block_internal(td_ptr, NULL); while (connection_ptr) { if ((connection_ptr->VALID == SEM_VALID) && (connection_ptr->TD_PTR == td_ptr) ) { sem_ptr = connection_ptr->SEM_PTR; if (sem_ptr->VALID == SEM_VALID) { /* Check if the connection is on the queue */ _int_disable(); sem_connection_ptr = (SEM_CONNECTION_STRUCT_PTR) ((pointer)sem_ptr->WAITING_TASKS.NEXT); while (sem_connection_ptr != (pointer)&sem_ptr->WAITING_TASKS.NEXT){ if (sem_connection_ptr == connection_ptr) { /* Connection is queued, so dequeue it */ _QUEUE_REMOVE(&sem_ptr->WAITING_TASKS, connection_ptr); break; }/* Endif */ sem_connection_ptr = (SEM_CONNECTION_STRUCT_PTR) sem_connection_ptr->NEXT; } /* Endwhile */ if (sem_ptr->POLICY & SEM_STRICT) { while (connection_ptr->POST_STATE) { _sem_post(connection_ptr); } /* Endwhile */ }/* Endif */ _int_enable(); }/* Endif */ } /* Endif */ connection_ptr = (SEM_CONNECTION_STRUCT_PTR) _mem_get_next_block_internal(td_ptr, connection_ptr); } /* Endwhile */ } /* Endbody */
void pthread_exit(void *retval) { struct rthread_cleanup_fn *clfn; pid_t tid; struct stack *stack; pthread_t thread = pthread_self(); thread->retval = retval; for (clfn = thread->cleanup_fns; clfn; ) { struct rthread_cleanup_fn *oclfn = clfn; clfn = clfn->next; oclfn->fn(oclfn->arg); free(oclfn); } _rthread_tls_destructors(thread); _spinlock(&_thread_lock); LIST_REMOVE(thread, threads); _spinunlock(&_thread_lock); _sem_post(&thread->donesem); stack = thread->stack; tid = thread->tid; if (thread->flags & THREAD_DETACHED) _rthread_free(thread); else _rthread_setflag(thread, THREAD_DONE); if (tid != _initial_thread.tid) _rthread_add_to_reaper(tid, stack); _rthread_reaper(); threxit(0); for(;;); }