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); }
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 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; }