static int test_mutex_destroyinit(void){ pthread_mutex_t lock,*heaplock; int ret = -1; if((heaplock = Malloc("heaplock",sizeof(*heaplock))) == NULL){ goto done; } if(Pthread_mutex_init(heaplock,NULL)){ goto done; } if(Pthread_mutex_init(&lock,NULL)){ goto done; } if(Pthread_mutex_init(&destroy_dyn_static_lock,NULL)){ goto done; } printf(" Destroying dynamic-init static lock...\n"); if(Pthread_mutex_destroy(&destroy_dyn_static_lock)){ goto done; } printf(" Destroying dynamic-init stack lock...\n"); if(Pthread_mutex_destroy(&lock)){ goto done; } printf(" Destroying dynamic-init heap lock...\n"); if(Pthread_mutex_destroy(heaplock)){ goto done; } ret = 0; done: Free(heaplock); return ret; }
/* --------------- threadpool API ------------------ */ threadpool_t *threadpool_create(int init, int max, int stack_size) { threadpool_t *pool; int i; assert(init > 0 && max >= init && stack_size >= 0); /* Allocate memory and zero all them. */ pool = (threadpool_t *)calloc(1, sizeof(*pool)); if (!pool) { return NULL; } Pthread_mutex_init(&pool->mutex, NULL); Pthread_cond_init(&pool->cond, NULL); Pthread_cond_init(&pool->exit_cond, NULL); Pthread_cond_init(&pool->task_over_cond, NULL); heap_init(&pool->task_queue); heap_set_less(&pool->task_queue, priority_less); pool->thread_stack_size = (stack_size == 0) ? THREAD_STACK_SIZE : stack_size; for (i = 0; i < init; ++i) { threadpool_thread_create(pool); } pool->threads_idle = init; pool->threads_num = init; pool->threads_max = max; return pool; }
DiskInfo::DiskInfo(int _diskid) { diskid = _diskid; waitingThread = 0; readingThread = 0; Pthread_mutex_init(&mutex, NULL); Pthread_cond_init(&cond, NULL); }
/* * _forwarding_setup * * 'index' is the index of the message config and forwarding info. * * Returns 0 success, -1 on error */ static int _forwarding_setup(int index) { hostlist_iterator_t itr = NULL; char *node; int rv = -1; assert(index >= 0 && index < conf.forward_message_config_len); /* We require a separate hostlist here b/c the hosts input by the * user and/or received by the remote hosts need to be mapped to a * single hostname. */ if ((forwarding_info[index].fd = _forwarding_setup_socket(index)) < 0) goto cleanup; if (conf.forward_message_config[index].hosts) { forwarding_info[index].hosts = Hostlist_create(NULL); itr = Hostlist_iterator_create(conf.forward_message_config[index].hosts); while ((node = Hostlist_next(itr))) { char nodebuf[CEREBRO_MAX_NODENAME_LEN+1]; char *nodeptr; if (found_clusterlist_module) { if (clusterlist_module_get_nodename(clusterlist_handle, node, nodebuf, CEREBRO_MAX_NODENAME_LEN+1) < 0) { CEREBROD_DBG(("clusterlist_module_get_nodename: %s", nodebuf)); Hostlist_destroy(forwarding_info[index].hosts); goto cleanup; } nodeptr = nodebuf; } else nodeptr = node; Hostlist_push(forwarding_info[index].hosts, nodeptr); free(node); } } else forwarding_info[index].hosts = NULL; Pthread_mutex_init(&(forwarding_info[index].lock), NULL); rv = 0; cleanup: if (itr) Hostlist_iterator_destroy(itr); return rv; }
void init_shared_pthread_mutex(pthread_mutex_t *mutex, int protocol, int policy) { pthread_mutexattr_t attr; process_shared_mutex_available(); Pthread_mutexattr_init(&attr); Pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); Pthread_mutexattr_setprotocol(&attr, protocol); Pthread_mutex_init(mutex, &attr); }
int main (int argc, char *argv []) { int proxy_port, listenfd, clientlen, clientfd; struct sockaddr_in clientaddr; pthread_t tid; /* parse command line arguments */ if (argc != 2) { fprintf(stderr, "This is a web proxy.\nUsage:\n$./proxy port\n"); exit(1); } proxy_port = atoi(argv[1]); /* ignore SIGPIPE signal */ Signal(SIGPIPE, SIG_IGN); /* initialize global variables */ head = NULL; cache_time = 0; cache_size = 0; Pthread_mutex_init(&malloc_mutex, NULL); Pthread_mutex_init(&free_mutex, NULL); Pthread_mutex_init(&open_mutex, NULL); Pthread_mutex_init(&time_mutex, NULL); Pthread_mutex_init(&dup_mutex, NULL); Pthread_rwlock_init(&cache_lock, NULL); /* open listen file discriptor and listen for connection requests */ listenfd = Open_listenfd(proxy_port); while (1) { clientlen = sizeof(clientaddr); if ((clientfd = accept(listenfd, (SA *)&clientaddr, (socklen_t *)&clientlen)) < 0) continue; Pthread_create(&tid, NULL, thread, (void *)(long)clientfd); } return 0; }
void shm_init_mutex() { pthread_mutex_t *mptr; pthread_mutexattr_t mattr; mptr = &x264_ring->mutex; Pthread_mutexattr_init(&mattr); #ifdef _POSIX_THREAD_PROCESS_SHARED Pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); #else CAP_DBG_EXIT("_POSIX_THREAD_PROCESS_SHARED not define"); #endif Pthread_mutex_init(mptr, &mattr); }
void my_lock_init(char *pathname) { int fd; pthread_mutexattr_t mattr; fd = Open("/dev/zero", O_RDWR, 0); mptr = Mmap(0, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); Close(fd); Pthread_mutexattr_init(&mattr); Pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); Pthread_mutex_init(mptr, &mattr); }
int main() { struct dyn_mutex *p; p = malloc(sizeof(struct dyn_mutex)); if (!p) { printf("no more memory\n"); exit(1); } Pthread_mutex_init(&p->m, NULL); Pthread_mutex_lock(&p->m); /* ... */ Pthread_mutex_unlock(&p->m); return 0; }
int crew_create(crew_p crew,int crew_size){ int worker_index; pthread_t tid; if(crew_size>CREWSIZE) return EINVAL; Pthread_mutex_init(&crew->mutex,NULL); Pthread_cond_init(&crew->done,NULL); Pthread_cond_init(&crew->go,NULL); crew->crew_size = crew_size; crew->work_cnt =0; crew->first_work=crew->last_work= NULL; for(worker_index=0;worker_index<crew_size;worker_index++){ crew->workers[worker_index].index = worker_index; crew->workers[worker_index].crew =crew; Pthread_create(&tid,NULL,work_loop,&crew->workers[worker_index]); crew->workers[worker_index].tid = tid; } return 0; }
void *do_reg(void *arg) { int sfd, confd; struct sockaddr_in cli_addr; arg_t *rarg; socklen_t len = sizeof(struct sockaddr_in); pthread_t tid; Pthread_mutex_init(&psyc, NULL); sbuf_init(&sbuf, SHORT_BUF); rarg = (arg_t *)arg; sfd = init_tcp_sock(rarg->port, rarg->serv_ip); if(sfd == -1) { err_sys("failed to init tcp socket...\n"); } #ifdef DEBUG printf("init the tcp sock\n"); /* for debuging... */ #endif while(1){ Pthread_mutex_lock(&psyc); confd = Accept(sfd, (SA *)&cli_addr, &len); Pthread_mutex_unlock(&psyc); Pthread_create(&tid, NULL, serv_thread, &confd); #ifdef DEBUG printf("one client come to server: %s\n", inet_ntoa(cli_addr.sin_addr)); #endif } Pthread_mutex_destroy(&psyc); sbuf_deinit(&sbuf); close(sfd); return NULL; }
FairLock() { Pthread_mutex_init(&m, NULL); locked = 0; q = qEnd = 0; }
int main(int argc, char **argv) { int n, k, e; unsigned prod_sum, cons_sum; pthread_attr_t attr; struct packet *p; int packets_per_producer; if (argc > 1 && !strcmp("-", argv[1])) Error("usage: %s num_producers num_consumers " "num_items buflen busy-loops", argv[0]); if (argc > 1) { num_producers = atoi(argv[1]); if (num_producers < 0) num_producers = 1; if (num_producers > MAXPRODUCERS) num_producers = MAXPRODUCERS; } if (argc > 2) { num_consumers = atoi(argv[2]); if (num_consumers < 0) num_consumers = 1; if (num_consumers > MAXCONSUMERS) num_consumers = MAXCONSUMERS; } if (argc > 3) { num_items = atoi(argv[3]); if (num_items < 0) num_items = 0; } if (argc > 4) { buflen = atoi(argv[4]); if (buflen < 1) buflen = 1; } if (argc > 5) busy_loops = atoi(argv[5]); printf("num_producers %d num_consumers %d num_items %d " "buflen %d busy-loops %d\n", num_producers, num_consumers, num_items, buflen, busy_loops); num_items /= num_producers; /* items/producer */ Pthread_attr_init(&attr); Pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); /* Producer Datenstruktur */ packets_per_producer = (num_producers + num_consumers) * 2; for (n = 0; n < num_producers; ++n) { Pthread_mutex_init(&producers[n].queue_mutex, NULL); Pthread_cond_init(&producers[n].empty_cond, NULL); producers[n].seed = n; producers[n].no = n; producers[n].emptyPacketQueue = NULL; for (k = 0; k < packets_per_producer; ++k) { p = malloc(sizeof(struct packet) + (buflen - 1) * sizeof(unsigned)); if (!p) Error("malloc"); p->owner = n; p->next = producers[n].emptyPacketQueue; producers[n].emptyPacketQueue = p; } } /* Consumer Datenstruktur */ for (n = 0; n < num_consumers; ++n) { Pthread_mutex_init(&consumers[n].queue_mutex, NULL); Pthread_cond_init(&consumers[n].empty_cond, NULL); consumers[n].no = n; consumers[n].eof = 0; consumers[n].fullPacketQueue = NULL; } /* Starte die Producer */ for (n = 0; n < num_producers; ++n) Pthread_create(&producers[n].tid, &attr, Producer, &producers[n]); /* Starte die Consumer */ for (n = 0; n < num_consumers; ++n) Pthread_create(&consumers[n].tid, &attr, Consumer, &consumers[n]); /* Warte auf alle Producer */ prod_sum = 0; for (n = 0; n < num_producers; ++n) { Pthread_join(producers[n].tid, NULL); prod_sum += producers[n].sum; } /* setze eof und wecke consumer auf */ for (n = 0; n < num_consumers; ++n) { Pthread_mutex_lock(&consumers[n].queue_mutex); consumers[n].eof = 1; Pthread_mutex_unlock(&consumers[n].queue_mutex); Pthread_cond_broadcast(&consumers[n].empty_cond); } /* Warte auf alle Consumer */ cons_sum = 0; for (n = 0; n < num_consumers; ++n) { Pthread_join(consumers[n].tid, NULL); cons_sum += consumers[n].sum; } printf("prot_sum %u cons_sum %u\n", prod_sum, cons_sum); if (cons_sum != prod_sum) printf("ERROR\n"); return 0; }
DiskMgr::DiskMgr(ConfigStrip *config) { // 虚拟读取磁盘时的初始化比较简单 if(!config->isUseRealDevice){ m_blockSize = config->blockSize; m_diskBand = config->diskBand; m_diskNumber = config->diskNumber; m_curReadThreadNum = config->curReadThreadNum; for (int i = 0; i < m_diskNumber; ++i) { DiskInfo *diskInfo = new DiskInfo(i); m_diskInfo.push_back(*diskInfo); // m_diskInfo[i] = 0; LOG_INFO("diskid " << m_diskInfo[i].diskid << " wait " << m_diskInfo[i].waitingThread << " read " << m_diskInfo[i].readingThread); } Pthread_mutex_init(&m_mutex, NULL); m_totalRead = 0; LOG_INFO("Finish initializing DiskMgr."); return; } m_blockSize = config->blockSize * 1024 * 1024; m_fileNumber = config->sourceNums; m_diskNumber = config->diskNumber; m_fileLength = config->maxLength / config->blockSize; m_curReadThreadNum = config->curReadThreadNum; for (int i = 0; i < m_diskNumber; ++i) { DiskInfo *diskInfo = new DiskInfo(i); m_diskInfo.push_back(*diskInfo); // m_diskInfo[i] = 0; } Pthread_mutex_init(&m_mutex, NULL); m_totalRead = 0; LOG_INFO("Start to initialize DiskMgr."); vector<string> segList; vector<FileInfoDisk> fileInfoList; vector<vector<string> > fileList; // m_fileIndex.push_back(segList); string prefix = "/disk"; int perDiskSegNum = m_fileLength * m_fileNumber / m_diskNumber + 1; for (int i = 0; i < m_diskNumber; ++i) { segList.clear(); string dirName = prefix + (char)('0' + i); GetFileNamesInDir(dirName, segList); assert((unsigned int)perDiskSegNum < segList.size()); fileList.push_back(segList); } int curIndex = 0; for (int i = 0; i < m_fileNumber; ++i) { fileInfoList.clear(); for (int j = 0; j < m_fileLength; ++j) { FileInfoDisk temp; temp.diskid = curIndex % m_diskNumber; temp.filename = fileList[curIndex % m_diskNumber][curIndex / m_diskNumber]; fileInfoList.push_back(temp); // cout << "file: " << i << " seg: " << j << "name: " << temp.filename << endl; ++curIndex; } m_fileIndex.push_back(fileInfoList); } LOG_INFO("Finish initializing DiskMgr."); // for (int i = 0; i < m_fileNumber; ++i) { // for (int j = 0; j < m_fileLength; ++j) { // cout << m_fileIndex[i][j].filename << " "; // } // cout << endl; // } }
/* * Under almost any circumstance, don't return a -1 error, cerebro can * go on without loading monitor modules. The listener_data_init_lock * should already be set. */ int cerebrod_event_modules_setup(void) { int i, event_module_count, event_index_len, event_index_count = 0; struct cerebrod_event_module_list *el = NULL; #if CEREBRO_DEBUG int rv; #endif /* CEREBRO_DEBUG */ assert(listener_data); #if CEREBRO_DEBUG /* Should be called with lock already set */ rv = Pthread_mutex_trylock(&listener_data_init_lock); if (rv != EBUSY) CEREBROD_EXIT(("mutex not locked: rv=%d", rv)); #endif /* CEREBRO_DEBUG */ if (!conf.event_server) return 0; if (!(event_handle = event_modules_load())) { CEREBROD_DBG(("event_modules_load")); goto cleanup; } if ((event_module_count = event_modules_count(event_handle)) < 0) { CEREBROD_DBG(("event_modules_count")); goto cleanup; } if (!event_module_count) { if (conf.debug && conf.event_server_debug) { Pthread_mutex_lock(&debug_output_mutex); fprintf(stderr, "**************************************\n"); fprintf(stderr, "* No Event Modules Found\n"); fprintf(stderr, "**************************************\n"); Pthread_mutex_unlock(&debug_output_mutex); } goto cleanup; } /* Each event module may want multiple metrics and/or offer multiple * event names. We'll assume there will never be more than 2 per * event module, and that will be enough to avoid all hash * collisions. */ event_index_len = event_module_count * 2; event_index = Hash_create(event_index_len, (hash_key_f)hash_key_string, (hash_cmp_f)strcmp, (hash_del_f)_cerebrod_event_module_list_destroy); event_names = List_create((ListDelF)_Free); event_module_timeouts = List_create((ListDelF)_cerebrod_event_module_timeout_data_destroy); event_module_timeout_index = Hash_create(event_module_count, (hash_key_f)hash_key_string, (hash_cmp_f)strcmp, (hash_del_f)list_destroy); for (i = 0; i < event_module_count; i++) { struct cerebrod_event_module *event_module; char *module_name, *module_metric_names, *module_event_names; char *metricPtr, *metricbuf; char *eventnamePtr, *eventbuf; int timeout; module_name = event_module_name(event_handle, i); if (conf.event_module_exclude_len) { int found_exclude = 0; int j; for (j = 0; j < conf.event_module_exclude_len; j++) { if (!strcasecmp(conf.event_module_exclude[j], module_name)) { found_exclude++; break; } } if (found_exclude) { if (conf.debug && conf.event_server_debug) { Pthread_mutex_lock(&debug_output_mutex); fprintf(stderr, "**************************************\n"); fprintf(stderr, "* Skip Event Module: %s\n", module_name); fprintf(stderr, "**************************************\n"); Pthread_mutex_unlock(&debug_output_mutex); } CEREBROD_ERR(("Dropping event module: %s", module_name)); continue; } } if (conf.debug && conf.event_server_debug) { Pthread_mutex_lock(&debug_output_mutex); fprintf(stderr, "**************************************\n"); fprintf(stderr, "* Setup Event Module: %s\n", module_name); fprintf(stderr, "**************************************\n"); Pthread_mutex_unlock(&debug_output_mutex); } if (event_module_setup(event_handle, i) < 0) { CEREBROD_DBG(("event_module_setup failed: %s", module_name)); continue; } if (!(module_metric_names = event_module_metric_names(event_handle, i)) < 0) { CEREBROD_DBG(("event_module_metric_names failed: %s", module_name)); event_module_cleanup(event_handle, i); continue; } if (!(module_event_names = event_module_event_names(event_handle, i)) < 0) { CEREBROD_DBG(("event_module_event_names failed: %s", module_name)); event_module_cleanup(event_handle, i); continue; } if ((timeout = event_module_timeout_length(event_handle, i)) < 0) { CEREBROD_DBG(("event_module_timeout_length failed: %s", module_name)); event_module_cleanup(event_handle, i); continue; } event_module = Malloc(sizeof(struct cerebrod_event_module_info)); event_module->metric_names = Strdup(module_metric_names); event_module->event_names = Strdup(module_event_names); event_module->index = i; Pthread_mutex_init(&(event_module->event_lock), NULL); /* The monitoring module may support multiple metrics */ metricPtr = strtok_r(event_module->metric_names, ",", &metricbuf); while (metricPtr) { if (!(el = Hash_find(event_index, metricPtr))) { el = (struct cerebrod_event_module_list *)Malloc(sizeof(struct cerebrod_event_module_list)); el->event_list = List_create((ListDelF)_cerebrod_event_module_info_destroy); Pthread_mutex_init(&(el->event_list_lock), NULL); List_append(el->event_list, event_module); Hash_insert(event_index, metricPtr, el); event_index_count++; } else List_append(el->event_list, event_module); metricPtr = strtok_r(NULL, ",", &metricbuf); } /* The monitoring module may support multiple event names */ eventnamePtr = strtok_r(event_module->event_names, ",", &eventbuf); while (eventnamePtr) { if (!list_find_first(event_names, (ListFindF)_cerebrod_name_strcmp, eventnamePtr)) { List_append(event_names, eventnamePtr); if (conf.debug && conf.event_server_debug) { Pthread_mutex_lock(&debug_output_mutex); fprintf(stderr, "**************************************\n"); fprintf(stderr, "* Event Name: %s\n", eventnamePtr); fprintf(stderr, "**************************************\n"); Pthread_mutex_unlock(&debug_output_mutex); } } eventnamePtr = strtok_r(NULL, ",", &eventbuf); } if (timeout) { struct cerebrod_event_module_timeout_data *mtd; List modules_list; if (!(mtd = List_find_first(event_module_timeouts, _event_module_timeout_data_find_callback, &timeout))) { char strbuf[64]; mtd = (struct cerebrod_event_module_timeout_data *)Malloc(sizeof(struct cerebrod_event_module_timeout_data)); mtd->timeout = timeout; snprintf(strbuf, 64, "%d", timeout); mtd->timeout_str = Strdup(strbuf); List_append(event_module_timeouts, mtd); if (timeout < event_module_timeout_min) event_module_timeout_min = timeout; } if (!(modules_list = Hash_find(event_module_timeout_index, mtd->timeout_str))) { modules_list = List_create((ListDelF)NULL); List_append(modules_list, event_module); Hash_insert(event_module_timeout_index, mtd->timeout_str, modules_list); } else List_append(modules_list, event_module); } } List_sort(event_module_timeouts, _event_module_timeout_data_compare); if (!event_index_count) goto cleanup; if (_setup_event_node_timeout_data() < 0) goto cleanup; /* * Since the cerebrod listener is started before any of the event * threads (node_timeout, queue_monitor, server), this must be * created in here (which is called by the listener) to avoid a * possible race of modules creating events before the event_queue * is created. */ event_queue = List_create((ListDelF)cerebrod_event_to_send_destroy); return 1; cleanup: if (event_handle) { event_modules_unload(event_handle); event_handle = NULL; } if (event_index) { Hash_destroy(event_index); event_index = NULL; } if (event_names) { List_destroy(event_names); event_names = NULL; } return 0; }
/* * Under almost any circumstance, don't return a -1 error, cerebro can * go on without loading monitor modules. The listener_data_init_lock * should already be set. */ int cerebrod_monitor_modules_setup(void) { int i, monitor_module_count, monitor_index_len, monitor_index_count = 0; struct cerebrod_monitor_module_list *ml = NULL; #if CEREBRO_DEBUG int rv; #endif /* CEREBRO_DEBUG */ #if CEREBRO_DEBUG /* Should be called with lock already set */ rv = Pthread_mutex_trylock(&listener_data_init_lock); if (rv != EBUSY) CEREBROD_EXIT(("mutex not locked: rv=%d", rv)); #endif /* CEREBRO_DEBUG */ if (!(monitor_handle = monitor_modules_load())) { CEREBROD_DBG(("monitor_modules_load")); goto cleanup; } if ((monitor_module_count = monitor_modules_count(monitor_handle)) < 0) { CEREBROD_DBG(("monitor_modules_count")); goto cleanup; } if (!monitor_module_count) { if (conf.debug && conf.listen_debug) { Pthread_mutex_lock(&debug_output_mutex); fprintf(stderr, "**************************************\n"); fprintf(stderr, "* No Monitor Modules Found\n"); fprintf(stderr, "**************************************\n"); Pthread_mutex_unlock(&debug_output_mutex); } goto cleanup; } /* Each monitor module may wish to monitor multiple metrics. We'll * assume there will never be more than 2 metrics per monitor module, and * that will be enough to avoid all hash collisions. */ monitor_index_len = monitor_module_count * 2; monitor_index = Hash_create(monitor_index_len, (hash_key_f)hash_key_string, (hash_cmp_f)strcmp, (hash_del_f)_cerebrod_monitor_module_list_destroy); for (i = 0; i < monitor_module_count; i++) { struct cerebrod_monitor_module_info *monitor_module; char *module_name, *metric_names; char *metricPtr, *metricbuf; module_name = monitor_module_name(monitor_handle, i); if (conf.monitor_module_exclude_len) { int found_exclude = 0; int j; for (j = 0; j < conf.monitor_module_exclude_len; j++) { if (!strcasecmp(conf.monitor_module_exclude[j], module_name)) { found_exclude++; break; } } if (found_exclude) { if (conf.debug && conf.listen_debug) { Pthread_mutex_lock(&debug_output_mutex); fprintf(stderr, "**************************************\n"); fprintf(stderr, "* Skip Monitor Module: %s\n", module_name); fprintf(stderr, "**************************************\n"); Pthread_mutex_unlock(&debug_output_mutex); } CEREBROD_ERR(("Dropping monitor module: %s", module_name)); continue; } } if (conf.debug && conf.listen_debug) { Pthread_mutex_lock(&debug_output_mutex); fprintf(stderr, "**************************************\n"); fprintf(stderr, "* Setup Monitor Module: %s\n", module_name); fprintf(stderr, "**************************************\n"); Pthread_mutex_unlock(&debug_output_mutex); } if (monitor_module_setup(monitor_handle, i) < 0) { CEREBROD_DBG(("monitor_module_setup failed: %s", module_name)); continue; } if (!(metric_names = monitor_module_metric_names(monitor_handle, i)) < 0) { CEREBROD_DBG(("monitor_module_metric_names failed: %s", module_name)); monitor_module_cleanup(monitor_handle, i); continue; } monitor_module = Malloc(sizeof(struct cerebrod_monitor_module_info)); monitor_module->metric_names = Strdup(metric_names); monitor_module->index = i; Pthread_mutex_init(&(monitor_module->monitor_lock), NULL); /* The monitoring module may support multiple metrics */ metricPtr = strtok_r(monitor_module->metric_names, ",", &metricbuf); while (metricPtr) { if (!(ml = Hash_find(monitor_index, metricPtr))) { ml = (struct cerebrod_monitor_module_list *)Malloc(sizeof(struct cerebrod_monitor_module_list)); ml->monitor_list = List_create((ListDelF)_cerebrod_monitor_module_info_destroy); Pthread_mutex_init(&(ml->monitor_list_lock), NULL); List_append(ml->monitor_list, monitor_module); Hash_insert(monitor_index, metricPtr, ml); monitor_index_count++; } else List_append(ml->monitor_list, monitor_module); metricPtr = strtok_r(NULL, ",", &metricbuf); } } if (!monitor_index_count) goto cleanup; return 1; cleanup: if (monitor_handle) { monitor_modules_unload(monitor_handle); monitor_handle = NULL; } if (monitor_index) { Hash_destroy(monitor_index); monitor_index = NULL; } return 0; }
int client_test() { int i, status; int count = 0; int normal = 0; pid_t pid[NUM_THREADS], wpid; unsigned long addr, len; unsigned long addr_to_map, addr_to_brk; void *ret[NUM_THREADS]; cat_t ar = create_category(CAT_S); cat_t aw = create_category(CAT_I); label_t L1 = {ar, aw}; label_t L2 = {ar}; own_t O = {}; label_t L_self, L_test; own_t O_self; addr = (unsigned long)ab_calloc(256, 4, L1); printf("child A malloc: %lx\n", addr); //printf("addr = %lx\n", *(unsigned long *)addr); //*(unsigned long *)addr = 0x12345678; // test code for ab_realloc() addr = (unsigned long)ab_malloc(11*4096, L2); *(unsigned long *)addr = 0xdeadbeef; printf("child A malloc: %lx, %lx\n", addr, *(unsigned long *)addr); addr = (unsigned long)ab_realloc((void *)addr, 12*4096); printf("child A realloc: %lx, %lx\n", addr, *(unsigned long *)addr); // test code for get_ownership() /* get_ownership(O_self); print_label(O_self); */ // test code for pthread mutex and condition variable //mutex = (pthread_mutex_t *)ab_malloc(sizeof(pthread_mutex_t), L1); pthread_mutexattr_t attr; Pthread_mutexattr_init(&attr); Pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); Pthread_mutex_init(mutex, &attr); cond = (pthread_cond_t *)ab_malloc(sizeof(pthread_cond_t), L1); pthread_condattr_t condattr; Pthread_condattr_init(&condattr); Pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED); Pthread_cond_init(cond, &condattr); // test code for ab_pthread_create() and ab_pthread_join() pthread_t tid[NUM_THREADS]; struct child_arg tdata[NUM_THREADS]; for (i = 0; i < NUM_THREADS; ++i) { if (i == 0) { tdata[i]._addr = addr; tdata[i].addr_to_map = (void *)(mutex); tdata[i].i = i; ab_pthread_create(&tid[i], NULL, child_func, &tdata[i], L1, O); } if (i == 1) { tdata[i].i = i; ab_pthread_create(&tid[i], NULL, child_func, &tdata[i], L2, O); } if (i == 2) { tdata[i].i = i; ab_pthread_create(&tid[i], NULL, child_func, &tdata[i], (label_t){}, (own_t){}); } if (i == 3) { tdata[i].i = i; ab_pthread_create(&tid[i], NULL, child_func, &tdata[i], (label_t){}, O); } } //wait some time for all the child to start //sleep(10); //printf("child A: addr = %lx\n", *(unsigned long *)addr); //*(unsigned long *)addr = 0xdeadbeef; // test code for pthread mutex AB_DBG("child A: mutex = %p\n", mutex); AB_DBG("child A: "); debug_mutex(mutex); Pthread_mutex_lock(&mmutex); //Pthread_cond_wait(cond, mutex); AB_DBG("child A: "); debug_mutex(mutex); printf("child A: doing somthing\n"); printf("child A: global_uninit=%lx\n", global_uninit); printf("child A: global_init=%lx\n", global_init); printf("child A: &global_uninit=%p\n", &global_uninit); printf("child A: &global_init=%p\n", &global_init); global_init = 0xdead0000; global_uninit = 0xdead0000; printf("child A: updated global_uninit=%lx\n", global_uninit); printf("child A: updated global_init=%lx\n", global_init); sleep(10); Pthread_mutex_unlock(&mmutex); AB_DBG("child A: "); debug_mutex(mutex); // test code for ablib_brk() // addr = (unsigned long)ablib_sbrk(pid[0], 0); // printf("child 0 sbrk: %lx\n", addr); // test code for ab_malloc() /* addr = (unsigned long)ab_malloc(41000, L2); printf("child A malloc: %lx\n", addr); //ab_free((void *)addr); addr = (unsigned long)ab_malloc(11*1024*4, L2); printf("child A malloc: %lx\n", addr); */ // test code for ab_malloc() and ab_free() exhaustively /* unsigned long addr_list[100]; for (i = 0; i < 100; i = i + 1) { addr_list[i] = (unsigned long)ab_malloc(i*1000, L2); printf("child A malloc: %lx, size = %d\n", addr_list[i], i*1000); } for (i = 0; i < 100; i = i + 1) { ab_free((void *)addr_list[i]); printf("child A free: %lx\n", addr_list[i]); } for (i = 0; i < 100; i = i + 1) { addr_list[i] = (unsigned long)ab_malloc(i*1000, L2); printf("child A malloc: %lx, size = %d\n", addr_list[i], i*1000); } */ // test code for get_mem_label() /* addr = (unsigned long)ab_malloc(4, L2); printf("child A malloc: %lx\n", addr); print_label(L2); get_mem_label((void *)addr, L_test); print_label(L_test); */ // test code for get_label() & get_mem_label(); /* get_label(L_self); print_label(L_self); addr = (unsigned long)ab_malloc(4, L_self); printf("child A malloc: %lx\n", addr); get_mem_label((void *)addr, L_test); print_label(L_test); */ /* while(1) { wpid = waitpid(-1, &status, 0); if(wpid <= 0) break; count++; if(WIFEXITED(status)) { printf("child %lu exited normally. count = %d\n", (unsigned long)wpid, count); normal++; } if(WIFSIGNALED(status)) { printf("child %lu terminated by a signal. count = %d\n", (unsigned long)wpid, count); } else printf("wait pid returns with status %d\n", status); } */ // test code for ab_pthread_create() and ab_pthread_join() for (i = 0; i < NUM_THREADS; ++i) { AB_DBG("main: tid[%d] = %d\n", i, (int)tid[i]); if (ab_pthread_join(tid[i], NULL) == 0) { normal++; } } if (normal == NUM_THREADS) { printf("mapping test successful.\n"); return 0; } test_failed: printf("test failed!!\n"); return -1; }