static int event_queue_wait(event_queue_t * peq) { int ret = 0; cmd_event_t * pe; struct timespec tsp; long seconds = 50; EVENT_LOCK(peq); do { // printf("\n1\n"); if ((pe = min_heap_top(peq->heap)) != NULL) break; // printf("\n2\n"); peq->event_state = EVENT_SLEEP; // printf("\n3\n"); // pthread_cond_wait(&peq->cond, &peq->lock); maketimeout(&tsp, seconds); pthread_cond_timedwait(&peq->cond, &peq->lock, &tsp); // printf("\n4\n"); if ((pe = min_heap_top(peq->heap)) == NULL) { log_error(LOG_DEBUG, "pthread_cond_wait"); ret = -1; } // printf("\n5\n"); peq->event_state = EVENT_ALIVE; } while (0); EVENT_UNLOCK(peq); return ret; }
int usock_close(USocket *usock) { int timeout = 2; if (usock->status & USOCK_CONNECT) { pthread_mutex_lock(&usock->wlock); while (timeout < 16) { struct timespec tsp; if (send_umsg(usock, UMSG_CLOSE, NULL, 0) < 0) break; maketimeout(&tsp, timeout); if (pthread_cond_timedwait(&usock->wcond, &usock->wlock, &tsp) != ETIMEDOUT) break; timeout *= 2; } pthread_mutex_lock(&usock->wlock); } usock->status &= ~USOCK_CONNECT; usock->status |= USOCK_CLOSE; close(usock->fd); sem_wait(&usock->sem); free(usock); return 0; }
int usock_read(USocket *usock, void *data, Uint32 size) { int retval = -1; if (size <= 0) return 0; if (usock->status & USOCK_CLOSE) return -1; if (!(usock->status & USOCK_CONNECT)) return 0; pthread_mutex_lock(&usock->rlock); if (usock->tsize > 0) { int n = buffer_write(usock->rbuf, usock->tbuf, usock->tsize); usock->tsize -= n; if (usock->tsize == 0) { int timeout = 1; int ret; Uint32 mcount = htonl(usock->r_pack_count); while (timeout < 16) { struct timespec tsp; maketimeout(&tsp, timeout); if (send_umsg(usock, UMSG_CONT, &mcount, sizeof(mcount)) < 0) return -1; ret = pthread_cond_timedwait(&usock->rcond, &usock->rlock, &tsp); if (ret == 0) break; if (ret != ETIMEDOUT) return -1; timeout *= 2; } if (timeout >= 16) return -1; } } while (buffer_empty(usock->rbuf)) { if (usock->status & USOCK_CLOSE) goto err; if (!(usock->status & USOCK_CONNECT)) { retval = 0; goto err; } pthread_cond_wait(&usock->rcond, &usock->rlock); } retval = buffer_read(usock->rbuf, data, size); err: pthread_mutex_unlock(&usock->rlock); pthread_cond_signal(&usock->wcond); return retval; }
int usock_write(USocket *usock, const void *data, Uint32 len) { int timeout = 2; int retval = -1; if (len <= 0) return 0; if ((usock->status & USOCK_CLOSE) || !(usock->status & USOCK_CONNECT)) return -1; pthread_mutex_lock(&usock->wlock); while (timeout < 16) { struct timespec tsp; int ret; if ((retval = send_umsg(usock, UMSG_PACK, data, len)) < 0) goto err; maketimeout(&tsp, timeout); ret = pthread_cond_timedwait(&usock->wcond, &usock->wlock, &tsp); if ((usock->status & USOCK_CLOSE) || !(usock->status & USOCK_CONNECT)) { retval = -1; break; } if (ret == 0) { while (usock->status & USOCK_PEER_BUSY) { pthread_cond_wait(&usock->wcond, &usock->wlock); if ((usock->status & USOCK_CLOSE) || !(usock->status & USOCK_CONNECT)) { retval = -1; goto err; } } break; } else if (ret != ETIMEDOUT) { retval = -1; goto err; } timeout *= 2; } if (timeout >= 16) retval = -1; else usock->w_pack_count++; err: pthread_mutex_unlock(&usock->wlock); return retval; }