void * zctx__socket_new (zctx_t *self, int type) { // Initialize context now if necessary assert (self); zmutex_lock (self->mutex); if (!self->context) self->context = zmq_init (self->iothreads); zmutex_unlock (self->mutex); if (!self->context) return NULL; // Create and register socket void *zocket = zmq_socket (self->context, type); if (!zocket) return NULL; #if (ZMQ_VERSION_MAJOR == 2) // For ZeroMQ/2.x we use sndhwm for both send and receive zsocket_set_hwm (zocket, self->sndhwm); #else // For later versions we use separate SNDHWM and RCVHWM zsocket_set_sndhwm (zocket, self->sndhwm); zsocket_set_rcvhwm (zocket, self->rcvhwm); #endif zmutex_lock (self->mutex); if (zlist_push (self->sockets, zocket)) { zmutex_unlock (self->mutex); zmq_close (zocket); return NULL; } zmutex_unlock (self->mutex); return zocket; }
u32_t sys_arch_sem_wait(struct sys_sem **s, u32_t timeout_ms) { u32_t time_needed = 0; struct sys_sem *sem; LWIP_ASSERT("invalid sem", (s != NULL) && (*s != NULL)); sem = *s; zmutex_lock(sem->xm->zdt, &sem->zmutex); while (sem->c <= 0) { if (timeout_ms > 0) { time_needed = cond_wait(sem->xm, &sem->zcond, &sem->zmutex, timeout_ms, sem->verbose); if (time_needed == SYS_ARCH_TIMEOUT) { zmutex_unlock(sem->xm->zdt, &sem->zmutex); return SYS_ARCH_TIMEOUT; } /* pthread_mutex_unlock(&(sem->mutex)); return time_needed; */ } else { cond_wait(sem->xm, &sem->zcond, &sem->zmutex, 0, sem->verbose); } } sem->c--; zmutex_unlock(sem->xm->zdt, &sem->zmutex); return time_needed; }
void ztst_mutex(){ zmutex_t mtx; zmutex_init(&mtx); zmutex_lock(&mtx); zmutex_unlock(&mtx); zmutex_uninit(&mtx); #ifdef ZSYS_POSIX zmutex_lock(&mtx); zmutex_unlock(&mtx); #endif }
void zctx_set_linger (zctx_t *self, int linger) { assert (self); zmutex_lock (self->mutex); self->linger = linger; zmutex_unlock (self->mutex); }
void zctx_set_pipehwm (zctx_t *self, int pipehwm) { assert (self); zmutex_lock (self->mutex); self->pipehwm = pipehwm; zmutex_unlock (self->mutex); }
void zctx_set_sndhwm (zctx_t *self, int sndhwm) { assert (self); zmutex_lock (self->mutex); self->sndhwm = sndhwm; zmutex_unlock (self->mutex); }
void zctx_set_rcvhwm (zctx_t *self, int rcvhwm) { assert (self); zmutex_lock (self->mutex); self->rcvhwm = rcvhwm; zmutex_unlock (self->mutex); }
void zctx_set_iothreads (zctx_t *self, int iothreads) { assert (self); zmutex_lock (self->mutex); self->iothreads = iothreads; zmutex_unlock (self->mutex); }
void zctx__initialize_underlying (zctx_t *self) { assert (self); zmutex_lock (self->mutex); if (!self->context) self->context = zmq_init (self->iothreads); zmutex_unlock (self->mutex); }
void zctx__socket_destroy (zctx_t *self, void *zocket) { assert (self); assert (zocket); zsocket_set_linger (zocket, self->linger); zmq_close (zocket); zmutex_lock (self->mutex); zlist_remove (self->sockets, zocket); zmutex_unlock (self->mutex); }
int zthreadx_cancelall(){ int ret = ZEOK; zthr_t* pa = NULL; zmutex_lock(&zg_thr_mtx); pa = zg_thr_head; while(pa){ ZMSG("%s cancel",pa->name); zsem_post(&(pa->exit)); pa = pa->next; } zmutex_unlock(&zg_thr_mtx); return ret; }
void zmutex_test (bool verbose) { printf (" * zmutex: "); // @selftest zmutex_t *mutex = zmutex_new (); zmutex_lock (mutex); zmutex_unlock (mutex); zmutex_destroy (&mutex); // @end printf ("OK\n"); }
int zmutex_test (bool verbose) { printf (" * zmutex: "); // @selftest zmutex_t *mutex = zmutex_new (); // Try to lock while unlocked assert (zmutex_try_lock (mutex)); zmutex_unlock (mutex); // Try to lock while locked zmutex_lock (mutex); assert (zmutex_try_lock (mutex) == 0); zmutex_unlock (mutex); // Try to lock while unlocked, again assert (zmutex_try_lock (mutex)); zmutex_unlock (mutex); zmutex_destroy (&mutex); // @end printf ("OK\n"); return 0; }
void _dbg(int level, char *format, ...) { va_list args; if (mutex == NULL) { mutex = zmutex_new(); } if (_output == NULL) { _output = stdout; } zmutex_lock(mutex); for (int i = 0; i < dbgTabulations; i++) { fprintf(_output, " "); } switch (level) { #ifdef WIN32 case DBG_LEVEL_INFO: SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), 0x0A); break; case DBG_LEVEL_DEBUG: break; case DBG_LEVEL_WARNING: SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), 0x0E); break; case DBG_LEVEL_ERROR: SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), 0x0C); break; case DBG_LEVEL_SPECIAL: SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), 0x0B); break; #else case DBG_LEVEL_INFO: fprintf(_output, "\x1b[32m"); break; case DBG_LEVEL_DEBUG: fprintf(_output, "\x1b[37m"); break; case DBG_LEVEL_WARNING: fprintf(_output, "\x1b[33m"); break; case DBG_LEVEL_ERROR: fprintf(_output, "\x1b[31m"); break; case DBG_LEVEL_SPECIAL: fprintf(_output, "\x1b[35m"); break; #endif } va_start(args, format); vfprintf(_output, format, args); va_end(args); #ifdef WIN32 SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), 0x07); #else fprintf(_output, "\033[0m"); #endif fflush(_output); zmutex_unlock(mutex); }
static int zthr_listpush(zthr_t* attr){ int ret = ZEOK; if( NULL == zg_thr_head ){ // first thread not need lock, and init list if( ZEOK != (ret = zmutex_init(&zg_thr_mtx)))return ret; zg_thr_head = attr; attr->next = NULL; ++zg_thr_name; }else{ zmutex_lock(&zg_thr_mtx); attr->next = zg_thr_head; zg_thr_head = attr; ++zg_thr_name; zmutex_unlock(&zg_thr_mtx); } return ret; }
void sys_sem_signal(struct sys_sem **s) { struct sys_sem *sem; LWIP_ASSERT("invalid sem", (s != NULL) && (*s != NULL)); sem = *s; zmutex_lock(sem->xm->zdt, &sem->zmutex); if (sem->verbose) { fprintf(stderr, "xax_arch signal zcond %08x SIGNAL\n", (uint32_t) &sem->zcond); } sem->c++; if (sem->c > 1) { sem->c = 1; } zcond_broadcast(sem->xm->zdt, &sem->zcond); zmutex_unlock(sem->xm->zdt, &sem->zmutex); }
void _bufferPrint(void *buffer, int bufferSize, char *prefix) { if (mutex == NULL) { mutex = zmutex_new(); } zmutex_lock(mutex); int curPos = 0; fprintf(_output, "%s ===== buffer size = %d (0x%x) ================\n", prefix, bufferSize, bufferSize); while (curPos < bufferSize) { int offset; fprintf(_output, "%s", prefix); for (offset = 0; offset < 16 && curPos < bufferSize; offset++, curPos++) { fprintf(_output, " %02X", ((uint8_t *) buffer)[curPos]); } if (offset != 16) { for (int j = 0; j < 16 - offset; j++) { fprintf(_output, " "); } } fprintf(_output, " | "); curPos -= offset; for (offset = 0; offset < 16 && curPos < bufferSize; offset++, curPos++) { uint8_t c = ((uint8_t *) buffer)[curPos]; fprintf(_output, "%c", isprint(c) ? c : '.'); } fprintf(_output, "\n"); } fprintf(_output, "%s=================================================\n", prefix); fflush(_output); zmutex_unlock(mutex); }
static int zthr_listerase(zthr_t* attr){ int ret = ZENOT_EXIST; zthr_t* pa = zg_thr_head; zmutex_lock(&zg_thr_mtx); attr->detach = 1; if(pa == attr){ zg_thr_head = pa->next; ret = ZEOK; } while(pa && (pa->next != attr))pa = pa->next; if(pa){ pa->next = attr->next; ret = ZEOK; } zmutex_unlock(&zg_thr_mtx); if(NULL == zg_thr_head){ zmutex_uninit(&zg_thr_mtx); } ZERRCX(ret); return ret; }
int zthreadx_joinall(){ int ret = ZEOK; void* result = NULL; zthr_t* pa = NULL; zmutex_lock(&zg_thr_mtx); pa = zg_thr_head; while(pa){ //zthreadx_join(pa); // cause dead lock by mutex ZMSG("%s join begin...", pa->name); #ifdef ZSYS_POSIX if( 0 != (ret = pthread_join(pa->id, &result)))ret = errno; #else // ZSYS_WINDOWS ret = zobj_wait(pa->id, ZINFINITE); #endif ZMSG("%s join end. %s", pa->name, zstrerr(ret)); pa = pa->next; } zg_thr_head = NULL; zmutex_unlock(&zg_thr_mtx); zmutex_uninit(&zg_thr_mtx); return ret; }