/** * Mutex */ LPDM_MUTEX mutexNew(const char *name) { LPDM_MUTEX m = NULL; #ifdef WIN32 m = CreateMutexA(NULL, FALSE, name); if(m == NULL){ DWORD dwError = GetLastError(); if(dwError == ERROR_ACCESS_DENIED) m = OpenMutexA(SYNCHRONIZE, FALSE, name); } #else pthread_mutexattr_t mutexattr; pthread_mutexattr_init(&mutexattr); m = (LPDM_MUTEX)malloc(sizeof(DM_MUTEX)); m->context = NULL; if(name){ int mutexexist = 0; mutexexist = shm_exist(name); m->context = mmapOpen(name, sizeof(pthread_mutex_t)); m->mutex = (pthread_mutex_t*)m->context->data; if(mutexexist) return m; pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED); pthread_mutexattr_setrobust(&mutexattr, PTHREAD_MUTEX_ROBUST); } else m->mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); pthread_mutex_init(m->mutex, &mutexattr); pthread_mutexattr_destroy(&mutexattr); #endif return m; }
static void mprofCountConstruct( void ) { size_t valueSize; const char * value = findArg( getConfStr(), "COUNTS_PATH", &valueSize ); if( value ) { char * tmp = tmpAllocVtable.calloc( valueSize + 1, sizeof( char ) ); strncpy( tmp, value, valueSize ); value = tmp; } else { value = "./mprof.counts"; } mprofRecordInit(); sem_init( &mmapSem, 0, 1 ); assert( mmapOpen( &countsArea, value, O_RDWR | O_TRUNC | O_CREAT ) ); assert( mmapSize( &countsArea, sizeof( struct MprofRecordCount ), MMAP_AREA_SET ) ); }
void logInit(int logType, const char* lpszPath) { #if !defined(ENABLE_DMLOG) return; #else if(g_logInited) return; g_logLevel = logDebugLevel; if(logType < LOG_DRIVER_MAX) g_logType = logType; InitLock(&g_LockLog); g_hLogFile = NULL; g_logInited = 1; if(g_logType == LOG_DRIVER_FILE) { g_hLogFile = fopen(lpszPath, "w+t"); } else if(g_logType == LOG_DRIVER_MMAP) { g_mmap_log.event = eventNew(NAMED_EVENT_PATH); g_mmap_log.mutex = mutexNew(NAMED_MUTEX_PATH); g_mmap_log.handle = mmapOpen(NAMED_MMAP_PATH, MMAP_LOG_FILE_SIZE); do{ MMAP_FILE_HEADER *hd; mutexLock(g_mmap_log.mutex, -1); g_mmap_log.readpos = sizeof(MMAP_FILE_HEADER); hd = (MMAP_FILE_HEADER*)g_mmap_log.handle->data; if(hd->magic_code != LOG_MMAP_MAGIC_CODE){ printf("It is the first process to create the memory map!\r\n"); hd->magic_code = LOG_MMAP_MAGIC_CODE; hd->writepos = sizeof(MMAP_FILE_HEADER); hd->endpos = sizeof(MMAP_FILE_HEADER); } mutexUnLock(g_mmap_log.mutex); }while(0); } else if(g_logType == LOG_DRIVER_SYSLOG) { #ifdef LINUX openlog(NULL, LOG_CONS|LOG_PID, LOG_USER); #endif } #endif }
/** * Event **/ LPDM_EVENT eventNew(const char *name) { LPDM_EVENT e; #ifdef WIN32 e = CreateEventA(NULL, FALSE, FALSE, name); return e; #else pthread_condattr_t condattr; pthread_mutexattr_t mutexattr; pthread_condattr_init(&condattr); pthread_mutexattr_init(&mutexattr); e = (LPDM_EVENT)malloc(sizeof(DM_EVENT)); e->context = NULL; if(name){ int mutexexist = 0; mutexexist = shm_exist(name); e->context = mmapOpen(name, sizeof(pthread_cond_t) + sizeof(pthread_mutex_t)); e->cond = (pthread_cond_t*)e->context->data; e->mutex = (pthread_mutex_t*)(e->cond + 1); if(mutexexist) return e; pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED); pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED); } else{ e->cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t)); e->mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); } pthread_cond_init(e->cond, &condattr); pthread_mutex_init(e->mutex, &mutexattr); pthread_condattr_destroy(&condattr); pthread_mutexattr_destroy(&mutexattr); return e; #endif }
static void mprofLogMmapConstruct( void ) { size_t argSize; const char * arg = findArg( getConfStr(), "LOG_QTY", &argSize ); if( arg ) { growthQty = strtol( arg, NULL, 10 ); } arg = findArg( getConfStr(), "CACHE_QTY", &argSize ); if( arg ) { cacheQty = strtol( arg, NULL, 10 ); } arg = findArg( getConfStr(), "LOG_PATH", &argSize ); if( arg ) { char * tmp = tmpAllocVtable.calloc( argSize + 1, sizeof( char ) ); strncpy( tmp, arg, argSize ); arg = tmp; } else { arg = "./mprof.log"; } assert( mmapOpen( &area, arg, O_RDWR | O_TRUNC | O_CREAT ) ); assert( mmapSize( &area, growthQty * sizeof( struct MprofRecordAlloc ), MMAP_AREA_SET ) ); sem_init( &mmapSem, 0, 1 ); }