int main() { lock = init_lock(); pthread_cond_init(&cond, NULL); pthread_cond_init(&work_cond, NULL); errorcheck_mutex_init(&mutex); memset(bools, false, NUM_THREADS); int i; for (i = 0; i < NUM_THREADS - 1; ++i) { pthread_create(workers + i, NULL, read_start, (void*)i); } pthread_create(workers + i, NULL, maywrite_start, (void*)i); pthread_mutex_lock(&mutex); while(counter != 0) { pthread_cond_wait(&cond, &mutex); } pthread_mutex_unlock(&mutex); pthread_cond_broadcast(&work_cond); for (i = 0; i < NUM_THREADS; ++i) { if (!bools[i]) { printf("Failed\n"); } } printf("PASS\n"); pthread_cond_destroy(&cond); pthread_cond_destroy(&work_cond); pthread_mutex_destroy(&mutex); destroy_lock(lock); return 0; }
bool Test1() { int i; for (i = 0; i < ARR_SIZE; ++i) { array[i] = i; } lock = init_lock(); pthread_t thread1; pthread_t thread2; pthread_t thread3; pthread_t thread4; pthread_t thread5; pthread_create(&thread1, NULL, work_on_array, &lock); pthread_create(&thread2, NULL, work_on_array, &lock); pthread_create(&thread3, NULL, work_on_array, &lock); pthread_create(&thread4, NULL, work_on_array, &lock); pthread_create(&thread5, NULL, work_on_array, &lock); pthread_join(thread1, NULL); pthread_join(thread2, NULL); pthread_join(thread3, NULL); pthread_join(thread4, NULL); pthread_join(thread5, NULL); destroy_lock(lock); return (counter == 5); }
bool Test2() { int i; for (i = 0; i < ARR_SIZE; ++i) { array[i] = 0; } lock = init_lock(); pthread_t thread1; pthread_t thread2; pthread_t thread3; pthread_t thread4; pthread_t thread5; pthread_create(&thread1, NULL, work_on_array2, &lock); pthread_create(&thread2, NULL, work_on_array3, &lock); pthread_create(&thread3, NULL, work_on_array3, &lock); pthread_create(&thread4, NULL, work_on_array4, &lock); pthread_create(&thread5, NULL, work_on_array4, &lock); pthread_join(thread1, NULL); pthread_join(thread2, NULL); pthread_join(thread3, NULL); pthread_join(thread4, NULL); pthread_join(thread5, NULL); for (i = 0; i < ARR_SIZE; ++i) { if ( (((i % 2) == 0) && array[i] != 4) || (((i % 2) == 1) && array[i] != 6)) return false; } destroy_lock(lock); return true; }
static void destroy_handle (struct shim_handle * hdl) { destroy_lock(hdl->lock); if (MEMORY_MIGRATED(hdl)) memset(hdl, 0, sizeof(struct shim_handle)); else free_mem_obj_to_mgr(handle_mgr, hdl); }
/** * Clean up function, which releases all the resources used by this library. * By default SIGINT signal is registered to call this function. * Resources freed in this function : * 1. Closes device file descriptor * 2. Free the mutex and locks * 3. Closes syslog * @warning fail to call this function, can result in memory leak. */ void gecko_clean_up() { // serial_write(tty_fd, stop_scan); close(tty_fd); stop_reader_thread(); destroy_serial_lock(); destroy_lock(); delete_all_node(); close_log(); }
static int free_index(int index) { proc_entry *entry; entry = (proc_entry *) get_proc_at_index(index); print(LOG_INFO, "Remove proc %s from index %d\n", entry->proc_name, index); clear_shm(entry->key_shm, entry->size_shm); entry->key_shm = 0; entry->size_shm = 0; mem_entry[index].shm = 0; destroy_lock(entry->key_rlock); entry->key_rlock = 0; mem_entry[index].rlock = 0; destroy_lock(entry->key_wlock); entry->key_wlock = 0; mem_entry[index].wlock = 0; destroy_lock(entry->key_active); entry->key_active = 0; mem_entry[index].active = 0; entry->active = 0; return 0; }
/* Delete all F_UNLCK locks */ static void delete_unlck_locks (pl_inode_t *inode) { posix_lock_t *l = inode->locks; while (l) { if (l->fl_type == F_UNLCK) { delete_lock (inode, l); destroy_lock (l); } l = l->next; } }
void ymempool_destroy(struct ymempool *mp) { int i; destroy_lock(mp); #ifdef CONFIG_MEMPOOL_DYNAMIC for (i = 0; i < mp->nrgrp; i++) { int j; for (j = 0; j < mp->grpsz; j++) yfree(mp->fbp[i][j]); yfree(mp->fbp[i]); } yfree(mp->fbp); #else /* CONFIG_MEMPOOL_DYNAMIC */ for (i = 0; i < mp->nrgrp; i++) { yfree(mp->fbp[i]); yfree(mp->grp[i]); } yfree(mp->fbp); yfree(mp->grp); #endif /* CONFIG_MEMPOOL_DYNAMIC */ yfree(mp); }
int lockthreadcounter(int nargs, char **args) { unsigned long num_threads = DEFAULT_THREADS; unsigned long num_inc = DEFAULT_INC; if (nargs > 1) num_threads = atoi(args[1]); if (nargs > 2) num_inc = atoi(args[2]); init_sem(); init_lock(); kprintf("Starting a thread counter using locks with %lu threads...\n", num_threads); runlockthreadcounter(num_threads, num_inc); kprintf("\nThread test done.\n"); kprintf("Counter: %lu (should be %lu)\n", counter, num_threads * num_inc); destroy_lock(); destroy_sem(); return 0; }
void ypool_destroy(struct ypool *p) { destroy_lock(p); yfree(p); }