int main(void) { report_start(START_CMPLT); //assuredly_misbehave((rand() % 521) % MISBEHAVE_MAX); misbehave(BGND_BRWN >> FGND_CYAN); // for landslide ERR(thr_init(STACK_SIZE)); ERR(mutex_init(&lock1)); ERR(mutex_init(&lock2)); ERR(cond_init(&cvar1)); ERR(cond_init(&cvar2)); ERR(thr_create(thread1, NULL)); report_misc("thread1 created"); /* Wait for thread1 to get to sleep on cvar1. */ mutex_lock(&lock1); while (!slept1) { mutex_unlock(&lock1); thr_yield(-1); mutex_lock(&lock1); } /* Indicate that we are about to signal */ signaled1 = 1; mutex_unlock(&lock1); /* Signal. Note that we know for sure that thread1 is asleep on * cvar1 (assuming correct cond vars and mutexes...) */ cond_signal(&cvar1); report_misc("cvar1 signaled"); //sleep(10); /* Now do it all again for the second set of things. */ /* Wait for thread1 to get to sleep on cvar2. */ mutex_lock(&lock2); while (!slept2) { mutex_unlock(&lock2); thr_yield(-1); mutex_lock(&lock2); } /* Indicate that we are about to signal */ signaled2 = 1; mutex_unlock(&lock2); /* Signal. Note that we know for sure that thread1 is asleep on * cvar1 (assuming correct cond vars and mutexes...) */ cond_signal(&cvar2); report_misc("cvar2 signaled"); /* We report success from the other thread. */ return 0; }
/****************************************************************** * KpThreadYield (Solaris Version) * * Description * * This function causes the calling thread to yield control to * another thread if one of equal or higher priority is ready * to run. * * Author * mjb * * Created * July 5, 1994 *****************************************************************************/ void KpThreadYield (void) { thr_yield (); } /* KpThreadYield */
// // Start performing I/O. // void Grunt::Begin_IO() { if ( !target_count || idle ) return; // Wait for all threads to finish opening their devices. while ( not_ready ) { #if defined(IOMTR_OSFAMILY_NETWARE) pthread_yield(); #elif defined(IOMTR_OSFAMILY_UNIX) #if defined(IOMTR_OS_SOLARIS) thr_yield(); #elif defined(IOMTR_OS_LINUX) sleep(0); #else #warning ===> WARNING: You have to do some coding here to get the port done! #endif #elif defined(IOMTR_OSFAMILY_WINDOWS) Sleep( 0 ); #else #warning ===> WARNING: You have to do some coding here to get the port done! #endif } InterlockedExchange( (long *) &grunt_state, TestRampingUp ); }
int ldap_pvt_thread_yield( void ) { #ifdef REPLACE_BROKEN_YIELD #ifdef HAVE_NANOSLEEP struct timespec t = { 0, 0 }; nanosleep(&t, NULL); #else struct timeval tv = {0,0}; select( 0, NULL, NULL, NULL, &tv ); #endif return 0; #elif defined(HAVE_THR_YIELD) thr_yield(); return 0; #elif HAVE_PTHREADS == 10 return sched_yield(); #elif defined(_POSIX_THREAD_IS_GNU_PTH) sched_yield(); return 0; #elif HAVE_PTHREADS == 6 pthread_yield(NULL); return 0; #else pthread_yield(); return 0; #endif }
void * work (void *n) { int ni = (int) n; while (1) { thr_yield (); count[ni]++; } return 0; }
void OSThread::ThreadYield() { // on platforms who's threading is not pre-emptive yield // to another thread #if THREADING_IS_COOPERATIVE #if __PTHREADS__ #if USE_THR_YIELD thr_yield(); #else sched_yield(); #endif #endif #endif }
int main(void) { report_start(START_CMPLT); ERR(thr_init(4096)); int tid = thr_create(child, NULL); thr_yield(-1); int value = __sync_fetch_and_add(&count, 1); assert(value == 0 && "causality violation"); ERR(thr_join(tid, NULL)); return 0; }
void * my_thread(void *arg) { int oldstate; if (arg == server) { while (fd == -1) thr_yield(); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate); Door_bind(fd); Door_return(NULL, 0, NULL, 0); } else err_quit("my_thread: unknown function"); return(NULL); /* never executed */ }
gpg_err_code_t gpgrt_yield (void) { #if USE_POSIX_THREADS # ifdef _POSIX_PRIORITY_SCHEDULING sched_yield (); # else return GPG_ERR_NOT_SUPPORTED; # endif #elif USE_SOLARIS_THREADS thr_yield (); #else return GPG_ERR_NOT_SUPPORTED; #endif return 0; }
/* * Get a free event and check to see if more are needed. */ robo_event_t * get_free_event( library_t *library) { robo_event_t *ret; char *ent_pnt = "get_free_event"; mutex_lock(&library->free_mutex); if (library->free_count < 20 && !library->inc_free_running) { sigset_t signal_set; (void) sigemptyset(&signal_set); (void) sigaddset(&signal_set, SIGEMT); library->inc_free_running++; thr_sigsetmask(SIG_BLOCK, &signal_set, NULL); thr_create(NULL, MD_THR_STK, &inc_free, (void *)library, (THR_DETACHED | THR_BOUND), NULL); thr_sigsetmask(SIG_UNBLOCK, &signal_set, NULL); thr_yield(); } while (library->free_count <= 0) { mutex_unlock(&library->free_mutex); if (DBG_LVL(SAM_DBG_DEBUG)) sam_syslog(LOG_DEBUG, "%s: Waiting for free event.", ent_pnt); sleep(2); mutex_lock(&library->free_mutex); } ret = library->free; ETRACE((LOG_NOTICE, "EV:LfGf: %#x.", ret)); library->free_count--; library->free = ret->next; mutex_unlock(&library->free_mutex); return (ret); }
void PGPThreadYield() { thr_yield(); }
void CVMthreadYield(void) { thr_yield(); }
/* Yield our process time to another thread. */ void __objc_thread_yield(void) { thr_yield(); }
//#ifdef _MT void runthreads(long sleep_cnt, int min_threads, int max_threads, int chperthread, int num_rounds) { thread_data *de_area = new thread_data[max_threads] ; thread_data *pdea; int nperthread ; int sum_threads ; unsigned long sum_allocs ; unsigned long sum_frees ; double duration ; #ifdef __WIN32__ _LARGE_INTEGER ticks_per_sec, start_cnt, end_cnt; #else long ticks_per_sec ; long start_cnt, end_cnt ; #endif _int64 ticks ; double rate_1=0, rate_n ; double reqd_space ; ULONG used_space ; int prevthreads ; int i ; QueryPerformanceFrequency( &ticks_per_sec ) ; pdea = &de_area[0] ; memset(&de_area[0], 0, sizeof(thread_data)) ; prevthreads = 0 ; for(num_threads=min_threads; num_threads <= max_threads; num_threads++ ) { warmup(&blkp[prevthreads*chperthread], (num_threads-prevthreads)*chperthread ); nperthread = chperthread ; stopflag = FALSE ; for(i=0; i< num_threads; i++){ de_area[i].threadno = i+1 ; de_area[i].NumBlocks = num_rounds*nperthread; de_area[i].array = &blkp[i*nperthread] ; de_area[i].blksize = &blksize[i*nperthread] ; de_area[i].asize = nperthread ; de_area[i].min_size = min_size ; de_area[i].max_size = max_size ; de_area[i].seed = lran2(&rgen) ; ; de_area[i].finished = 0 ; de_area[i].cAllocs = 0 ; de_area[i].cFrees = 0 ; de_area[i].cThreads = 0 ; de_area[i].finished = FALSE ; lran2_init(&de_area[i].rgen, de_area[i].seed) ; #ifdef __WIN32__ _beginthread((void (__cdecl*)(void *)) exercise_heap, 0, &de_area[i]) ; #else _beginthread(exercise_heap, 0, &de_area[i]) ; #endif } QueryPerformanceCounter( &start_cnt) ; // printf ("Sleeping for %ld seconds.\n", sleep_cnt); Sleep(sleep_cnt * 1000L) ; stopflag = TRUE ; for(i=0; i<num_threads; i++){ while( !de_area[i].finished ){ #ifdef __WIN32__ Sleep(1); #elif defined(__SVR4) thr_yield(); #else sched_yield(); #endif } } QueryPerformanceCounter( &end_cnt) ; sum_frees = sum_allocs =0 ; sum_threads = 0 ; for(i=0;i< num_threads; i++){ sum_allocs += de_area[i].cAllocs ; sum_frees += de_area[i].cFrees ; sum_threads += de_area[i].cThreads ; de_area[i].cAllocs = de_area[i].cFrees = 0; } #ifdef __WIN32__ ticks = end_cnt.QuadPart - start_cnt.QuadPart ; duration = (double)ticks/ticks_per_sec.QuadPart ; #else ticks = end_cnt - start_cnt ; duration = (double)ticks/ticks_per_sec ; #endif for( i=0; i<num_threads; i++){ if( !de_area[i].finished ) printf("Thread at %d not finished\n", i) ; } rate_n = sum_allocs/duration ; if( rate_1 == 0){ rate_1 = rate_n ; } reqd_space = (0.5*(min_size+max_size)*num_threads*chperthread) ; // used_space = CountReservedSpace() - init_space; used_space = 0; printf ("Throughput = %8.0f operations per second.\n", sum_allocs / duration); #if 0 printf("%2d ", num_threads ) ; printf("%6.3f", duration ) ; printf("%6.3f", rate_n/rate_1 ) ; printf("%8.0f", sum_allocs/duration ) ; printf(" %6.3f %.3f", (double)used_space/(1024*1024), used_space/reqd_space) ; printf("\n") ; #endif Sleep(5000L) ; // wait 5 sec for old threads to die prevthreads = num_threads ; printf ("Done sleeping...\n"); } delete [] de_area; }
int main(int argc, char **argv) { int what_signal, i; char *ent_pnt = "main"; char logname[20]; char *l_mess, *lc_mess; sigset_t sigwait_set; struct sigaction sig_action; dev_ptr_tbl_t *dev_ptr_tbl; shm_ptr_tbl_t *shm_ptr_tbl; sam_defaults_t *defaults; if (argc != 4) exit(1); initialize_fatal_trap_processing(SOLARIS_THREADS, fatal_cleanup); CustmsgInit(1, NULL); library = (library_t *)malloc_wait(sizeof (library_t), 2, 0); (void) memset(library, 0, sizeof (library_t)); /* * Crack the arguments */ argv++; master_shm.shmid = atoi(*argv); argv++; preview_shm.shmid = atoi(*argv); argv++; library->eq = atoi(*argv); mypid = getpid(); if ((master_shm.shared_memory = shmat(master_shm.shmid, NULL, 0774)) == (void *)-1) exit(2); shm_ptr_tbl = (shm_ptr_tbl_t *)master_shm.shared_memory; if ((preview_shm.shared_memory = shmat(preview_shm.shmid, NULL, 0774)) == (void *)-1) exit(3); fifo_path = strdup(SHM_REF_ADDR(shm_ptr_tbl->fifo_path)); sprintf(logname, "sony-%d", library->eq); defaults = GetDefaults(); openlog(logname, LOG_PID | LOG_NOWAIT, defaults->log_facility); dev_ptr_tbl = (dev_ptr_tbl_t *)SHM_REF_ADDR( ((shm_ptr_tbl_t *)master_shm.shared_memory)->dev_table); /* LINTED pointer cast may result in improper alignment */ library->un = (dev_ent_t *) SHM_REF_ADDR(dev_ptr_tbl->d_ent[library->eq]); /* LINTED pointer cast may result in improper alignment */ library->help_msg = (sony_priv_mess_t *) SHM_REF_ADDR(library->un->dt.rb.private); l_mess = library->un->dis_mes[DIS_MES_NORM]; lc_mess = library->un->dis_mes[DIS_MES_CRIT]; /* check if we should log sef data */ (void) sef_status(); if (DBG_LVL(SAM_DBG_RBDELAY)) { int ldk = 60; sam_syslog(LOG_DEBUG, "Waiting for 60 seconds."); while (ldk > 0 && DBG_LVL(SAM_DBG_RBDELAY)) { sprintf(lc_mess, "waiting for %d seconds pid %d", ldk, mypid); sleep(10); ldk -= 10; } *lc_mess = '\0'; } mutex_init(&library->mutex, USYNC_THREAD, NULL); /* * Hold the lock until initialization is complete */ mutex_lock(&library->mutex); common_init(library->un); mutex_init(&library->help_msg->mutex, USYNC_PROCESS, NULL); cond_init(&library->help_msg->cond_i, USYNC_PROCESS, NULL); cond_init(&library->help_msg->cond_r, USYNC_PROCESS, NULL); library->help_msg->mtype = SONY_PRIV_VOID; /* * Start the main threads */ if (thr_create(NULL, DF_THR_STK, monitor_msg, (void *)library, (THR_BOUND | THR_NEW_LWP | THR_DETACHED), &threads[SONY_MSG_THREAD])) { sam_syslog(LOG_ERR, "Unable to start thread monitor_msg: %m.\n"); thr_exit(NULL); } if (thr_create(NULL, MD_THR_STK, manage_list, (void *)library, (THR_BOUND |THR_NEW_LWP | THR_DETACHED), &threads[SONY_WORK_THREAD])) { sam_syslog(LOG_ERR, "Unable to start thread manage_list: %m.\n"); thr_kill(threads[SONY_MSG_THREAD], SIGINT); thr_exit(NULL); } mutex_lock(&library->un->mutex); library->un->dt.rb.process = getpid(); library->un->status.b.ready = FALSE; library->un->status.b.present = FALSE; mutex_unlock(&library->un->mutex); /* * Initialize the library. This will release the library mutex. */ memccpy(l_mess, catgets(catfd, SET, 9065, "initializing"), '\0', DIS_MES_LEN); if (initialize(library, dev_ptr_tbl)) thr_exit(NULL); /* * Now let the other threads run */ thr_yield(); mutex_lock(&library->mutex); i = 30; { char *MES_9155 = catgets(catfd, SET, 9155, "waiting for %d drives to initialize"); char *mes = (char *)malloc_wait(strlen(MES_9155) + 15, 5, 0); while (library->countdown && i-- > 0) { sprintf(mes, MES_9155, library->countdown); memccpy(l_mess, mes, '\0', DIS_MES_LEN); sam_syslog(LOG_INFO, catgets(catfd, SET, 9156, "%s: Waiting for %d drives to initialize."), ent_pnt, library->countdown); mutex_unlock(&library->mutex); sleep(10); mutex_lock(&library->mutex); } free(mes); } if (i <= 0) sam_syslog(LOG_INFO, catgets(catfd, SET, 9157, "%s: %d drive(s) did not initialize."), ent_pnt, library->countdown); mutex_unlock(&library->mutex); memccpy(l_mess, "running", '\0', DIS_MES_LEN); mutex_lock(&library->un->mutex); library->un->status.b.audit = FALSE; library->un->status.b.requested = FALSE; library->un->status.b.mounted = TRUE; library->un->status.b.ready = TRUE; mutex_unlock(&library->un->mutex); /* * Now that the daemon is fully initialized, the main thread is * just used to monitor the thread state and an indication of shutdown. * This is accomplished using the signals SIGALRM, SIGINT, and SIGTERM. * This is not done with a signal handler, but using the sigwait() call. */ sigemptyset(&sigwait_set); sigaddset(&sigwait_set, SIGINT); sigaddset(&sigwait_set, SIGTERM); sigaddset(&sigwait_set, SIGALRM); /* want to restart system calls */ sig_action.sa_handler = SIG_DFL; sigemptyset(&sig_action.sa_mask); sig_action.sa_flags = SA_RESTART; sigaction(SIGINT, &sig_action, NULL); sigaction(SIGTERM, &sig_action, NULL); sigaction(SIGALRM, &sig_action, NULL); for (;;) { alarm(20); what_signal = sigwait(&sigwait_set); switch (what_signal) { case SIGALRM: if ((threads[SONY_MSG_THREAD] == (thread_t)-1) || (threads[SONY_WORK_THREAD] == (thread_t)-1)) { /* * If any of the processing threads * have disappeared, log the * fact, and take a core dump */ sam_syslog(LOG_INFO, "%s: SIGALRM: Thread(s) gone.", ent_pnt); abort(); } break; /* * For a normal shutdown of the robot daemon: * 1) prevent the alarm from going off during * shutdown and causing a core dump * 2) log the reason we are shutting down * 3) kill the helper pid if there is one * 4) terminate the connection to the catalog * 5) terminate all of the processing threads */ case SIGINT: case SIGTERM: sigdelset(&sigwait_set, SIGALRM); sam_syslog(LOG_INFO, "%s: Shutdown by signal %d", ent_pnt, what_signal); if (library->helper_pid > 0) { kill(library->helper_pid, 9); } kill_off_threads(library); exit(0); break; default: break; } } }
void *race(void *arg) { int err; int col = -1; int i; int row = (int)arg; mutex_lock(&lock_print); color += get_ticks(); set_term_color(color); expect(set_cursor_pos(row, 0)); printf("%s", &empty[0]); mutex_unlock(&lock_print); while(1) { sem_wait(&sem); mutex_lock(&lock_running); int r = ++running; mutex_unlock(&lock_running); if(r > sems) lprintf("BAD! I have %d > %d threads running", r, sems); mutex_lock(&lock_print); expect(set_cursor_pos(24, 77)); printf("%.2d", running); mutex_unlock(&lock_print); for(i = 0; i < run; i++) { col++; if(80 == col) { mutex_lock(&lock_print); color += get_ticks(); set_term_color(color); expect(set_cursor_pos(row, 0)); printf("%s", &empty[0]); mutex_unlock(&lock_print); col = 0; } mutex_lock(&lock_print); color += get_ticks(); set_term_color(color); expect(set_cursor_pos(row, col)); printf("%c", alphabet[i]); mutex_unlock(&lock_print); if(pause) expect(sleep(pause)); thr_yield(-1); } mutex_lock(&lock_running); running--; mutex_unlock(&lock_running); sem_signal(&sem); } }
int ldap_pvt_thread_yield( void ) { thr_yield(); return 0; }
int main(int argc, char **argv) { int i, n, maxnconn; pthread_t tid; struct file *fptr; if (argc < 5) err_quit("usage: web <#conns> <IPaddr> <homepage> file1 ..."); maxnconn = atoi(argv[1]); nfiles = min(argc - 4, MAXFILES); for (i = 0; i < nfiles; i++) { file[i].f_name = argv[i + 4]; file[i].f_host = argv[2]; file[i].f_flags = 0; } printf("nfiles = %d\n", nfiles); home_page(argv[2], argv[3]); nlefttoread = nlefttoconn = nfiles; nconn = 0; while (nlefttoread > 0) { /* printf("nconn = %d, nlefttoconn = %d\n", nconn, nlefttoconn); */ while (nconn < maxnconn && nlefttoconn > 0) { /* find a file to read */ for (i = 0 ; i < nfiles; i++) if (file[i].f_flags == 0) break; if (i == nfiles) err_quit("nlefttoconn = %d but nothing found", nlefttoconn); if ( (n = pthread_create(&tid, NULL, &do_get_read, &file[i])) != 0) errno = n, err_sys("pthread_create error"); printf("created thread %d\n", tid); file[i].f_tid = tid; file[i].f_flags = F_CONNECTING; nconn++; nlefttoconn--; } thr_yield(); /* See if one of the threads is done */ if ( (n = pthread_mutex_lock(&ndone_mutex)) != 0) errno = n, err_sys("pthread_mutex_lock error"); if (ndone > 0) { for (i = 0; i < nfiles; i++) { if (file[i].f_flags & F_DONE) { if ( (n = pthread_join(file[i].f_tid, (void **) &fptr)) != 0) errno = n, err_sys("pthread_join error"); if (&file[i] != fptr) err_quit("file[i] != fptr"); fptr->f_flags = F_JOINED; /* clears F_DONE */ ndone--; nconn--; nlefttoread--; printf("thread id %d for %s done\n", file[i].f_tid, fptr->f_name); } } } if ( (n = pthread_mutex_unlock(&ndone_mutex)) != 0) errno = n, err_sys("pthread_mutex_unlock error"); } exit(0); }