void test_pthread(void) { pthread_t consumer_t = 0; pthread_t producer_t = 0; Locker *wrlock = locker_pthread_create(); Locker *nest_lock = locker_pthread_nest_create(wrlock, (ReadSelfId)pthread_self); #ifdef RW_LOCK Locker *rdlock = locker_pthread_create(); RWLock *rwlock = rwlock_create(wrlock, rdlock); #endif DList *head = NULL; #ifdef RW_LOCK head = dlist_create(rwlock); #else head = dlist_create(wrlock); #endif return_if_fail(head != NULL); pthread_create(&producer_t, NULL, producer_thread, head); pthread_create(&consumer_t, NULL, consumer_thread, head); pthread_join(consumer_t, NULL); pthread_join(producer_t, NULL); }
PUBLIC DomElement* dom_element_create(SvgToken* token){ DomElement* thiz = NULL; ATTR* attr = NULL; Iterator* attr_iter = NULL; return_val_if_fail(token, NULL); thiz = (DomElement*)calloc(1, sizeof(DomElement)); thiz->nodeType = DOM_NODE_TYPE_ELEMENT; thiz->nodeName = copy_string(svg_token_get_data(token)); thiz->nodeValue = NULL; //读取属性信息 //thiz->attributes = attr_dlist_to_dom_attr_list(svg_token_get_attributes(token)); thiz->attributes = svg_token_get_attributes(token); //查找是否有id属性 attr_iter = dlist_iterator_create(thiz->attributes); while(attr = (ATTR*)attr_iter->get(attr_iter)){ if(!_tcscmp(attr->name, _T("id"))){ thiz->id = attr->value;//FIXME:这里没有拷贝 break; } attr_iter->next(attr_iter); } //FIXME:创建子节点链表,这里有待完善 thiz->childNodes = dlist_create(NULL); thiz->tagName = copy_string(svg_token_get_data(token));//FIXME:一些相同的属性是否保留两份副本?如Element的tagName和nodeName return thiz; }
void test_dlist_prepend_to_empty(void) { unsigned long *val = NULL; assert_true(test_dlist == NULL); test_dlist = dlist_create(); assert_true(test_dlist != NULL); assert_true(dlist_is_empty(test_dlist)); val = make_ulong_ptr(9999); assert_true(val != NULL); assert_true(dlist_prepend(test_dlist, val) == 0); /* Verify */ val = NULL; val = dlist_index(test_dlist, 0); assert_true(val != NULL); assert_ulong_equal(9999, *val); assert_true(dlist_size(test_dlist) == 1); dlist_free_all(test_dlist, NULL); test_dlist = NULL; }
dlist_t *dlist_search_all(dlist_t *thiz, void *key, dlist_cmp_t cmp) { int i; node_t *t; if (NULL != thiz->indxs) dlist_destroy(thiz->indxs); thiz->indxs = dlist_create(sizeof(int), NULL); if (NULL == thiz->indxs) goto err0; for (i = 0, t = thiz->head; i < thiz->count; i++, t = t->next) { if (!cmp(key, t->data)) { if (0 != dlist_append(thiz->indxs, &i)) goto err1; } } return thiz->indxs; err1: dlist_destroy(thiz->indxs); return NULL; err0: return NULL; }
PacketTransfer *packet_transfer_create(GatherBoard *gather_board) { if (gather_board == NULL) { return NULL; } PacketTransfer *thiz = (PacketTransfer *)malloc(sizeof(PacketTransfer)); if (thiz != NULL) { thiz->uploader_socket = -1; thiz->gather_board = gather_board; int i; for (i = 0; i < NODE_NUM; i++) { thiz->command_set[i] = dlist_create(command_destroy_cb, NULL); } memset(&thiz->response, 0, sizeof(thiz->response)); } return thiz; }
int main() { struct DList* list = dlist_create(); dlist_print(list); printf("list length = %d\n", dlist_length(list)); dlist_append(list, 5); printf("list length = %d\n", dlist_length(list)); dlist_append(list, 7); printf("list length = %d\n", dlist_length(list)); dlist_append(list, 8); printf("list length = %d\n", dlist_length(list)); dlist_print(list); dlist_print_reverse(list); dlist_remove(list, 1); printf("list length = %d\n", dlist_length(list)); dlist_print(list); dlist_print_reverse(list); dlist_insert(list, 0, 5); printf("list length = %d\n", dlist_length(list)); dlist_print(list); dlist_print_reverse(list); dlist_delete(list); return 0; }
/** * dlist_insert_tail: * Inserts a node at the tail of list. * @args **list: Double pointer to head of the list * @args *data: Pointer to data * @returns 0: Success * 1: Failure */ int dlist_insert_tail(dlist_node_t **list, void *data) { dlist_node_t *p, *q; p = (*list); if(!p) { /* It's an empty list */ *list = dlist_create(); if(!(*list)) return 1; /* dlist_create failed to allocate */ (*list)->data = data; return 0; } while(p->next) { p = p->next; } q = dlist_new_node(); if(q == NULL) { /* dlist_new_node failed to allocate memory */ return 1; } p->next = q; q->prev = p; q->next = NULL; q->data = data; return 0; }
int main() { int length = 0; int len = 0; // printf("please input list node num:"); // scanf("%d", &len); //int i = 'a'; //int i = 0; //Node* list = dlist_create(len, i); // dlist_set_print_callback(my_dlist_print_int); // dlist_print(list); //dlist_set_print_callback(my_dlist_print_char); //dlist_print(list); // length = dlist_get_length(list); // printf("lenght:%d\n", length); // dlist_for_each(list, upper_case_for_each, NULL); // dlist_print(list); // int index = 0; // int value =0; // printf("please input node index you want to insert:"); // scanf("%d", &index); // printf("please input node value you want to insert:"); // scanf("%d", &value); // dlist_insert(list,index, (void*)value); // dlist_print(list); // printf("please input node index you want to delete:"); // scanf("%d", &index); // dlist_delete(list, index); // dlist_print(list); // Node* max = dlist_get_max(list, dlist_get_max_node_int); // printf("max:"); // dlist_print_node(max); // int sum = 0; // dlist_for_each(list, sum_for_each, (void *)&sum); // printf("sum is %d\n", sum); int i = 0; Node* list = dlist_create(len, i); dlist_append(list, "hello"); dlist_append(list, "world"); dlist_append(list, "!"); dlist_set_print_callback(my_dlist_print_str); dlist_print(list); return 0; }
int main() { DList dlist = dlist_create(NULL, NULL, locker_pthread_create); pthread_t t1, t2; pthread_create(&t1, NULL, &consumer, (void *)&dlist); pthread_create(&t2, NULL, &producer, (void *)&dlist); while (1) sleep(1); return 0; }
static void test_int_dlist(void) { int i = 0; int n = 100; int data = 0; DList* dlist = dlist_create(NULL, NULL, NULL); for(i = 0; i < n; i++) { assert(dlist_append(dlist, (void*)i) == RET_OK); assert(dlist_length(dlist) == (i + 1)); assert(dlist_get_by_index(dlist, i, (void**)&data) == RET_OK); assert(data == i); assert(dlist_set_by_index(dlist, i, (void*)(2*i)) == RET_OK); assert(dlist_get_by_index(dlist, i, (void**)&data) == RET_OK); assert(data == 2*i); assert(dlist_set_by_index(dlist, i, (void*)i) == RET_OK); assert(dlist_find(dlist, cmp_int, (void*)i) == i); } for(i = 0; i < n; i++) { assert(dlist_get_by_index(dlist, 0, (void**)&data) == RET_OK); assert(data == (i)); assert(dlist_length(dlist) == (n-i)); assert(dlist_delete(dlist, 0) == RET_OK); assert(dlist_length(dlist) == (n-i-1)); if((i + 1) < n) { assert(dlist_get_by_index(dlist, 0, (void**)&data) == RET_OK); assert((int)data == (i+1)); } } assert(dlist_length(dlist) == 0); for(i = 0; i < n; i++) { assert(dlist_prepend(dlist, (void*)i) == RET_OK); assert(dlist_length(dlist) == (i + 1)); assert(dlist_get_by_index(dlist, 0, (void**)&data) == RET_OK); assert(data == i); assert(dlist_set_by_index(dlist, 0, (void*)(2*i)) == RET_OK); assert(dlist_get_by_index(dlist, 0, (void**)&data) == RET_OK); assert(data == 2*i); assert(dlist_set_by_index(dlist, 0, (void*)i) == RET_OK); } i = n - 1; assert(dlist_foreach(dlist, check_and_dec_int, &i) == RET_OK); dlist_destroy(dlist); return; }
void test_dlist_create(void) { test_dlist = dlist_create(); assert_true(test_dlist != NULL); assert_true(dlist_size(test_dlist) == 0); assert_true(dlist_is_empty(test_dlist)); dlist_free(test_dlist); test_dlist = NULL; }
PUBLIC DomDocument* dom_document_create(){ DomDocument* thiz; thiz = (DomDocument*)calloc(1, sizeof(DomDocument)); thiz->nodeType = DOM_NODE_TYPE_DOCUMENT; thiz->nodeName = _T("#document"); thiz->nodeValue = NULL; //Document的attributes成员为NULL thiz->attributes = NULL; //创建子节点,有待完善 thiz->childNodes = dlist_create(NULL); return thiz; }
static void multi_thread_test(void) { pthread_t consumer_tid = 0; pthread_t producer_tid = 0; DList* dlist = dlist_create(NULL, NULL, locker_pthread_create()); pthread_create(&producer_tid, NULL, producer, dlist); pthread_create(&consumer_tid, NULL, consumer, dlist); pthread_join(consumer_tid, NULL); pthread_join(producer_tid, NULL); return; }
dlist_t *dlist_new() { dlist_t *l = NULL; if (dlist_salloc_id == -1) { init_dlist(); } l = salloc(dlist_salloc_id); dlist_create(l); return l; }
PRIVATE DList* attr_dlist_to_dom_attr_list(DList* attr_list){ DomAttr* dom_attr = NULL; ATTR* attr = NULL; Iterator* attr_iter = NULL; DList* dom_attr_list = NULL; return_val_if_fail(attr_list, NULL); dom_attr_list = dlist_create(NULL);//FIXME attr_iter = dlist_iterator_create(attr_list); while(attr = (ATTR*)attr_iter->get(attr_iter)){ dom_attr = dom_attr_create(attr); dlist_append(dom_attr_list, dom_attr); } return dom_attr_list; }
void test_dlist_remove_data_from_empty(void) { assert_true(test_dlist == NULL); test_dlist = dlist_create(); assert_true(test_dlist != NULL); assert_true(dlist_is_empty(test_dlist)); assert_true(dlist_remove_data(test_dlist, NULL) == -1); dlist_free_all(test_dlist, NULL); test_dlist = NULL; }
void test_dlist_mergesort_empty(void) { assert_true(test_dlist == NULL); test_dlist = dlist_create(); assert_true(test_dlist != NULL); assert_true(dlist_is_empty(test_dlist)); assert_true(dlist_mergesort(test_dlist, (CompareFn)ulong_compare) == 0); dlist_free_all(test_dlist, NULL); test_dlist = NULL; }
Ret hash_table_insert(HashTable* thiz, void* data) { size_t index = 0; return_val_if_fail(thiz != NULL, RET_INVALID_PARAMS); index = thiz->hash(data)%thiz->slot_nr; if(thiz->slots[index] == NULL) { thiz->slots[index] = dlist_create(thiz->data_destroy, thiz->data_destroy_ctx); } return dlist_prepend(thiz->slots[index], data); }
void test_dlist_reverse_empty(void) { assert_true(test_dlist == NULL); test_dlist = dlist_create(); assert_true(test_dlist != NULL); assert_true(dlist_is_empty(test_dlist)); assert_true(dlist_reverse(test_dlist) == 0); dlist_free_all(test_dlist, NULL); test_dlist = NULL; }
static void multi_thread_test(void) { pthread_t consumer_tid = 0; pthread_t producer_tid = 0; Locker* locker = locker_pthread_create(); Locker* nest_locker = locker_nest_create(locker, (TaskSelfFunc)pthread_self); DList* dlist = dlist_create(NULL, NULL, nest_locker); pthread_create(&producer_tid, NULL, producer, dlist); pthread_create(&consumer_tid, NULL, consumer, dlist); printf("===========multi thread test==============\n"); pthread_join(consumer_tid, NULL); pthread_join(producer_tid, NULL); return; }
void dlist_setup_ints_random(void) { unsigned long i, *val; test_dlist = dlist_create(); assert_true(test_dlist != NULL); assert_true(dlist_is_empty(test_dlist)); for(i = 0; i < 1000; i++) { val = make_ulong_ptr(rand() % 10000); if(val != NULL) { dlist_append(test_dlist, val); } } assert_true(dlist_size(test_dlist) == 1000); }
Queue* queue_create(DataDestroyFunc data_destroy, void* ctx) { Queue* thiz = (Queue*)malloc(sizeof(Queue)); if(thiz != NULL) { Locker* locker = locker_pthread_create(); Locker* nest_locker = locker_nest_create(locker, (TaskSelfFunc)pthread_self); if((thiz->dlist = dlist_create(data_destroy, ctx, nest_locker)) == NULL) { free(thiz); thiz = NULL; } } return thiz; }
int main(int argc, char* argv[]) { int i = 0; int n = 100; DList* dlist = dlist_create(); /*for(i = 0; i < n; i++) { assert(dlist_append(dlist, (void*)i) == DLIST_RET_OK); }*/ for(i = 0; i < n; i++) { assert(dlist_prepend(dlist, (void*)i) == DLIST_RET_OK); } dlist_print(dlist, print_int); dlist_destroy(dlist); return 0; }
static void multi_thread_test(void) { pthread_t consumer_tid = 0; pthread_t producer_tid = 0; pthread_t reader_tid = 0; Locker* rw_locker = locker_pthread_create(); Locker* rd_locker = locker_pthread_create(); DList* dlist = dlist_create(NULL, NULL, rw_locker_create(rw_locker, rd_locker)); pthread_create(&producer_tid, NULL, producer, dlist); pthread_create(&consumer_tid, NULL, consumer, dlist); pthread_create(&reader_tid, NULL, reader, dlist); pthread_join(consumer_tid, NULL); pthread_join(producer_tid, NULL); pthread_join(reader_tid, NULL); printf("length=%d\n", dlist_length(dlist)); dlist_destroy(dlist); return; }
LinearContainer* linear_container_dlist_create(DataDestroyFunc data_destroy, void* ctx) { LinearContainer* thiz = (LinearContainer*)malloc(sizeof(LinearContainer) + sizeof(PrivInfo)); if(thiz != NULL) { PrivInfo* priv = (PrivInfo*)thiz->priv; priv->dlist = dlist_create(data_destroy, ctx); thiz->insert = linear_container_dlist_insert; thiz->prepend = linear_container_dlist_prepend; thiz->append = linear_container_dlist_append; thiz->del = linear_container_dlist_delete; thiz->get_by_index = linear_container_dlist_get_by_index; thiz->set_by_index = linear_container_dlist_set_by_index; thiz->length = linear_container_dlist_length; thiz->find = linear_container_dlist_find; thiz->foreach = linear_container_dlist_foreach; thiz->destroy = linear_container_dlist_destroy; } return thiz; }
int main(int argc, char* argv[]) { int i = 0; int n = 101; int last = n - 1; DList* dlist = dlist_create(NULL, NULL); for (i = 0; i < n; i++) { dlist_append(dlist, (void*)i); } Iterator* forward = dlist_iterator_create(dlist); Iterator* backward = dlist_iterator_create(dlist); iterator_advance(backward, last); invert(forward, backward); dlist_foreach(dlist, check_and_dec_int, &last); iterator_destroy(forward); iterator_destroy(backward); dlist_destroy(dlist); return 0; }
static struct worker_thread* worker_thread_create( const char* mqueue_name, struct allocator* alc ) { /* * Each worker thread has an epoll descriptor where it multiplexes * socket/message_queue descriptors. Also each worker thread has * a message queue where it retrieves notifications about new client * connections. Once a client has been accept()'ed, the socket is * sent to a worker thread via the message queue. * The thread will take ownership of the client, serve it and free * all the allocated resources when the client disconnects. */ assert(mqueue_name); if (!alc) { alc = allocator_handle; } struct worker_thread* wthread = alc->al_mem_alloc( alc, sizeof(struct worker_thread)); if (!wthread) { return NULL; } memset(wthread, 0, sizeof(*wthread)); wthread->wk_allocator = alc; wthread->wk_termsig.context.objid_type = kObjectTypeSignal; wthread->wk_termsig.so_sigfds = eventfd(0, EFD_NONBLOCK); if (wthread->wk_termsig.so_sigfds == -1) { D_FUNCFAIL_ERRNO(eventfd); goto ERR_CLEANUP; } /* * Level 1 - event descriptor allocated. */ ++wthread->wk_rollback; /* * This list is used to keep track of all the clients that are served * by this thread. Once a client pointer arrives via the message queue * it is put into this list. The client gets removed when it disconnects * or when there's an error on the socket. */ wthread->wk_clients = dlist_create(client_compare, wthread->wk_allocator, wthread); if (!wthread->wk_clients) goto ERR_CLEANUP; /* * Level 2 - client list created. */ ++wthread->wk_rollback; wthread->wk_mqueue_name = strdup(mqueue_name); if (!wthread->wk_mqueue_name) { D_FUNCFAIL_ERRNO(strdup); goto ERR_CLEANUP; } /* * Level 3 - memory for queue name allocated. */ ++wthread->wk_rollback; wthread->wk_epoll_fds = epoll_create(kMaxEpollCompletionEntries); if (wthread->wk_epoll_fds == -1) { D_FUNCFAIL_ERRNO(epoll_create); goto ERR_CLEANUP; } /* * Level 4 - epoll descriptor allocated. */ ++wthread->wk_rollback; /* * We only read from the message queue while the accepter thread only * writes into it. Note that the call to mq_open * will fail with EINVAL if the program is not run as superuser and * the values of mq_msgsize or mq_maxmsg are greater than those * specified in /proc/sys/fs/mqueue/msgsize_max and * /proc/sys/fs/mqueue/msg_max. */ struct mq_attr queue_attributes = { .mq_flags = O_NONBLOCK, .mq_maxmsg = kMessageQueueMaxMsgCount, .mq_msgsize = kMessageQueueMaxMsgSize }; /*queue_attributes.mq_flags = O_NONBLOCK; queue_attributes.mq_maxmsg = kMessageQueueMaxMsgCount; queue_attributes.mq_msgsize = kMessageQueueMaxMsgSize;*/ wthread->wk_messagequeue.context.objid_type = kObjectTypeMessageQueue; wthread->wk_messagequeue.mq_queuefds = mq_open(wthread->wk_mqueue_name, O_RDWR | O_EXCL | O_CREAT, S_IRWXU | S_IRGRP | S_IROTH, &queue_attributes); if (wthread->wk_messagequeue.mq_queuefds == (mqd_t) -1) { D_FUNCFAIL_ERRNO(mq_open); goto ERR_CLEANUP; } /* * Level 5 - message queue created. */ ++wthread->wk_rollback; return wthread; ERR_CLEANUP : /* * Fall-through is intended here. */ switch (wthread->wk_rollback) { case 4 : HANDLE_EINTR_ON_SYSCALL(close(wthread->wk_epoll_fds)); case 3 : free(wthread->wk_mqueue_name); case 2 : dlist_destroy(wthread->wk_clients); case 1 : HANDLE_EINTR_ON_SYSCALL(close(wthread->wk_termsig.so_sigfds)); case 0 : wthread->wk_allocator->al_mem_release(wthread->wk_allocator, wthread); default : break; } return NULL; }
dlist_t *dlist_load(dlist_t *thiz, const char *path, dlist_fileop_t fileop) { FILE *fp; size_t ret; int i, size, count; char magic[sizeof(DLIST_MAGIC)]; void *data; dlist_t *ls = dlist_create(thiz->size, thiz->dstr); if (NULL == ls) goto err0; dlist_destroy(thiz); if (NULL == (fp = fopen(path, "r"))) { perror("fopen"); goto err1; } ret = fread(magic, sizeof(char), sizeof(DLIST_MAGIC), fp); if (ret != sizeof(DLIST_MAGIC) || strcmp(magic, DLIST_MAGIC) != 0) goto err2; ret = fread(&size, sizeof(int), 1, fp); ret = fread(&count, sizeof(int), 1, fp); data = malloc(size); if (NULL == data) goto err2; if (NULL != fileop) { for (i = 0; i < count; i++) { ret = fileop(data, fp); if (0 != dlist_append(ls, data)) goto err3; } } else { for (i = 0; i < count; i++) { ret = fread(data, size, 1, fp); if (0 != dlist_append(ls, data)) goto err3; } } free(data); fclose(fp); return ls; err3: free(data); dlist_destroy(ls); err2: fclose(fp); return NULL; err1: dlist_destroy(ls); return NULL; err0: return NULL; }
int main(int argc, char **argv) { int size, rank, rc, root = 0, nameLength = 20; MPI_Status status; MPI_File configFile = malloc(sizeof configFile); MPI_Info info; char *configFileName = "./configFile.txt"; createLogFile(); MPI_Init(&argc, &argv); initLogFile(); MPI_Info_create(&info); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); printf("%d/%d started.\n", rank+1, size); char buf[nameLength + 1]; rc = MPI_File_open(MPI_COMM_WORLD, configFileName, MPI_MODE_RDONLY, info, &configFile); printf("%d/%d achieved the file_open result: %d.\n", rank+1, size, rc); // set the individual pointer to our position in the config file // master is the master of elevators rc = MPI_File_seek(configFile, rank * nameLength, MPI_SEEK_SET); rc = MPI_File_read(configFile, buf, nameLength, MPI_CHAR, &status); buf[nameLength] = '\0'; int len = nameLength - 1; while ((len >= 0) && (buf[len] == ' ')) { buf[len] = '\0'; len--; } rc = MPI_File_close(&configFile); char *ourname = buf; char str[50 + nameLength]; printf(strcat (strcat (strcpy (str, "%d/%d has the name '"), ourname), "'.\n"), rank+1, size); //read in the file to be counted, line by line. FILE * fp; fp = fopen(argv[1], "r"); if (fp == NULL) { printf("%d/%d did not find a document to count! Switching to assignment part 1.\n", rank+1, size); assgn = 1; } else { printf("%d/%d found a document to count. Switching to assignment part 2.\n", rank+1, size); assgn = 2; /* // general idea: char * line = NULL; size_t lineLen = 0; ssize_t read; dlist* list = NULL; dlist_create(10, &list); printf("size of list is %d\n", list->size); for (int c = 0; c < 15; c++) { char buf[6]; // long enough for test + num, e.g. test1 sprintf(buf, "test%d", c); dlist_append(&list, buf); } char* thestring; printf("capacity is %d\n", list->capacity); for (int c = 0; c < 15; c++) { dlist_get(&list, c, &thestring); printf("string is: %s\n", thestring); } while ((read = getline(&line, &lineLen, fp)) != -1) { printf("Retrieved line of length %zu :\n", read); printf("%s", line); } if (line) free(line); */ char * line = NULL; size_t lineLen = 0; ssize_t read; dlist_create(10, &list); // go through the lines in the file... int i = 0; while ((read = getline(&line, &lineLen, fp)) != -1) { i++; // ... and map them to the individual worker threads if (i % (size-1) == rank-1) { dlist_append(&list, line); } } if (line) free(line); } // we check whether or not we are the root process. if (rank == root) { master(rank); } else { worker(rank, ourname); } closeLogFile(); printf("%d/%d ended.\n", rank+1, size); MPI_Finalize(); }
PUBLIC SvgDom* svg_dom_create(){ SvgDom* thiz = (SvgDom*)calloc(1, sizeof(SvgDom)); thiz->root = NULL; thiz->id_element_list = dlist_create(NULL);//FIXME return thiz; }