void LockInitGlobal(void) { globalLock->claims = 0; LockInit(globalLock); globalRecLock->claims = 0; LockInit(globalRecLock); globalLockInit = TRUE; }
static void lockEnsureGlobalLock(void) { /* Ensure both global locks have been initialized. */ /* There is a race condition initializing them. */ if (!globalLockInit) { LockInit(globalLock); LockInit(globalRecLock); globalLockInit = TRUE; } }
ThreadPool::ThreadPool(SearchController *ctrl, int n) : controller(ctrl), nThreads(n) { for (int i = 0; i < Constants::MaxCPUs; i++) { data[i] = NULL; } #ifndef _WIN32 if (pthread_attr_init (&stackSizeAttrib)) { perror("pthread_attr_init"); return; } size_t stackSize; if (pthread_attr_getstacksize(&stackSizeAttrib, &stackSize)) { perror("pthread_attr_getstacksize"); return; } if (stackSize < THREAD_STACK_SIZE) { if (pthread_attr_setstacksize (&stackSizeAttrib, THREAD_STACK_SIZE)) { perror("error setting thread stack size"); } } #endif #ifdef NUMA cout << topo.description() << endl; rebindMask.set(); // bind main thread if (bind(0)) { cerr << "Warning: bind to CPU failed for thread 0" << endl; } rebindMask.set(0,0); #endif #ifdef _THREAD_TRACE LockInit(io_lock); #endif LockInit(poolLock); for (int i = 0; i < n; i++) { ThreadInfo *p = new ThreadInfo(this,i); if (i==0) { p->work = new RootSearch(controller,p); p->work->ti = p; } else { // defer search creation until thread starts //p->work = new Search(controller,p); } data[i] = p; } activeMask = 1ULL; availableMask = (n == 64) ? 0xffffffffffffffffULL : (1ULL << n)-1; }
virtual int Init(size_t memlen, unsigned int bloc_size, int id = 0) { LockInit(id); size_t mem_size; bool bNewCreate; ssize_t iInitType; mem_size = memlen; char buffer[256]; snprintf(buffer, sizeof(buffer), "touch .mem_cache_%d_key_value_mng", id); system(buffer); snprintf(buffer, sizeof(buffer), ".mem_cache_%d_key_value_mng", id); key_t shm_key = ftok(buffer, 'A'); printf("Creating %s cache[%d] shm: key[0x%08x], size[%u B].\n", buffer, id, (unsigned int)shm_key,(unsigned int)mem_size); assert(shm_key != -1); m_pMemKeyCache = new MemKeyCache(); m_iMemSize = mem_size; int err; m_p_shm_addr = CreateShm(shm_key, mem_size, err, bNewCreate, 0); if (!m_p_shm_addr) { printf("attach %s key[0x%08x] size[%d] error\n", buffer, (unsigned int)shm_key, (int)mem_size); return -1; } iInitType = bNewCreate?emInit:emRecover; ssize_t atBytes = m_pMemKeyCache->AttachMem(m_p_shm_addr, mem_size, 0, iInitType, bloc_size); if (atBytes < 0) { return -2; } return 0; }
// test idle scheduling int main(void){ int pid,cvarid,lockid; TracePrintf(0, "test_temp main:\n"); LockInit(&lockid); CvarInit(&cvarid); pid = Fork(); if(pid==0){ TracePrintf(0, "child: acquire lock\n"); Acquire(lockid); TracePrintf(0, "child: reclaim cvar\n"); Reclaim(cvarid); TracePrintf(0, "child: exit\n"); Exit(0); } else{ TracePrintf(0, "parent: acquire\n"); Acquire(lockid); TracePrintf(0, "parent: wait\n"); CvarWait(cvarid,lockid); Release(lockid); } Exit(0); }
int main(int argc, char *argv[]){ int numThreads = atoi(argv[1]); threshold = atoi(argv[2]); int levels = atoi(argv[3]); int * participantsAtLevel = (int * ) malloc(levels); for (int i = 0; i < levels; i++) { participantsAtLevel[i] = atoi(argv[4 + i]); } omp_set_num_threads(numThreads); int numIter = 144 * (0x4ffff) / numThreads; //int levels = 4; //int participantsAtLevel[] = {2, 4, 8, 16}; //omp_set_num_threads(16); //int levels = 2; //int participantsAtLevel[] = {12, 36}; //omp_set_num_threads(36); //int levels = 2; //int participantsAtLevel[] = {2, 4}; //omp_set_num_threads(4); struct timeval start; struct timeval end; uint64_t elapsed; #pragma omp parallel { int tid = omp_get_thread_num(); int size = omp_get_num_threads(); HMCS * hmcs = LockInit(tid, size, levels, participantsAtLevel); if(tid == 0) gettimeofday(&start, 0); QNode me; for(int i = 0; i < numIter; i++) { me.Reuse(); Acquire(hmcs, &me); //printf("Acquired %d!\n", tid); //#define VALIDATE #ifdef VALIDATE int lvar = var; var ++; assert(var == lvar + 1); #endif Release(hmcs, &me); } } gettimeofday(&end, 0); elapsed = TIME_SPENT(start, end); double throughPut = (numIter * numThreads * 144 * 0x4ffffL) * 100000.0 / elapsed; std::cout<<"\n Throughput = " << throughPut; return 0; }
void LinuxHandleSignals() { struct sigaction sig_action[1]; sig_action->sa_handler = SignalHandle; sigaction(SIGABRT, sig_action, NULL); sigaction(SIGHUP, sig_action, NULL); sigaction(SIGINT, sig_action, NULL); sigaction(SIGSEGV, sig_action, NULL); sigaction(SIGTERM, sig_action, NULL); LockInit(DeletionLock); }
void InitTotalBaseCache(uint64 mb) { int i; LockInit(SweepStackLock); TotalBase_Cache = malloc(sizeof(uint8 **) << 2); Block = malloc(128 + (TotalCachePer << 4)); for (i = 0; i < 4; i++) { CacheInfo[i] = NULL; TotalBase_Cache[i] = NULL; } SetTotalBaseCache(mb); }
ThreadPool::ThreadPool(SearchController *ctrl, int n) : controller(ctrl) { #ifndef _WIN32 if (pthread_attr_init (&stackSizeAttrib)) { perror("pthread_attr_init"); return; } size_t stackSize; if (pthread_attr_getstacksize(&stackSizeAttrib, &stackSize)) { perror("pthread_attr_getstacksize"); return; } if (stackSize < THREAD_STACK_SIZE) { if (pthread_attr_setstacksize (&stackSizeAttrib, THREAD_STACK_SIZE)) { perror("error setting thread stack size"); } } #endif #ifdef _THREAD_TRACE LockInit(io_lock); #endif LockInit(poolLock); nThreads = n; for (int i = 0; i < n; i++) { ThreadInfo *p = new ThreadInfo(this,i); if (i==0) { p->work = new RootSearch(controller,p); p->work->ti = p; } else { // defer search creation until thread starts //p->work = new Search(controller,p); } data[i] = p; } activeMask = 1ULL; availableMask = (n == 64) ? 0xffffffffffffffffULL : (1ULL << n)-1; }
//----------------------------------------------------------------------- // LockCreate // // LockCreate grabs a lock from the systeme-wide pool of locks and // initializes it. // It also sets the inuse flag of the lock to indicate that the lock is // being used by a process. It returns a unique id for the lock. All the // references to the lock should be made through the returned id. The // process of grabbing the lock should be atomic. // // If a new lock cannot be created, your implementation should return // INVALID_LOCK (see synch.h). //----------------------------------------------------------------------- lock_t LockCreate() { lock_t l; uint32 intrval; // grabbing a lock should be an atomic operation intrval = DisableIntrs(); for(l=0; l<MAX_LOCKS; l++) { if(locks[l].inuse==0) { locks[l].inuse = 1; break; } } RestoreIntrs(intrval); if(l==MAX_LOCKS) return SYNC_FAIL; if (LockInit(&locks[l]) != SYNC_SUCCESS) return SYNC_FAIL; return l; }
int main(int argc, char *argv[]) { // Create a single lock that the initial process obtains. TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "Creating the lock.\n"); int lock_id; int parent_cvar_id; LockInit(&lock_id); int rc; // Init with invalid id pointer rc = CvarInit((void *) 10); TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarInit w/ invalid pointer: rc = %d\n", rc); // Signal with bad id rc = CvarSignal(2388); TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarSignal w/ invalid id: rc = %d\n", rc); // Broadcast with bad id rc = CvarBroadcast(2388); TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarBroadcast w/ invalid id: rc = %d\n", rc); // Wait w/ bad cvar id rc = CvarWait(lock_id, 1239988); TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarWait w/ invalid cvar id: rc = %d\n", rc); // Wait w/ lock I don't own rc = CvarWait(lock_id, parent_cvar_id); TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarWait w/ lock I don't own: rc = %d\n", rc); // Wait with bad lock id rc = CvarWait(1283, parent_cvar_id); TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarWait w/ invalid cvar id: rc = %d\n", rc); int child_cvar_id; rc = CvarInit(&child_cvar_id); TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarInit w/ %x: rc = %d\n", &child_cvar_id, rc); // Signal with no one waiting rc = CvarSignal(child_cvar_id); TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarSignal w/ no waiting proc rc = %d\n", rc); // Broadcast with no one waiting rc = CvarBroadcast(child_cvar_id); TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarBroadcast w/ no waiting proc rc = %d\n", rc); rc = CvarInit(&parent_cvar_id); TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarInit w/ %x: rc = %d\n", &parent_cvar_id, rc); // The initial process then attempts to acquire the lock it holds. This should return immediately. // Spawn 3 processes, each of which waits on the lock. int i; int n = 3; bool is_parent = true; for (i = 0; i < n; i++) { rc = Fork(); if (0 == rc) { // Child process TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'm a child with PID %d and I WANT the lock!\n", GetPid()); is_parent = false; Acquire(lock_id); break; } } // When a spawned process acquires the locks, it delays for a while, then releases it. TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'm process %d and I HAVE the lock!\n", GetPid()); if (!is_parent) { TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'm a child w/ PID %d and I'm waiting on my cvar!\n", GetPid()); CvarWait(child_cvar_id, lock_id); TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'ma a child w/ PID %d and I've finished waiting!\n", GetPid()); CvarSignal(parent_cvar_id); // signal with proc waiting } else { Delay(5); // let children start waiting Acquire(lock_id); TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'm parent w/ PID %d and I'm broadcasting for child cvar!\n", GetPid()); CvarBroadcast(child_cvar_id); // Broadchat with procs waiting TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'm parent w/ PID %d and I'm waiting on my cvar!\n", GetPid()); CvarWait(parent_cvar_id, lock_id); TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'm parent w/ PID %d and I've finished waiting!!\n", GetPid()); } Release(lock_id); int status; if (is_parent) { for (i = 0; i < n; i++) { Wait(&status); } Reclaim(lock_id); Reclaim(child_cvar_id); Reclaim(parent_cvar_id); } return 0; }
static void globalLockInit(void) { LockInit(globalLock); LockInit(globalRecLock); }
int main(int argc, char* argv[]){ // test 1 : pipe, lock and cvar int lock_id, cvar_id, pipe_id; int pid,status; int condition=0; char *test = "Yalnix Works"; char *buffer = (char*)malloc(1024); TracePrintf(1, "init main: test pipe, lock, cvar.\n"); LockInit(&lock_id); CvarInit(&cvar_id); PipeInit(&pipe_id); TracePrintf(1, "init main: Lockid %d.\n", lock_id); TracePrintf(1, "init main: Cvarid %d.\n", cvar_id); TracePrintf(1, "init main: Pipeid %d.\n", pipe_id); pid = Fork(); if (pid == 0) { TracePrintf(1,"init main: child \n"); Acquire(lock_id); TracePrintf(1,"init main: child acquire the lock\n"); condition=1; TracePrintf(1,"init main: child condition %d\n",condition); PipeWrite(pipe_id, &condition,sizeof(int)); TracePrintf(1,"init main: child change the condition and write it to pipe\n"); TracePrintf(1,"init main: child cvar signal\n"); CvarSignal(cvar_id); Release(lock_id); TracePrintf(1,"init main: child write to pipe: %s\n",test); PipeWrite(pipe_id,test,20); TracePrintf(1,"init main: child release the lock\n"); Exit(0); } else{ TracePrintf(1,"init main: parent\n"); Acquire(lock_id); TracePrintf(1,"init main: parent acquire the lock\n"); while(!condition){ TracePrintf(1,"init main: parent cvar wait, condition %d\n",condition); CvarWait(cvar_id,lock_id); PipeRead(pipe_id,&condition,sizeof(int)); TracePrintf(1,"init main: parent read the condition from pipe, condition %d\n",condition); } TracePrintf(1,"init main: parent wake up\n"); Release(lock_id); TracePrintf(1,"init main: parent release the lock\n"); PipeRead(pipe_id,buffer,20); TracePrintf(1,"init main: parent read from pipe: %s\n",buffer); } Reclaim(lock_id); Reclaim(cvar_id); Reclaim(pipe_id); Exit(0); }
int main(int argc, char **argv) { int pid, i, j, ret; int exit_status; char *exec_argv[] = { "haha", NULL }; int *a = (int *)calloc(3, sizeof(int)); int num_a = 100; char *str; unsigned int delay_ticks = 2; char buf[2 * TERMINAL_MAX_LINE + 2]; char pipe_buf[1025]; int pipe_fd; int lock_fd; int cvar_fd; #ifdef SWAP_TEST free(a); a = (void *)calloc(num_a, PAGESIZE); if (a == NULL) goto loop; if (Fork() == 0) { while (1) { Delay(2); a[0] = 1000; TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]); } } for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int))) a[i] = 100; if (Fork() == 0) { while (1) { Delay(2); a[0] = 2000; TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]); } } for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int))) a[i] = 100; if (Fork() == 0) { while (1) { Delay(2); a[0] = 3000; TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]); } } for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int))) a[i] = 100; if (Fork() == 0) { while (1) { Delay(2); a[0] = 4000; TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]); } } for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int))) a[i] = 100; if (Fork() == 0) { while (1) { Delay(2); a[0] = 5000; TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]); } } for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int))) a[i] = 100; #endif #ifdef PIPE_TEST ret = PipeInit(&pipe_fd); pid = Fork(); bzero(pipe_buf, sizeof(pipe_buf)); if (pid != ERROR && !pid) { TtyPrintf(CONSOLE, "pipe_fd = %u\n", pipe_fd); j = 0; Delay(1); while (1) { for (i = 0; i < sizeof(pipe_buf); i++) pipe_buf[i] = (j % 26) + 'a'; TtyPrintf(CONSOLE, ">>>>>>>>>>> write pipe\n"); ret = PipeWrite(pipe_fd, pipe_buf, sizeof(pipe_buf)); TtyPrintf(CONSOLE, "write pipe ret = %d, pid = %u\n", ret, GetPid()); j++; } Exit(0); } while (1) { bzero(pipe_buf, sizeof(pipe_buf)); TtyPrintf(CONSOLE, ">>>>>>>>>>> read pipe\n"); ret = PipeRead(pipe_fd, pipe_buf, sizeof(pipe_buf) - 7); TtyPrintf(CONSOLE, "<<<<<<<<<<< read pipe ret = %d, pid = %u, %s\n", ret, GetPid(), pipe_buf); } Reclaim(pipe_fd); #endif #ifdef CVAR_TEST ret = LockInit(&lock_fd); ret = CvarInit(&cvar_fd); pid = Custom0(0, 0, 0, 0); if (pid != ERROR && !pid) { Acquire(lock_fd); while (!condition) CvarWait(cvar_fd, lock_fd); Delay(2); Release(lock_fd); Exit(7); } Acquire(lock_fd); condition = 1; CvarSignal(cvar_fd); Release(lock_fd); ret = Reclaim(lock_fd); if (ret) Exit(-1); #endif #ifdef TTY_TEST for (i = 0; i < sizeof(buf) - 1; i++) buf[i] = '9'; buf[i] = '\0'; TtyPrintf(CONSOLE, buf); TtyPrintf(CONSOLE, "\n"); a[0] = 10; a[2] = 100; TtyPrintf(CONSOLE, "Enter somthing:\n"); bzero(buf, sizeof(buf)); ret = TtyRead(CONSOLE, buf, sizeof(buf)); TtyPrintf(CONSOLE, "You just entered: %s (len = %d)\n", buf, ret); #endif #ifdef COW_TEST if (argc == 2) delay_ticks = atoi(argv[1]); pid = Fork(); if (pid == ERROR) { Delay(2); return ERROR; } else if (!pid) { GetPid(); delay_ticks = 5; TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid()); pid = Fork(); if (pid != ERROR && !pid) { GetPid(); delay_ticks = 8; TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid()); Delay(delay_ticks); Exec("exec_test", exec_argv); } pid = Wait(&exit_status); TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid()); TtyPrintf(CONSOLE, " wait child = %u, status = %d\n", pid, exit_status); Delay(delay_ticks); Exit(10); } pid = Fork(); if (pid != ERROR && !pid) { incursive_func(0); GetPid(); delay_ticks = 9; TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid()); Delay(delay_ticks); Exit(100); } TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid()); Delay(delay_ticks); GetPid(); Wait(&exit_status); Wait(&exit_status); GetPid(); #endif loop: while (1) Delay(delay_ticks); return 0; }
void LedyardBridgeInit() { CvarInit(&(cvar[0])); CvarInit(&(cvar[1])); LockInit(&mutex); }