//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== void AddThread(void (*func)(int)) { thread_t *thread; if (numthreads == 1) { if (currentnumthreads >= numthreads) { return; } currentnumthreads++; func(-1); currentnumthreads--; } //end if else { ThreadLock(); if (currentnumthreads >= numthreads) { ThreadUnlock(); return; } //end if //allocate new thread thread = GetMemory(sizeof(thread_t)); if (!thread) { Error("can't allocate memory for thread\n"); } // thread->threadid = currentthreadid; thread->id = sprocsp((void (*)(void *, size_t))func, PR_SALL, (void *)thread->threadid, NULL, 0x100000); if (thread->id == -1) { perror("sproc"); Error("sproc failed"); } //add the thread to the end of the list thread->next = NULL; if (lastthread) { lastthread->next = thread; } else { firstthread = thread; } lastthread = thread; // #ifdef THREAD_DEBUG qprintf("added thread with id %d\n", thread->threadid); #endif //THREAD_DEBUG // currentnumthreads++; currentthreadid++; // ThreadUnlock(); } //end else } //end of the function AddThread
/***************************************************************************\ * xxxHotTrackMenu * * Hot-track a menu item in the menu bar. \***************************************************************************/ BOOL xxxHotTrackMenu(PWND pwnd, UINT nItem, BOOL fDraw) { PMENU pmenu = pwnd->spmenu; PITEM pItem; HDC hdc; UINT oldAlign; TL tlpmenu; CheckLock(pwnd); /* * The window may have lied about the hit-test code on * WM_NCHITTEST. Make sure it does indeed have a menu. */ if (!TestWF(pwnd, WFMPRESENT) || pmenu == NULL) return FALSE; if (nItem >= pmenu->cItems) { RIPMSG0(RIP_WARNING, "xxxHotTrackMenu: menu too large"); return FALSE; } pItem = &pmenu->rgItems[nItem]; /* * Make sure we draw on the right spot */ ThreadLock(pmenu, &tlpmenu); xxxMNRecomputeBarIfNeeded(pwnd, pmenu); ValidateThreadLocks(NULL, PtiCurrent()->ptl, (ULONG_PTR)&tlpmenu, TRUE); if (fDraw) { if (TestMFS(pItem, MF_GRAYED)) { ThreadUnlock(&tlpmenu); return FALSE; } SetMFS(pItem, MFS_HOTTRACK); } else { ClearMFS(pItem, MFS_HOTTRACK); } hdc = _GetDCEx(pwnd, NULL, DCX_WINDOW | DCX_USESTYLE | DCX_CACHE); GreSelectBrush(hdc, SYSHBR(MENUTEXT)); GreSelectFont(hdc, ghMenuFont); oldAlign = GreGetTextAlign(hdc); if (pmenu->rgItems && TestMFT(pmenu->rgItems, MFT_RIGHTORDER)) GreSetTextAlign(hdc, oldAlign | TA_RTLREADING); /* * When the item is not owner draw, xxxDrawMenuItem does not * call back and does not leave the critical section. */ xxxDrawMenuItem(hdc, pmenu, pItem, 0); GreSetTextAlign(hdc, oldAlign); ThreadUnlock(&tlpmenu); _ReleaseDC(hdc); return TRUE; }
// get a new work for thread int GetThreadWork ( void ) { int r; int f; ThreadLock(); if (dispatch >= workcount) { ThreadUnlock(); return -1; } if (pacifier == qtrue) { f = 10 * dispatch / workcount; while ( oldf < f) { oldf++; Sys_Printf ("%i...", oldf); } } r = dispatch; dispatch++; ThreadUnlock (); return r; }
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== int GetThreadWork(void) { int r; int f; ThreadLock(); if (dispatch == workcount) { ThreadUnlock (); return -1; } f = 10*dispatch / workcount; if (f != oldf) { oldf = f; if (pacifier) printf ("%i...", f); } //end if r = dispatch; dispatch++; ThreadUnlock (); return r; } //end of the function GetThreadWork
/** * @brief Return an iteration of work, updating progress when appropriate. */ static int GetThreadWork (void) { int r; int f; ThreadLock(); if (threadstate.workindex == threadstate.workcount) { /* done */ ThreadUnlock(); return -1; } /* update work fraction and output progress if desired */ f = 10 * threadstate.workindex / threadstate.workcount; if (f != threadstate.workfrac) { threadstate.workfrac = f; if (threadstate.progress) { fprintf(stdout, "%i...", f); fflush(stdout); } } else if (threadstate.progress && threadstate.workindex % threadstate.worktick == 0) { fprintf(stdout, "%c\b", "-\\|/"[(threadstate.workindex / threadstate.worktick) & 3]); fflush(stdout); } /* assign the next work iteration */ r = threadstate.workindex; threadstate.workindex++; ThreadUnlock(); return r; }
/* ============= GetThreadWork ============= */ int GetThreadWork (void) { int r; int f; ThreadLock (); if (dispatch == workcount) { ThreadUnlock (); return -1; } f = 10*dispatch / workcount; if (f != oldf) { oldf = f; if (pacifier) { Sys_Printf ("%i...", f); fflush( stdout ); /* ydnar */ } } r = dispatch; dispatch++; ThreadUnlock (); return r; }
static int RemoveDBHandle(CF_DB *dbp) /* Remove a specific DB handle */ { int i; if (!ThreadLock(cft_dbhandle)) { return false; } i = 0; while(OPENDB[i] != dbp) { i++; if(i == MAX_OPENDB) { ThreadUnlock(cft_dbhandle); CfOut(cf_error,"","!! Database handle was not found"); return false; } } // free slot OPENDB[i] = NULL; ThreadUnlock(cft_dbhandle); return true; }
static int GetDBHandle(CF_DB **dbp) /* Return the first unused DB handle in the parameter - NULL if empty */ { int i; if (!ThreadLock(cft_dbhandle)) { return false; } i = 0; while (OPENDB[i] == NULL) { i++; if(i == MAX_OPENDB) { ThreadUnlock(cft_dbhandle); *dbp = NULL; return true; } } *dbp = OPENDB[i]; ThreadUnlock(cft_dbhandle); return true; }
void ScopeClear(const char *ns, const char *name) { assert(name); assert(!ScopeIsReserved(name)); if (!ns) { ns = "default"; } if (!ThreadLock(cft_vscope)) { Log(LOG_LEVEL_ERR, "Could not lock VSCOPE"); return; } Scope *scope = ScopeGet(ns, name); if (!scope) { Log(LOG_LEVEL_DEBUG, "No scope '%s' to clear", name); ThreadUnlock(cft_vscope); return; } HashFree(scope->hashtable); scope->hashtable = HashInit(); Log(LOG_LEVEL_DEBUG, "Scope '%s' cleared", name); ThreadUnlock(cft_vscope); }
static int SaveDBHandle(CF_DB *dbp) { int i; if (!ThreadLock(cft_dbhandle)) { return false; } // find first free slot i = 0; while(OPENDB[i] != NULL) { i++; if(i == MAX_OPENDB) { ThreadUnlock(cft_dbhandle); CfOut(cf_error,"","!! Too many open databases"); return false; } } OPENDB[i] = dbp; ThreadUnlock(cft_dbhandle); return true; }
Scope *ScopeNew(const char *name) { Scope *ptr; CfDebug("Adding scope data %s\n", name); if (!ThreadLock(cft_vscope)) { CfOut(OUTPUT_LEVEL_ERROR, "", "!! Could not lock VSCOPE"); return NULL; } for (ptr = VSCOPE; ptr != NULL; ptr = ptr->next) { if (strcmp(ptr->scope, name) == 0) { ThreadUnlock(cft_vscope); CfDebug("SCOPE Object %s already exists\n", name); return NULL; } } ptr = xcalloc(1, sizeof(Scope)); ptr->next = VSCOPE; ptr->scope = xstrdup(name); ptr->hashtable = HashInit(); VSCOPE = ptr; ThreadUnlock(cft_vscope); return ptr; }
/* * GetThreadWork * * Return an iteration of work, updating progress when appropriate. */ static int GetThreadWork(void){ int r; int f; ThreadLock(); if(thread_work.index == thread_work.count){ // done ThreadUnlock(); return -1; } // update work fraction and output progress if desired f = 10 * thread_work.index / thread_work.count; if(f != thread_work.fraction){ thread_work.fraction = f; if(thread_work.progress && !(verbose || debug)){ Com_Print("%i...", f); fflush(stdout); } } // assign the next work iteration r = thread_work.index; thread_work.index++; ThreadUnlock(); return r; }
void NewScope(const char *name) /* * Thread safe */ { Scope *ptr; CfDebug("Adding scope data %s\n", name); if (!ThreadLock(cft_vscope)) { CfOut(cf_error, "", "!! Could not lock VSCOPE"); return; } for (ptr = VSCOPE; ptr != NULL; ptr = ptr->next) { if (strcmp(ptr->scope, name) == 0) { ThreadUnlock(cft_vscope); CfDebug("SCOPE Object %s already exists\n", name); return; } } ptr = xcalloc(1, sizeof(Scope)); ptr->next = VSCOPE; ptr->scope = xstrdup(name); ptr->hashtable = HashInit(); VSCOPE = ptr; ThreadUnlock(cft_vscope); }
Scope *ScopeNew(const char *name) { Scope *ptr; if (!ThreadLock(cft_vscope)) { Log(LOG_LEVEL_ERR, "Could not lock VSCOPE"); return NULL; } for (ptr = VSCOPE; ptr != NULL; ptr = ptr->next) { if (strcmp(ptr->scope, name) == 0) { ThreadUnlock(cft_vscope); return NULL; } } ptr = xcalloc(1, sizeof(Scope)); ptr->next = VSCOPE; ptr->scope = xstrdup(name); ptr->hashtable = HashInit(); VSCOPE = ptr; ThreadUnlock(cft_vscope); return ptr; }
//get the first node from the front of the node list node_t *NextNodeFromList( void ) { node_t *node; ThreadLock(); numwaiting++; if ( !firstnode ) { if ( numwaiting >= GetNumThreads() ) { ThreadSemaphoreIncrease( GetNumThreads() ); } } //end if ThreadUnlock(); ThreadSemaphoreWait(); ThreadLock(); numwaiting--; node = firstnode; if ( firstnode ) { firstnode = firstnode->next; nodelistsize--; } //end if if ( !firstnode ) { lastnode = NULL; } ThreadUnlock(); return node; } //end of the function NextNodeFromList
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== void AddThread(void (*func)(int)) { thread_t *thread; if (numthreads == 1) { if (currentnumthreads >= numthreads) { return; } currentnumthreads++; func(-1); currentnumthreads--; } //end if else { ThreadLock(); if (currentnumthreads >= numthreads) { ThreadUnlock(); return; } //end if //allocate new thread thread = GetMemory(sizeof(thread_t)); if (!thread) { Error("can't allocate memory for thread\n"); } // thread->threadid = currentthreadid; if (pthread_create(&thread->thread, attrib, (pthread_startroutine_t)func, (pthread_addr_t)thread->threadid) == -1) { Error("pthread_create failed"); } //add the thread to the end of the list thread->next = NULL; if (lastthread) { lastthread->next = thread; } else { firstthread = thread; } lastthread = thread; // #ifdef THREAD_DEBUG qprintf("added thread with id %d\n", thread->threadid); #endif //THREAD_DEBUG // currentnumthreads++; currentthreadid++; // ThreadUnlock(); } //end else } //end of the function AddThread
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== void WaitForAllThreadsFinished( void ) { ThreadLock(); while ( firstthread ) { ThreadUnlock(); //wait (NULL); ThreadLock(); } //end while ThreadUnlock(); } //end of the function WaitForAllThreadsFinished
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== void AddThread( void ( *func )(int) ) { thread_t *thread; if ( numthreads == 1 ) { if ( currentnumthreads >= numthreads ) { return; } currentnumthreads++; func( -1 ); currentnumthreads--; } //end if else { ThreadLock(); if ( currentnumthreads >= numthreads ) { ThreadUnlock(); return; } //end if //allocate new thread thread = GetMemory( sizeof( thread_t ) ); if ( !thread ) { Error( "can't allocate memory for thread\n" ); } // thread->threadid = currentthreadid; thread->handle = CreateThread( NULL, // LPSECURITY_ATTRIBUTES lpsa, 0, // DWORD cbStack, (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr, (LPVOID) thread->threadid, // LPVOID lpvThreadParm, 0, // DWORD fdwCreate, &thread->id ); //add the thread to the end of the list thread->next = NULL; if ( lastthread ) { lastthread->next = thread; } else { firstthread = thread;} lastthread = thread; // #ifdef THREAD_DEBUG qprintf( "added thread with id %d\n", thread->threadid ); #endif //THREAD_DEBUG // currentnumthreads++; currentthreadid++; // ThreadUnlock(); } //end else } //end of the function AddThread
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== void WaitForAllThreadsFinished( void ) { HANDLE handle; ThreadLock(); while ( firstthread ) { handle = firstthread->handle; ThreadUnlock(); WaitForSingleObject( handle, INFINITE ); ThreadLock(); } //end while ThreadUnlock(); } //end of the function WaitForAllThreadsFinished
static int Unix_cf_pclose(FILE *pp) { int fd; pid_t pid; CfDebug("Unix_cf_pclose(pp)\n"); if (!ThreadLock(cft_count)) { return -1; } if (CHILDREN == NULL) /* popen hasn't been called */ { ThreadUnlock(cft_count); return -1; } ThreadUnlock(cft_count); ALARM_PID = -1; fd = fileno(pp); if (fd >= MAX_FD) { CfOut(cf_error, "", "File descriptor %d of child higher than MAX_FD in Unix_cf_pclose, check for defunct children", fd); pid = -1; } else { if ((pid = CHILDREN[fd]) == 0) { return -1; } ThreadLock(cft_count); CHILDREN[fd] = 0; ThreadUnlock(cft_count); } if (fclose(pp) == EOF) { return -1; } return cf_pwait(pid); }
Rlist *RlistAppendRval(Rlist **start, Rval rval) { Rlist *rp = xmalloc(sizeof(Rlist)); if (*start == NULL) { *start = rp; } else { Rlist *lp = *start; while (lp->next != NULL) { lp = lp->next; } lp->next = rp; } rp->val = rval; ThreadLock(cft_lock); rp->next = NULL; ThreadUnlock(cft_lock); return rp; }
void ScopeDeleteAll() { Scope *ptr, *this; CfDebug("Deleting all scoped variables\n"); if (!ThreadLock(cft_vscope)) { CfOut(OUTPUT_LEVEL_ERROR, "", "!! Could not lock VSCOPE"); return; } ptr = VSCOPE; while (ptr != NULL) { this = ptr; CfDebug(" -> Deleting scope %s\n", ptr->scope); HashFree(this->hashtable); free(this->scope); ptr = this->next; free((char *) this); } VSCOPE = NULL; SCOPE_CURRENT = NULL; ThreadUnlock(cft_vscope); }
void ScopeDeleteAll() { Scope *ptr, *this; if (!ThreadLock(cft_vscope)) { Log(LOG_LEVEL_ERR, "Could not lock VSCOPE"); return; } ptr = VSCOPE; while (ptr != NULL) { this = ptr; HashFree(this->hashtable); free(this->scope); ptr = this->next; free((char *) this); } VSCOPE = NULL; ThreadUnlock(cft_vscope); }
static void ScopeDelete(Scope *scope) { if (!ThreadLock(cft_vscope)) { Log(LOG_LEVEL_ERR, "Could not lock VSCOPE"); return; } Scope *prev = NULL; for (Scope *curr = VSCOPE; curr; prev = curr, curr = curr->next) { if (curr != scope) { continue; } if (!prev) { VSCOPE = scope->next; } else { prev->next = curr->next; } free(scope->scope); HashFree(scope->hashtable); free(scope); break; } ThreadUnlock(cft_vscope); }
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== void RemoveThread(int threadid) { thread_t *thread, *last; //if a single thread if (threadid == -1) return; // ThreadLock(); last = NULL; for (thread = firstthread; thread; thread = thread->next) { if (thread->threadid == threadid) { if (last) last->next = thread->next; else firstthread = thread->next; if (!thread->next) lastthread = last; // FreeMemory(thread); currentnumthreads--; #ifdef THREAD_DEBUG qprintf("removed thread with id %d\n", threadid); #endif //THREAD_DEBUG break; } //end if last = thread; } //end if if (!thread) Error("couldn't find thread with id %d", threadid); ThreadUnlock(); } //end of the function RemoveThread
void RvalDestroy(Rval rval) { if (rval.item == NULL) { return; } switch (rval.type) { case RVAL_TYPE_SCALAR: ThreadLock(cft_lock); free(RvalScalarValue(rval)); ThreadUnlock(cft_lock); return; case RVAL_TYPE_LIST: RlistDestroy(RvalRlistValue(rval)); return; case RVAL_TYPE_FNCALL: FnCallDestroy(RvalFnCallValue(rval)); break; case RVAL_TYPE_CONTAINER: JsonDestroy(RvalContainerValue(rval)); break; case RVAL_TYPE_NOPROMISEE: return; } }
void LastSaw(char *username, char *ipaddress, unsigned char digest[EVP_MAX_MD_SIZE + 1], enum roles role) { char databuf[CF_BUFSIZE]; char *mapip; if (strlen(ipaddress) == 0) { CfOut(cf_inform, "", "LastSeen registry for empty IP with role %d", role); return; } ThreadLock(cft_output); switch (role) { case cf_accept: snprintf(databuf, CF_BUFSIZE - 1, "-%s", HashPrint(CF_DEFAULT_DIGEST, digest)); break; case cf_connect: snprintf(databuf, CF_BUFSIZE - 1, "+%s", HashPrint(CF_DEFAULT_DIGEST, digest)); break; } ThreadUnlock(cft_output); mapip = MapAddress(ipaddress); UpdateLastSawHost(databuf, mapip); }
Rlist *RlistAppendAlien(Rlist **start, void *item) /* Allocates new memory for objects - careful, could leak! */ { Rlist *rp, *lp = *start; rp = xmalloc(sizeof(Rlist)); if (*start == NULL) { *start = rp; } else { for (lp = *start; lp->next != NULL; lp = lp->next) { } lp->next = rp; } rp->item = item; rp->type = RVAL_TYPE_SCALAR; ThreadLock(cft_lock); rp->next = NULL; ThreadUnlock(cft_lock); return rp; }
/* ============= GetNextPortal Returns the next portal for a thread to work on Returns the portals from the least complex, so the later ones can reuse the earlier information. ============= */ portal_t *GetNextPortal (void) { int j; portal_t *p, *tp; int min; int i; i = GetThreadWork (); // bump the pacifier if (i == -1) return NULL; ThreadLock(); min = 99999; p = NULL; for (j=0, tp = portals ; j<numportals*2 ; j++, tp++) { if (tp->nummightsee < min && tp->status == stat_none) { min = tp->nummightsee; p = tp; } } if (p) p->status = stat_working; ThreadUnlock(); return p; }
/* ============= AllocWinding ============= */ winding_t *AllocWinding (int points) { winding_t *w; if (numthreads == 1) { c_winding_allocs++; c_winding_points += points; c_active_windings++; if (c_active_windings > c_peak_windings) c_peak_windings = c_active_windings; } ThreadLock(); if (winding_pool[points]) { w = winding_pool[points]; winding_pool[points] = w->next; } else { w = (winding_t *)malloc(sizeof(*w)); w->p = (Vector *)calloc( points, sizeof(Vector) ); } ThreadUnlock(); w->numpoints = 0; // None are occupied yet even though allocated. w->maxpoints = points; w->next = NULL; return w; }