zctx_t * zctx_shadow (zctx_t *ctx) { zctx_t *self; // Shares same 0MQ context but has its own list of sockets so that // we create, use, and destroy sockets only within a single thread. self = (zctx_t *) zmalloc (sizeof (zctx_t)); if (!self) return NULL; self->sockets = zlist_new (); self->mutex = zmutex_new (); if (!self->sockets || !self->mutex) { zlist_destroy (&self->sockets); zmutex_destroy (&self->mutex); free (self); return NULL; } self->context = ctx->context; self->pipehwm = ctx->pipehwm; self->sndhwm = ctx->sndhwm; self->rcvhwm = ctx->rcvhwm; self->linger = ctx->linger; self->shadow = true; // This is a shadow context return self; }
zctx_t * zctx_new (void) { zctx_t *self; self = (zctx_t *) zmalloc (sizeof (zctx_t)); if (!self) return NULL; self->sockets = zlist_new (); self->mutex = zmutex_new (); if (!self->sockets || !self->mutex) { zlist_destroy (&self->sockets); zmutex_destroy (&self->mutex); free (self); return NULL; } self->iothreads = 1; self->pipehwm = 1000; self->sndhwm = 1000; self->rcvhwm = 1000; zsys_handler_set (s_signal_handler); return self; }
zctx_t * zctx_new (void) { zctx_t *self = (zctx_t *) zmalloc (sizeof (zctx_t)); if (!self) return NULL; self->sockets = zlist_new (); self->mutex = zmutex_new (); if (!self->sockets || !self->mutex) { zctx_destroy (&self); return NULL; } self->iothreads = 1; self->pipehwm = 1000; self->sndhwm = 1000; self->rcvhwm = 1000; // Catch SIGINT and SIGTERM unless ZSYS_SIGHANDLER=false if ( getenv ("ZSYS_SIGHANDLER") == NULL || strneq (getenv ("ZSYS_SIGHANDLER"), "false")) zsys_catch_interrupts (); return self; }
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"); }
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); }
int main(int argc, char* argv[]){ Fix_Initialize(); g_apex_cfg = apex_cfg_new(argc,argv); assert(g_apex_cfg); if(g_apex_cfg->log_path){ zlog_use_file(g_apex_cfg->log_path); } else { zlog_use_stdout(); } g_zmq_context = zctx_new(1); g_mutex_hash = zmutex_new(); g_msgid2reply = hash_new(&g_hctrl_msgid2reply, NULL); char* broker = zstrdup(g_apex_cfg->broker); int thread_count = g_apex_cfg->worker_threads; zthread_t* reply_threads = (zthread_t*)zmalloc(thread_count*sizeof(zthread_t)); zthread_t* recv_threads = (zthread_t*)zmalloc(thread_count*sizeof(zthread_t)); for(int i=0; i<g_apex_cfg->worker_threads; i++){ //reply_threads[i] = zthread_new(thread_reply, zstrdup(broker)); recv_threads[i] = zthread_new(thread_recv, zstrdup(broker)); } zfree(broker); for(int i=0; i<thread_count; i++){ //zthread_join(reply_threads[i]); zthread_join(recv_threads[i]); } zmutex_destroy(&g_mutex_hash); hash_destroy(&g_msgid2reply); apex_cfg_destroy(&g_apex_cfg); zctx_destroy(&g_zmq_context); Fix_Uninitialize(); return 0; }
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); }
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; }
zctx_t * zctx_shadow_zmq_ctx (void *zmqctx) { assert (zmqctx); // Shares same 0MQ context but has its own list of sockets so that // we create, use, and destroy sockets only within a single thread. zctx_t *self = (zctx_t *) zmalloc (sizeof (zctx_t)); if (!self) return NULL; self->shadow = true; // This is a shadow context self->sockets = zlist_new (); self->mutex = zmutex_new (); if (!self->sockets || !self->mutex) { zctx_destroy (&self); return NULL; } self->context = zmqctx; self->pipehwm = 1000; self->sndhwm = 1000; self->rcvhwm = 1000; return self; }