void cm_InitDaemon(int nDaemons) { static osi_once_t once; long pid; thread_t phandle; int i; cm_nDaemons = (nDaemons > CM_MAX_DAEMONS) ? CM_MAX_DAEMONS : nDaemons; if (osi_Once(&once)) { lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock", LOCK_HIERARCHY_DAEMON_GLOBAL); osi_EndOnce(&once); /* creating IP Address Change monitor daemon */ phandle = thrd_Create((SecurityAttrib) 0, 0, (ThreadFunc) cm_IpAddrDaemon, 0, 0, &pid, "cm_IpAddrDaemon"); osi_assertx(phandle != NULL, "cm_IpAddrDaemon thread creation failure"); thrd_CloseHandle(phandle); /* creating pinging daemon */ phandle = thrd_Create((SecurityAttrib) 0, 0, (ThreadFunc) cm_Daemon, 0, 0, &pid, "cm_Daemon"); osi_assertx(phandle != NULL, "cm_Daemon thread creation failure"); thrd_CloseHandle(phandle); for(i=0; i < cm_nDaemons; i++) { phandle = thrd_Create((SecurityAttrib) 0, 0, (ThreadFunc) cm_BkgDaemon, (LPVOID)i, 0, &pid, "cm_BkgDaemon"); osi_assertx(phandle != NULL, "cm_BkgDaemon thread creation failure"); thrd_CloseHandle(phandle); } } }
void cm_InitCell(int newFile, long maxCells) { static osi_once_t once; if (osi_Once(&once)) { cm_cell_t * cellp; lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL); if ( newFile ) { cm_data.allCellsp = NULL; cm_data.currentCells = 0; cm_data.maxCells = maxCells; memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize); memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize); #ifdef AFS_FREELANCE_CLIENT /* Generate a dummy entry for the Freelance cell whether or not * freelance mode is being used in this session */ cellp = &cm_data.cellBaseAddress[cm_data.currentCells++]; memset(cellp, 0, sizeof(cm_cell_t)); cellp->magic = CM_CELL_MAGIC; lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL); lock_ObtainMutex(&cellp->mx); lock_ObtainWrite(&cm_cellLock); /* copy in name */ strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/ cellp->name[CELL_MAXNAMELEN-1] = '\0'; /* thread on global list */ cellp->allNextp = cm_data.allCellsp; cm_data.allCellsp = cellp; cellp->cellID = AFS_FAKE_ROOT_CELL_ID; cellp->vlServersp = NULL; _InterlockedOr(&cellp->flags, CM_CELLFLAG_FREELANCE); cm_AddCellToNameHashTable(cellp); cm_AddCellToIDHashTable(cellp); lock_ReleaseWrite(&cm_cellLock); lock_ReleaseMutex(&cellp->mx); #endif } else { lock_ObtainRead(&cm_cellLock); for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) { lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL); cellp->vlServersp = NULL; _InterlockedOr(&cellp->flags, CM_CELLFLAG_VLSERVER_INVALID); } lock_ReleaseRead(&cm_cellLock); } osi_EndOnce(&once); } }
cm_space_t *cm_GetSpace(void) { cm_space_t *tsp; if (osi_Once(&cm_utilsOnce)) { lock_InitializeRWLock(&cm_utilsLock, "cm_utilsLock", LOCK_HIERARCHY_UTILS_GLOBAL); osi_EndOnce(&cm_utilsOnce); } lock_ObtainWrite(&cm_utilsLock); if (tsp = cm_spaceListp) { cm_spaceListp = tsp->nextp; } else tsp = (cm_space_t *) malloc(sizeof(cm_space_t)); (void) memset(tsp, 0, sizeof(cm_space_t)); lock_ReleaseWrite(&cm_utilsLock); return tsp; }
void cm_InitDaemon(int nDaemons) { static osi_once_t once; pthread_t phandle; pthread_attr_t tattr; int pstatus; int i; pthread_attr_init(&tattr); pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); cm_nDaemons = (nDaemons > CM_MAX_DAEMONS) ? CM_MAX_DAEMONS : nDaemons; if (osi_Once(&once)) { /* creating IP Address Change monitor daemon */ pstatus = pthread_create(&phandle, &tattr, cm_IpAddrDaemon, 0); osi_assertx(pstatus == 0, "cm_IpAddrDaemon thread creation failure"); /* creating pinging daemon */ pstatus = pthread_create(&phandle, &tattr, cm_Daemon, 0); osi_assertx(pstatus == 0, "cm_Daemon thread creation failure"); pstatus = pthread_create(&phandle, &tattr, cm_LockDaemon, 0); osi_assertx(pstatus == 0, "cm_LockDaemon thread creation failure"); cm_bkgListpp = malloc(nDaemons * sizeof(void *)); cm_bkgListEndpp = malloc(nDaemons * sizeof(void *)); cm_bkgQueueCountp = malloc(nDaemons * sizeof(afs_uint64)); cm_daemonLockp = malloc(nDaemons * sizeof(osi_rwlock_t)); for(i=0; i < cm_nDaemons; i++) { lock_InitializeRWLock(&cm_daemonLockp[i], "cm_daemonLock", LOCK_HIERARCHY_DAEMON_GLOBAL); cm_bkgListpp[i] = cm_bkgListEndpp[i] = NULL; cm_bkgQueueCountp[i]=0; pstatus = pthread_create(&phandle, &tattr, cm_BkgDaemon, (LPVOID)(LONG_PTR)i); osi_assertx(pstatus == 0, "cm_BkgDaemon thread creation failure"); } osi_EndOnce(&once); } pthread_attr_destroy(&tattr); }
/* * Initialize the cache to have an entries. Called during system startup. */ long cm_InitACLCache(int newFile, long size) { cm_aclent_t *aclp; long i; static osi_once_t once; if (osi_Once(&once)) { lock_InitializeRWLock(&cm_aclLock, "cm_aclLock", LOCK_HIERARCHY_ACL_GLOBAL); osi_EndOnce(&once); } lock_ObtainWrite(&cm_aclLock); if ( newFile ) { cm_data.aclLRUp = cm_data.aclLRUEndp = NULL; aclp = (cm_aclent_t *) cm_data.aclBaseAddress; memset(aclp, 0, size * sizeof(cm_aclent_t)); /* * Put all of these guys on the LRU queue */ for (i = 0; i < size; i++) { aclp->magic = CM_ACLENT_MAGIC; osi_QAddH((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q); aclp++; } } else { aclp = (cm_aclent_t *) cm_data.aclBaseAddress; for (i = 0; i < size; i++) { aclp->userp = NULL; aclp->tgtLifetime = 0; aclp++; } } lock_ReleaseWrite(&cm_aclLock); return 0; }
main_BasicTest(HANDLE hWnd) { long mod1ID; long mod2ID; long scan1ID; long scan2ID; HANDLE mod1Handle; HANDLE mod2Handle; HANDLE scan1Handle; HANDLE scan2Handle; long localDone; osi_Init(); if (main_logp == NULL) { main_logp = osi_LogCreate("basic", 0); osi_LogEnable(main_logp); osi_SetStatLog(main_logp); } /* create three processes, two modifiers and one scanner. The scanner * checks that the basic invariants are being maintained, while the * modifiers modify the global variables, maintaining certain invariants * by using locks. * * The invariant is that global variables a and b total 100. */ a = 100; b = 0; done = 0; lock_InitializeRWLock(&main_doneRWLock, "done lock"); lock_InitializeRWLock(&main_bRWLock, "b lock"); lock_InitializeMutex(&main_aMutex, "a mutex"); mod1Handle = CreateThread((SECURITY_ATTRIBUTES *) 0, 0, (LPTHREAD_START_ROUTINE) main_Mod1, 0, 0, &mod1ID); if (mod1Handle == NULL) return -1; mod2Handle = CreateThread((SECURITY_ATTRIBUTES *) 0, 0, (LPTHREAD_START_ROUTINE) main_Mod2, 0, 0, &mod2ID); if (mod2Handle == NULL) return -2; scan1Handle = CreateThread((SECURITY_ATTRIBUTES *) 0, 0, (LPTHREAD_START_ROUTINE) main_Scan1, 0, 0, &scan1ID); if (scan1Handle== NULL) return -2; scan2Handle = CreateThread((SECURITY_ATTRIBUTES *) 0, 0, (LPTHREAD_START_ROUTINE) main_Scan2, 0, 0, &scan2ID); if (scan2Handle== NULL) return -2; /* start running check daemon */ while (1) { Sleep(1000); wsprintf(main_screenText[1], "Mod1 iteration %d", m1Loops); wsprintf(main_screenText[2], "Mod2 iteration %d", m2Loops); wsprintf(main_screenText[3], "Scan1 iteration %d", s1Loops); wsprintf(main_screenText[4], "Scan2 iteration %d, %d opportunites seen", s2Loops, s2Events); main_ForceDisplay(hWnd); /* copy out count of # of dudes finished */ lock_ObtainRead(&main_doneRWLock); localDone = done; lock_ReleaseRead(&main_doneRWLock); /* right now, we're waiting for 4 threads */ if (localDone == 4) break; } wsprintf(main_screenText[0], "Test done."); main_ForceDisplay(hWnd); /* done, release and finalize all locks */ lock_FinalizeRWLock(&main_doneRWLock); lock_FinalizeRWLock(&main_bRWLock); lock_FinalizeMutex(&main_aMutex); /* finally clean up thread handles */ CloseHandle(mod1Handle); CloseHandle(mod2Handle); CloseHandle(scan1Handle); CloseHandle(scan2Handle); return 0; }