static switch_status_t timer_next(switch_timer_t *timer) { timer_private_t *private_info = timer->private_info; #ifdef DISABLE_1MS_COND int cond_index = timer->interval; #else int cond_index = 1; #endif int delta = (int) (private_info->reference - TIMER_MATRIX[timer->interval].tick); /* sync up timer if it's not been called for a while otherwise it will return instantly several times until it catches up */ if (delta < -1) { private_info->reference = timer->tick = TIMER_MATRIX[timer->interval].tick; } timer_step(timer); if (!MATRIX) { do_sleep(1000 * timer->interval); goto end; } while (globals.RUNNING == 1 && private_info->ready && TIMER_MATRIX[timer->interval].tick < private_info->reference) { check_roll(); if (globals.timer_count >= runtime.tipping_point) { os_yield(); globals.use_cond_yield = 0; } else { if (globals.use_cond_yield == 1) { switch_mutex_lock(TIMER_MATRIX[cond_index].mutex); if (TIMER_MATRIX[timer->interval].tick < private_info->reference) { switch_thread_cond_wait(TIMER_MATRIX[cond_index].cond, TIMER_MATRIX[cond_index].mutex); } switch_mutex_unlock(TIMER_MATRIX[cond_index].mutex); } else { do_sleep(1000); } } } end: return globals.RUNNING == 1 ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE; }
void HEADBAND::all_on() { int led; for (led = 0; led < _nr_leds; led++) { _leds[led].on(); } do_sleep(64); for (led = 0; led < _nr_leds; led++) { _leds[led].off(); } }
//---------- philosophers using monitor (condition variable) ---------------------- int philosopher_using_condvar(void * arg) { /* arg is the No. of philosopher 0~N-1*/ int i, iter=0; i=(int)arg; cprintf("I am No.%d philosopher_condvar\n",i); while(iter++<TIMES) { /* iterate*/ cprintf("Iter %d, No.%d philosopher_condvar is thinking\n",iter,i); /* thinking*/ do_sleep(SLEEP_TIME); phi_take_forks_condvar(i); /* need two forks, maybe blocked */ cprintf("Iter %d, No.%d philosopher_condvar is eating\n",iter,i); /* eating*/ do_sleep(SLEEP_TIME); phi_put_forks_condvar(i); /* return two forks back*/ } cprintf("No.%d philosopher_condvar quit\n",i); return 0; }
int main(int argc,char *argv[]) { char out; int i; #ifdef LOCK_LOCKFILE if(argc!=3) { fprintf(stderr,"usage:%s character lockfile\n",argv[0]); exit(1); } lockfile=argv[2]; if(!(fname=malloc(strlen(lockfile)+16))) { perror("malloc()"); exit(1); } sprintf(fname,"%s%d",lockfile,getpid()); signal(SIGINT,handler); /* Zajistit zrusení zámku pri násilném ukoncení */ signal(SIGTERM,handler); signal(SIGQUIT,handler); signal(SIGHUP,handler); #else if(argc!=2) { fprintf(stderr,"usage: %s character\n",argv[0]); exit(1); } #endif out=argv[1][0]; for(;;) { LOCK for(i=0;i<LINE_LEN;i++) { if(write(1,&out,1)==-1) { perror("write()"); exit(1); } do_sleep(SLEEP_CHAR); } if(write(1,"\n",1)==-1) { perror("write()"); exit(1); } UNLOCK do_sleep(SLEEP_LINE); } }
int philosopher_using_semaphore(void * arg) /* i:哲学家号码,从0到N-1 */ { int i, iter=0; i=(int)arg; cprintf("I am No.%d philosopher_sema\n",i); while(iter++<TIMES) { /* 无限循环 */ cprintf("Iter %d, No.%d philosopher_sema is thinking\n",iter,i); /* 哲学家正在思考 */ do_sleep(SLEEP_TIME); phi_take_forks_sema(i); /* 需要两只叉子,或者阻塞 */ cprintf("Iter %d, No.%d philosopher_sema is eating\n",iter,i); /* 进餐 */ do_sleep(SLEEP_TIME); phi_put_forks_sema(i); /* 把两把叉子同时放回桌子 */ } cprintf("No.%d philosopher_sema quit\n",i); return 0; }
/* idle task */ static void idle_task(void) { while(1) { #ifdef BUILD_ARM_BB do_work(); #else do_sleep(); #endif } }
void update_target (CHAR_DATA *tch, int affect_type) { if ( affect_type == MAGIC_AFFECT_SLEEP ) { do_sleep (tch, "", 0); if ( tch->fighting ) { if ( tch->fighting == tch ) stop_fighting (tch->fighting); stop_fighting (tch); } } }
ATMO_BOOL CFnordlichtConnection::reset(unsigned char addr) { if(m_hComport == INVALID_HANDLE_VALUE) return ATMO_FALSE; stop(255); if ( sync() && start_bootloader(addr) ) { #if defined(_ATMO_VLC_PLUGIN_) do_sleep(200000); // wait 200ms #else do_sleep(200); // wait 200ms #endif if ( sync() && boot_enter_application(addr) ) return ATMO_TRUE; } return ATMO_FALSE; }
void HEADBAND::blink_all(int nr_times) { int led, nr_blinks; for (nr_blinks = 0; nr_blinks < nr_times; nr_blinks++) { for (led = 0; led < _nr_leds; led++) { _leds[led].on(); } do_sleep(10); for (led = 0; led < _nr_leds; led++) { _leds[led].off(); } } }
int body() { char c; printf("proc %d starts from body()\n", running->pid); while(1) { printf("-----------------------------------------\n"); //prints the list of procs that are initializedd and free printf("freelist = "); printQueue(freeList);// optional: show the freeList //print ready queue which are procs that are ready printf("readyQueue = "); printQueue(readyQueue); // show the readyQueue //printf("sleepList = "); //printQueue(sleepList); printf("-----------------------------------------\n"); printf("proc %d running: parent=%d\n",running->pid,running->ppid); printf("enter a char [s|f|q z|a|w] : "); c = getc(); printf("%c\n", c); switch(c) { case 'f': //fork a child off of the current process do_kfork(); break; case 's': //switch to next process in ready queue do_tswitch(); break; case 'q': //zombie the current process do_exit(); break; case 'z': //running PROC go to sleep on an event value do_sleep(); break; case 'a': //wakeup all PROCs sleeping on event do_wakeup(); break; case 'w': //to wait for a ZOMBIE child do_wait(); break; } } }
static switch_status_t timer_init(switch_timer_t *timer) { timer_private_t *private_info; int sanity = 0; while (globals.STARTED == 0) { do_sleep(100000); if (++sanity == 300) { abort(); } } if (globals.RUNNING != 1 || !globals.mutex || timer->interval < 1) { return SWITCH_STATUS_FALSE; } if ((private_info = switch_core_alloc(timer->memory_pool, sizeof(*private_info)))) { switch_mutex_lock(globals.mutex); if (!TIMER_MATRIX[timer->interval].mutex) { switch_mutex_init(&TIMER_MATRIX[timer->interval].mutex, SWITCH_MUTEX_NESTED, module_pool); switch_thread_cond_create(&TIMER_MATRIX[timer->interval].cond, module_pool); } TIMER_MATRIX[timer->interval].count++; switch_mutex_unlock(globals.mutex); timer->private_info = private_info; private_info->start = private_info->reference = TIMER_MATRIX[timer->interval].tick; private_info->start -= 2; /* switch_core_timer_init sets samplecount to samples, this makes first next() step once */ private_info->roll = TIMER_MATRIX[timer->interval].roll; private_info->ready = 1; if (timer->interval > 0 && timer->interval < MS_PER_TICK) { MS_PER_TICK = timer->interval; STEP_MS = 1; STEP_MIC = 1000; TICK_PER_SEC = 10000; switch_time_sync(); } switch_mutex_lock(globals.mutex); globals.timer_count++; if (globals.timer_count == (runtime.tipping_point + 1)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Crossed tipping point of %u, shifting into high-gear.\n", runtime.tipping_point); } switch_mutex_unlock(globals.mutex); return SWITCH_STATUS_SUCCESS; } return SWITCH_STATUS_MEMERR; }
// // void test_1() // // This test insures that Timers are constructed properly. // static void test_1() { spica::Timer object_1; // Sleep for a while. If the timer is running (shouldn't be) let's // give it a chance to accumulate some time. // std::cout << "Test #1: Sleeping for 5 seconds..." << std::endl; do_sleep(5); // It should have just sat there. std::cout << " object_1's time is: " << object_1.time() << "ms (should be zero)" << std::endl; }
/** @brief Pauses the writes, so that you can manipulate the base object (the formatters/destinations, for instance) After this function has been called, you can be @b sure that the other (dedicated) thread is not writing any messagges. In other words, the other thread is not manipulating the base object (formatters/destinations, for instance), but you can do it. FIXME allow a timeout as well */ void pause() { { scoped_lock lk( non_const_context_base::context().cs); non_const_context_base::context().is_paused = true; non_const_context_base::context().pause_acknowledged = false; } while ( true) { do_sleep(10); scoped_lock lk( non_const_context_base::context().cs); if ( non_const_context_base::context().pause_acknowledged ) // the other thread has acknowledged break; } }
void HEADBAND::start() { int led; int action = random(17); switch (action) { case 0: case 1: // strobe up strobe_up(); break; case 2: case 3: // strobe down strobe_down(); break; case 5: strobe_up(); strobe_down(); break; case 6: case 7: case 8: case 9: led = random(_nr_leds); _leds[led].choose(); break; case 10: glow_up(); break; case 11: glow_down(); break; case 12: glow_all(); break; case 13: blink_all(random(3)); break; case 14: shira_morse(); break; case 15: alternate(random(5) + 5); break; case 16: all_on(); break; } do_sleep(random(15) * 1000); }
void handle_signal(int signal) { const char *signal_name; sigset_t pending; // Find out which signal we're handling switch (signal) { case SIGHUP: signal_name = "SIGHUP"; break; case SIGUSR1: signal_name = "SIGUSR1"; break; case SIGINT: printf("Caught SIGINT, exiting now\n"); exit(0); default: fprintf(stderr, "Caught wrong signal: %d\n", signal); return; } /* * Please note that printf et al. are NOT safe to use in signal handlers. * Look for async safe functions. */ printf("Caught %s, sleeping for ~3 seconds\n" "Try sending another SIGHUP / SIGINT / SIGALRM " "(or more) meanwhile\n", signal_name); /* * Indeed, all signals are blocked during this handler * But, at least on OSX, if you send 2 other SIGHUP, * only one will be delivered: signals are not queued * However, if you send HUP, INT, HUP, * you'll see that both INT and HUP are queued * Even more, on my system, HUP has priority over INT */ do_sleep(5); printf("Done sleeping for %s\n", signal_name); // So what did you send me while I was asleep? sigpending(&pending); if (sigismember(&pending, SIGHUP)) { printf("A SIGHUP is waiting\n"); } if (sigismember(&pending, SIGUSR1)) { printf("A SIGUSR1 is waiting\n"); } printf("Done handling %s\n\n", signal_name); }
static switch_interval_time_t average_time(switch_interval_time_t t, int reps) { int x = 0; switch_time_t start, stop, sum = 0; for (x = 0; x < reps; x++) { start = switch_time_ref(); do_sleep(t); stop = switch_time_ref(); sum += (stop - start); } return sum / reps; }
int body() { char c; color = running->pid + 7; printf("\nproc %d resumes to body()\n", running->pid); while(1){ color = running->pid + 7; printQueue(freeList, "freeList"); printQueue(readyQueue, "readyQueue"); printf("proc %d running : \nenter a key (s|q|f | z|a|w): ", running->pid); c = getc(); printf("%c\n", c); switch(c) { case 's': tswitch(); break; case 'q': do_exit(); break; case 'f': do_kfork(); break; case 'z': //printf("sleep\n"); do_sleep(); //go to sleep on an even break; case 'a': //printf("wake up\n"); do_wakeup(); //wakeup all procs sleeping on event break; case 'w': //printf("wait\n"); do_wait(); //wait for a ZOMBIE child break; } //tswitch(); } }
void one_measurement(int seconds, char *workload) { create_all_usb_devices(); start_power_measurement(); devices_start_measurement(); start_process_measurement(); start_cpu_measurement(); if (workload && workload[0]) { if (system(workload)) fprintf(stderr, _("Unknown issue running workload!\n")); } else { do_sleep(seconds); } end_cpu_measurement(); end_process_measurement(); collect_open_devices(); devices_end_measurement(); end_power_measurement(); process_cpu_data(); process_process_data(); /* output stats */ process_update_display(); report_summary(); w_display_cpu_cstates(); w_display_cpu_pstates(); if (reporttype != REPORT_OFF) { report_display_cpu_cstates(); report_display_cpu_pstates(); } report_process_update_display(); tuning_update_display(); end_process_data(); global_joules_consumed(); compute_bundle(); show_report_devices(); report_show_open_devices(); report_devices(); ahci_create_device_stats_table(); store_results(measurement_time); end_cpu_data(); }
void master() { pid_t server_pid, client_pid; // handle error properly, otherwise /* start the server */ spawn(server, &server_pid); do_sleep(10); /* start the client */ spawn(client, &client_pid); /* wait all */ waitpid(client_pid); waitpid(server_pid); return; }
void one_measurement(int seconds, char *workload) { create_all_usb_devices(); start_power_measurement(); devices_start_measurement(); start_process_measurement(); start_cpu_measurement(); if (workload && workload[0]) { system(workload); } else { do_sleep(seconds); } end_cpu_measurement(); end_process_measurement(); collect_open_devices(); devices_end_measurement(); end_power_measurement(); process_cpu_data(); process_process_data(); /* output stats */ process_update_display(); report_summary(); w_display_cpu_cstates(); w_display_cpu_pstates(); report_display_cpu_cstates(); report_display_cpu_pstates(); report_process_update_display(); tuning_update_display(); end_process_data(); global_joules_consumed(); compute_bundle(); show_report_devices(); report_show_open_devices(); report_devices(); store_results(measurement_time); end_cpu_data(); }
void run_test(int64_t delay_ns) { for (int i = 0; i <= 25; i++) { int64_t start, finish; start = get_ns(); do_get(key, 0); finish = get_ns(); int64_t elapsed = finish - start; if (i > 0) { printf("%f\n", elapsed / 1e9); fflush(stdout); } do_sleep(delay_ns); } }
void do_client_run(ctx_t *ctx) { int full = ctx->size; int half = ctx->size >> 1; int cnt = 0; /* 1 */ do_recv(ctx, full, cnt++); /* 3 */ do_recv(ctx, full, cnt); /* 4 */ do_recv(ctx, half, cnt++); do_recv(ctx, half, cnt++); do_sleep(50); }
void do_server_run(ctx_t *ctx) { int full = ctx->size; int half = ctx->size >> 1; int cnt = 0; /* 1 */ do_send(ctx, full, cnt++); /* 3 */ do_send(ctx, half, cnt); do_send(ctx, half, cnt++); /* 4 */ do_send(ctx, full, cnt++); do_sleep(50); }
int main(void) { // Attiny13A fuse setting: -U lfuse:w:0x7A:m -U hfuse:w:0xFB:m // // set system-clock prescaler to 1/8 // 4.8MHz RC-oscillator --> 600kHz system-clock CLKPR = _BV(CLKPCE); CLKPR = _BV(CLKPS1) | _BV(CLKPS0); // configure TIMER0 TCCR0A = _BV(WGM01); // set CTC mode TCCR0B = ( _BV(CS01) ); // set prescaler to 8 // enable COMPA ISR TIMSK0 = _BV(OCIE0A); // set top value for TCNT0 OCR0A = 10; // just some start value // pull-up on for BUTTON_PIN PORT_OUT_REG |= _BV(BUTTON_PIN); // set port directions PORT_DIR_REG = PORT_DIR_MASK; // disable analog comparator to save power ACSR = _BV(ACD); // Pin change interrupt enabled // The actual pin will be activated later GIMSK |= _BV(PCIE); // globally enable interrupts // necessary to wake up from sleep via pin-change interrupt sei(); fade(0,255,1); while (1) { // the flag 'sleep_requested' is set in the ISR: TIM0_COMPA_vect if(sleep_requested == 1) { do_sleep(); } flicker(); } }
int body() { char c; while(1){ printf("\n\n\n"); ps(); printf("I am Proc %d in body()\n", running->pid); printf("Input a char : [s|q|f|z|a|w]: "); c=getc(); switch(c){ case 's': tswitch(); break; case 'q': do_exit(); break; case 'f': kfork(); break; case 'z': do_sleep(); break; case 'a': do_wakeup(); break; case 'w': do_wait(); break; default : break; } } }
SWITCH_DECLARE(void) switch_cond_yield(switch_interval_time_t t) { switch_time_t want; if (!t) return; if (globals.RUNNING != 1 || !runtime.timestamp || globals.use_cond_yield != 1) { do_sleep(t); return; } want = runtime.timestamp + t; while (globals.RUNNING == 1 && globals.use_cond_yield == 1 && runtime.timestamp < want) { switch_mutex_lock(TIMER_MATRIX[1].mutex); if (runtime.timestamp < want) { switch_thread_cond_wait(TIMER_MATRIX[1].cond, TIMER_MATRIX[1].mutex); } switch_mutex_unlock(TIMER_MATRIX[1].mutex); } }
/* Usage example * * First, compile and run this program: * $ gcc signal.c * $ ./a.out * * It will print out its pid. Use it from another terminal to send signals * $ kill -HUP <pid> * $ kill -USR1 <pid> * $ kill -ALRM <pid> * * Exit the process with ^C ( = SIGINT) or SIGKILL, SIGTERM */ int main() { struct sigaction sa; // Print pid, so that we can send signals from other shells printf("My pid is: %d\n", getpid()); // Setup the sighub handler sa.sa_handler = &handle_signal; // Restart the system call, if at all possible sa.sa_flags = SA_RESTART; // Block every signal during the handler sigfillset(&sa.sa_mask); // Intercept SIGHUP and SIGINT if (sigaction(SIGHUP, &sa, NULL) == -1) { perror("Error: cannot handle SIGHUP"); // Should not happen } if (sigaction(SIGUSR1, &sa, NULL) == -1) { perror("Error: cannot handle SIGUSR1"); // Should not happen } // Will always fail, SIGKILL is intended to force kill your process if (sigaction(SIGKILL, &sa, NULL) == -1) { perror("Cannot handle SIGKILL"); // Will always happen printf("You can never handle SIGKILL anyway...\n"); } if (sigaction(SIGINT, &sa, NULL) == -1) { perror("Error: cannot handle SIGINT"); // Should not happen } for (;;) { printf("\nSleeping for ~3 seconds\n"); do_sleep(3); // Later to be replaced with a SIGALRM } }
void * mt_thread(void *unused) { print_line("Aux thread tries to lock mutex"); ethr_mutex_lock(&mt_mutex); print_line("Aux thread locked mutex"); ASSERT(mt_data == 0); mt_data = 1; print_line("Aux thread wrote"); print_line("Aux thread goes to sleep for 1 second"); do_sleep(1); print_line("Aux thread woke up"); ASSERT(mt_data == 1); ethr_mutex_unlock(&mt_mutex); print_line("Aux thread unlocked mutex"); return NULL; }
void * st_thread(void *unused) { print_line("Aux thread tries to lock spinlock"); ethr_spin_lock(&st_spinlock); print_line("Aux thread locked spinlock"); ASSERT(st_data == 0); st_data = 1; print_line("Aux thread wrote"); print_line("Aux thread goes to sleep for 1 second"); do_sleep(1); print_line("Aux thread woke up"); ASSERT(st_data == 1); ethr_spin_unlock(&st_spinlock); print_line("Aux thread unlocked spinlock"); return NULL; }
static void detached_thread_test(void) { ethr_thr_opts thr_opts = ETHR_THR_OPTS_DEFAULT_INITER; ethr_tid tid[DT_BATCH_SIZE]; int i, j, res; res = ethr_mutex_init(&dt_mutex); ASSERT(res == 0); res = ethr_cond_init(&dt_cond); ASSERT(res == 0); thr_opts.detached = 1; dt_count = 0; dt_limit = 0; for (i = 0; i < DT_THREADS/DT_BATCH_SIZE; i++) { dt_limit += DT_BATCH_SIZE; for (j = 0; j < DT_BATCH_SIZE; j++) { res = ethr_thr_create(&tid[j], dt_thread, NULL, &thr_opts); ASSERT(res == 0); } ethr_mutex_lock(&dt_mutex); while (dt_count < dt_limit) { res = ethr_cond_wait(&dt_cond, &dt_mutex); ASSERT(res == 0 || res == EINTR); } ethr_mutex_unlock(&dt_mutex); print_line("dt_count = %d", dt_count); } do_sleep(1); }