/* * Unlocks a shared-memory FIFO. * * Precondition: * The FIFO is locked by this process. * Arguments: * shm Pointer to the shared-memory FIFO data-structure. * Returns: * 0 Success. * ECANCELED Operating-system failure. Error-message logged. * EINVAL "shm" is uninitialized. * EINVAL FIFO is locked by this process. Error-message logged. */ static int shmfifo_unlock( const struct shmhandle* const shm) { int status = checkLocked(shm); if (0 == status) { struct sembuf op[1]; /* printf("<%d> unlocking %d\n",getpid(),shm->semid); */ op[0].sem_num = SI_LOCK; op[0].sem_op = 1; /*op[0].sem_flg = SEM_UNDO; */ op[0].sem_flg = 0; if (semop(shm->semid, op, 1) == -1) { log_syserr("semop(2) failure"); status = ECANCELED; } else { status = 0; /* success */ } /* printf("unlocked. done\n"); */ } return status; }
void LinkedIntList::clear() { checkLocked("clear"); while (front != NULL) { ListNode* temp = front; front = front->next; delete temp; } }
int LinkedIntList::get(int index) const { checkLocked("get"); checkIndex(index, 0, size() - 1); ListNode* current = front; for (int i = 0; i < index; i++) { current = current->next; } return current->data; }
void LinkedIntList::set(int index, int value) { checkLocked("set"); checkIndex(index, 0, size() - 1); ListNode* current = front; for (int i = 0; i < index; i++) { current = current->next; } current->data = value; }
int LinkedIntList::size() const { checkLocked("size"); int count = 0; ListNode* current = front; while (current != NULL) { count++; current = current->next; } return count; }
void LinkedIntList::add(int value) { checkLocked("add"); if (front == NULL) { // empty list: add this node as the new front front = new ListNode(value, NULL); } else { // non-empty list: move to end, attach new node ListNode* current = front; while (current->next != NULL) { current = current->next; } current->next = new ListNode(value, NULL); } }
/* * Logs shared-memory usage statistics at level DEBUG. * * Precondition: * The FIFO is locked by this process. An error-message is logged if it * isn't. * Arguments: * shm Pointer to the shared-memory FIFO data-structure. */ static void shmfifo_printmemstatus( const struct shmhandle* const shm) { if (log_is_enabled_debug) { struct shmprefix* p = (struct shmprefix*)shm->mem; (void)checkLocked(shm); log_debug ("<%d> c: %d sz: %d, r: %d, w: %d, used: %d, free: %d, maxblock: %d", getpid (), p->counter, shm->sz, p->read, p->write, shmfifo_ll_memused (shm), shmfifo_ll_memfree (shm), shmfifo_ll_memfree (shm) - sizeof (struct shmbh)); } }
void LinkedIntList::insert(int index, int value) { checkLocked("insert"); checkIndex(index, 0, size()); if (index == 0) { ListNode* temp = front; front = new ListNode(value, temp); } else { ListNode* current = front; for (int i = 0; i < index - 1; i++) { current = current->next; } ListNode* temp = current->next; current->next = new ListNode(value, temp); } }
void LinkedIntList::remove(int index) { checkLocked("remove"); checkIndex(index, 0, size() - 1); ListNode* trash; if (index == 0) { trash = front; front = front->next; } else { ListNode* curr = front; for (int i = 0; i < index-1; i++) { curr = curr->next; } trash = curr->next; curr->next = curr->next->next; } delete trash; }
/* * Notifies the reader or writer process. * * Arguments: * shm Pointer to the shared-memory FIFO data-structure. * semIndex Which process to notify. One of SI_WRITER or SI_READER. * Returns: * 0 Success * ECANCELED Operating-system failure. Error-message logged. * EINVAL The FIFO isn't locked by the current process. * Error-message logged. * EINVAL "which" isn't SI_WRITER or SI_READER. Error-message * logged. * Precondition: * The FIFO is locked by the current process. */ static int shmfifo_notify( const struct shmhandle* shm, const SemIndex which) { int status = checkLocked(shm); if (0 == status) { if ((status = vetSemIndex(which)) == 0) { if (semctl(shm->semid, which, SETVAL, 1)) { log_syserr("semctl() failure"); status = ECANCELED; } else { status = 0; } } } return status; }
void Enemy::checkDamage() { int ch; // Chech shots. for (int i = 0; i < shots->actor.size(); ++i) { if (!shots->actor[i]->isExist) continue; Vector sp = tr1::dynamic_pointer_cast<Shot>(shots->actor[i])->pos; ch = checkHit(sp, 0.7, 0); if (ch >= HIT) { manager->addParticle(sp, rand.nextSignedFloat(0.3), 0, Shot::SPEED / 4); manager->addParticle(sp, rand.nextSignedFloat(0.3), 0, Shot::SPEED / 4); manager->addParticle(sp, M_PI + rand.nextSignedFloat(0.3), 0, Shot::SPEED / 7); shots->actor[i]->isExist = false; if (ch == HIT) addDamage(SHOT_DAMAGE); else addDamageBattery(ch, SHOT_DAMAGE); } } if (manager->mode == P47GameManager::ROLL) { // Chech rolls. for (int i = 0; i < rolls->actor.size(); ++i) { if (!rolls->actor[i]->isExist) continue; tr1::shared_ptr<Roll> rl = tr1::dynamic_pointer_cast<Roll>(rolls->actor[i]); ch = checkHit(rl->pos[0], 1.0, 1.0); if (ch >= HIT) { for (int i = 0; i < 4; ++i) manager->addParticle(rl->pos[0], rand.nextFloat(M_PI * 2), 0, Shot::SPEED / 10); float rd = ROLL_DAMAGE; if (rl->released) { rd += rd; } else { if (rl->cnt < Roll::NO_COLLISION_CNT) continue; } if (ch == HIT) addDamage(static_cast<int>(rd)); else addDamageBattery(ch, static_cast<int>(rd)); } } } else if (type->type != EnemyType::SMALL) { // Chech locks. for (int i = 0; i < locks->actor.size(); ++i) { if (!locks->actor[i]->isExist) continue; tr1::shared_ptr<Lock> lk = tr1::dynamic_pointer_cast<Lock>(locks->actor[i]); if (lk->state == Lock::SEARCH || lk->state == Lock::SEARCHED) { ch = checkLocked(lk->pos[0], 2.5, lk); if (ch >= HIT) { lk->state = Lock::SEARCHED; // lk->lockedEnemy = this; lk->lockedEnemy = returnSharedThis(); lk->lockedPart = ch; } return; // } else if (lk->state == Lock::FIRED && lk->lockedEnemy == this) { } else if (lk->state == Lock::FIRED && lk->lockedEnemy == returnSharedThis()) { ch = checkHit(lk->pos[0], 1.5, 1.5); if (ch >= HIT && ch == lk->lockedPart) { for (int i = 0; i < 4; ++i) manager->addParticle(lk->pos[0], rand.nextFloat(M_PI * 2), 0, Shot::SPEED / 10); if (ch == HIT) addDamage(LOCK_DAMAGE); else addDamageBattery(ch, LOCK_DAMAGE); lk->hit(); } } } } }