Example #1
0
void GamePainter::drawFreeCell(QPoint _pos)
{
    QBrush freeZone( Qt::gray );
    m_painter.setBrush( freeZone );

    m_painter.drawRect( _pos.x() * m_cellSize.width()
                           , _pos.y() * m_cellSize.height() + 30
                           , m_cellSize.width()
                           , m_cellSize.height()
                           );
}
/*
 *freeRNDF function
 *
 *r : a pointer to struct RNDF
 *
 *this function will free the space allocated for a struct RNDF
 */
void freeRNDF(struct RNDF *r)
{
  int i;

  free(r -> RNDF_name);
  if(r -> format_version != 0)
    free(r -> format_version);
  if(r -> creation_date != 0)
    free(r -> creation_date);
  for(i = 0; i < r -> number_of_segments; i++)
    freeSegment(r -> segments_list[i]);
  free(r -> segments_list);
  for(i = 0; i < r -> number_of_zones; i++)
    freeZone(r -> zones_list[i]);
  free(r -> zones_list);
  free(r);
}
Example #3
0
bool rtZone::tryDelete(rtZone * deletingZone) {

    // try to delete this zone.
    // Note it is not always possible to delete a zone at the current time.
    // We might have to wait until later to delete
    // If we can delete though, recursively try to delete the parent zone too; return TRUE if the top-most zone is deleted (I.E. the hsm is done executing!)
    //
    // Note: the original zone trying the delete is passed in recursively

    //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" locking self zone"<<std::endl;
    zoneLock.lock();
    //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" locked self zone"<<std::endl;

    //
    // handle case where zone is not dormant
    //
    if ((!zoneIsInactive)) {
        // this zone is not dormant -- its thread is still running and accessing frames -- we can't really do anything to it now.
        //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" unlocking self zone (not dormant)"<<std::endl;
        zoneLock.unlock();
        //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" unlocked self zone (not dormant) and returning false"<<std::endl;
        return false;
    }

    //
    // at this point we know the zone is locked and is inactive -- its thread is not accessing any of its frames any longer
    // sweep through and remove any dormant frames with no child frames
    // this activity is mutexed via zoneLock
    //
    size_t dormantFramesFreed;
    while (dormantFramesFreed>0) { // while loop - at least one iteration
        dormantFramesFreed=0;
        //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" before sweep through dormant frames (active/dormant/free: "<<activeFrames.size()<<"/"<<dormantFrames.size()<<"/"<<freeFrames.size()<<")"<<std::endl;
        for(auto i=dormantFrames.begin();i!=dormantFrames.end();i++) {

            if (i->second->numChildFrames==0) {
                dormantFramesFreed++;
                freeFrames.insert(std::pair<std::string,rtFrame *>(i->second->mySubMachine->name,i->second));

                if (i->second->parentFrame != 0) {
                    //erfc deletingZone->logFile<<"tryDelete: C: {"<<i->second->parentFrame->pathStr()<<"} numChildFrames: "<<i->second->parentFrame->numChildFrames<<std::endl;
                }

                dormantFrames.erase(i->second->absolutePath); // careful: erasing from map we're iterating.
                break;
            }
        }
        //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" after sweep through dormant frames (active/dormant/free: "<<activeFrames.size()<<"/"<<dormantFrames.size()<<"/"<<freeFrames.size()<<")"<<std::endl;
    }
    //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" done sweeping up (active/dormant/free: "<<activeFrames.size()<<"/"<<dormantFrames.size()<<"/"<<freeFrames.size()<<")"<<std::endl;

    //
    // zone is locked and inactive, check if root zone with no dormant frames remaining (if yes, return true to indicate "hsm execution complete")
    //
    //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" check if hsm done"<<std::endl;
    if ((dormantFrames.size()==0) && (superiorZone==0)) {
        //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" topmost zone, unlocking self, deleting self, and returning true"<<std::endl;
        zoneLock.unlock();
        freeZone();
        return true;
    }

    //
    // zone is locked and inactive, handle case where dormant frames remain
    //
    //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" hsm not done, check if any dormant frames"<<std::endl;
    if (dormantFrames.size()>0) {
        //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" dormant frames detected, returning false"<<std::endl;
        zoneLock.unlock();
        return false;
    }

    //
    // zone is locked, inactive, and no dormant frames remaining
    //
    //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" no dormant frames, assert superior zone present and tryDelete the superior"<<std::endl;
    assert (superiorZone!=0);  // we checked for this case earlier

    //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" tryDelete superior zone"<<std::endl;
    bool rv=superiorZone->tryDelete(deletingZone);
    //erfc deletingZone->logFile<<"tryDelete: "<<"("<<eyeCatcher<<")"<<" tryDelete of superior zone completed, unlocking self, deleting self, and returning rv: "<<rv<<" from tryDelete of superior"<<std::endl;
    zoneLock.unlock();
    freeZone();
    return rv;
}
Example #4
0
void run_work(struct work_param* param) {
	if (pipe(param->pipes) != 0) {
		printf("Failed to create pipe! %s\n", strerror(errno));
		return;
	}
	unsigned char wb;
	unsigned char* mbuf = xmalloc(1024);
	while (1) {
		pthread_rwlock_rdlock(&param->conns->data_mutex);
		size_t cc = param->conns->count;
		struct pollfd fds[cc + 1];
		struct conn* conns[cc];
		int fdi = 0;
		for (int i = 0; i < param->conns->size; i++) {
			if (param->conns->data[i] != NULL) {
				conns[fdi] = (param->conns->data[i]);
				struct conn* conn = conns[fdi];
				fds[fdi].fd = conns[fdi]->fd;
				fds[fdi].events = POLLIN | (conn->writeBuffer_size > 0 ? POLLOUT : 0);
				fds[fdi++].revents = 0;
				if (fdi == cc) break;
			} else conns[fdi] = NULL;
		}
		pthread_rwlock_unlock(&param->conns->data_mutex);
		fds[cc].fd = param->pipes[0];
		fds[cc].events = POLLIN;
		fds[cc].revents = 0;
		int cp = poll(fds, cc + 1, -1);
		if (param->mysql->mysql && param->mysql->complete && param->zone != param->mysql->czone) {
			if (param->zone != NULL) {
				freeZone(param->zone);
			}
			param->zone = param->mysql->czone;
		}
		if (cp < 0) {
			printf("Poll error in worker thread! %s\n", strerror(errno));
		} else if (cp == 0) continue;
		else if ((fds[cc].revents & POLLIN) == POLLIN) {
			if (read(param->pipes[0], &wb, 1) < 1) printf("Error reading from pipe, infinite loop COULD happen here.\n");
			if (cp-- == 1) continue;
		}
		for (int i = 0; i < cc; i++) {
			int re = fds[i].revents;
			struct conn* conn = conns[i];
			if (conn == NULL) continue;
			if ((re & POLLERR) == POLLERR) {
				//printf("POLLERR in worker poll! This is bad!\n");
				goto cont;
			}
			if ((re & POLLHUP) == POLLHUP) {
				closeConn(param, conn);
				conn = NULL;
				goto cont;
			}
			if ((re & POLLNVAL) == POLLNVAL) {
				printf("Invalid FD in worker poll! This is bad!\n");
				closeConn(param, conn);
				conn = NULL;
				goto cont;
			}
			if ((re & POLLIN) == POLLIN) {
				size_t tr = 0;
				ioctl(fds[i].fd, FIONREAD, &tr);
				unsigned char* loc;
				if (conn->readBuffer == NULL) {
					conn->readBuffer = xmalloc(tr); // TODO: max upload?
					conn->readBuffer_size = tr;
					loc = conn->readBuffer;
				} else {
					conn->readBuffer_size += tr;
					conn->readBuffer = xrealloc(conn->readBuffer, conn->readBuffer_size);
					loc = conn->readBuffer + conn->readBuffer_size - tr;
				}
				ssize_t r = 0;
				if (r == 0 && tr == 0) { // nothing to read, but wont block.
					ssize_t x = 0;
					x = read(fds[i].fd, loc + r, tr - r);
					if (x <= 0) {
						closeConn(param, conn);
						conn = NULL;
						goto cont;
					}
					r += x;
				}
				while (r < tr) {
					ssize_t x = 0;
					x = read(fds[i].fd, loc + r, tr - r);
					if (x <= 0) {
						closeConn(param, conn);
						conn = NULL;
						goto cont;
					}
					r += x;
				}
				int p = 0;
				p = handleRead(conn, param, fds[i].fd);
				if (p == 1) {
					goto cont;
				}
			}
			if ((re & POLLOUT) == POLLOUT && conn != NULL) {
				ssize_t mtr = write(fds[i].fd, conn->writeBuffer, conn->writeBuffer_size);
				if (mtr < 0 && errno != EAGAIN) {
					closeConn(param, conn);
					conn = NULL;
					goto cont;
				} else if (mtr < 0) {
					goto cont;
				} else if (mtr < conn->writeBuffer_size) {
					memmove(conn->writeBuffer, conn->writeBuffer + mtr, conn->writeBuffer_size - mtr);
					conn->writeBuffer_size -= mtr;
					conn->writeBuffer = xrealloc(conn->writeBuffer, conn->writeBuffer_size);
				} else {
					conn->writeBuffer_size = 0;
					xfree(conn->writeBuffer);
					conn->writeBuffer = NULL;
				}
				if (conn->writeBuffer_size == 0 && conn->state == 1) {
					closeConn(param, conn);
					conn = NULL;
					goto cont;
				}
			}
			cont: ;
			if (--cp == 0) break;
		}
	}
	xfree(mbuf);
}