/* ** Usage: thread_result ID ** ** Wait on the most recent operation to complete, then return the ** result code from that operation. */ static int tcl_thread_result( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ int i; const char *zName; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID", 0); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } thread_wait(&threadset[i]); switch( threadset[i].rc ){ case SQLITE_OK: zName = "SQLITE_OK"; break; case SQLITE_ERROR: zName = "SQLITE_ERROR"; break; case SQLITE_PERM: zName = "SQLITE_PERM"; break; case SQLITE_ABORT: zName = "SQLITE_ABORT"; break; case SQLITE_BUSY: zName = "SQLITE_BUSY"; break; case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break; case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break; case SQLITE_READONLY: zName = "SQLITE_READONLY"; break; case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break; case SQLITE_IOERR: zName = "SQLITE_IOERR"; break; case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break; case SQLITE_FULL: zName = "SQLITE_FULL"; break; case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break; case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break; case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break; case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break; case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break; case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break; case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break; case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break; case SQLITE_AUTH: zName = "SQLITE_AUTH"; break; case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break; case SQLITE_RANGE: zName = "SQLITE_RANGE"; break; case SQLITE_ROW: zName = "SQLITE_ROW"; break; case SQLITE_DONE: zName = "SQLITE_DONE"; break; default: zName = "SQLITE_Unknown"; break; } Tcl_AppendResult(interp, zName, 0); return TCL_OK; }
static int relayTee(Connection *Conn,int fcbsize,int fsbsize,int server){ int scerr,cserr; XFlash Xf; fprintf(stderr,"---relayTee\n"); Xf.xf_Conn = Conn; Xf.xf_xfsockfd = -1; Xf.xf_xfsockfp = 0; if( server ){ Xf.xf_cstid = thread_fork(0,0,"XFlash-CS",(IFUNCP)XCS,&Xf); Xf.xf_sctid = thread_fork(0,0,"XFlash-SC",(IFUNCP)XSC,&Xf,fsbsize); Xf.xf_fptid = thread_fork(0,0,"XFlash-Player",(IFUNCP)XFP,&Xf,server); cserr = thread_wait(Xf.xf_cstid,0); scerr = thread_wait(Xf.xf_sctid,1); sv1log("---- XFinished %X/%d %X/%d\n", PRTID(Xf.xf_cstid),cserr,PRTID(Xf.xf_sctid),scerr); }else{ XFP(&Xf,server); } return 0; }
int main() { /*用默认属性初始化互斥锁*/ pthread_mutex_init(&mut,NULL); printf("我是主函数哦,我正在创建线程,呵呵\n"); thread_create(); printf("我是主函数哦,我正在等待线程完成任务阿,呵呵\n"); thread_wait(); printf("两个线程已经结束\n"); sleep(5); printf("结束\n"); return 0; }
static void* event_loop(void* arg) { event_block_t* block; event_t* event = 0; FOUNDATION_UNUSED(arg); event_stream_set_beacon(system_event_stream(), &thread_self()->beacon); while (!_test_should_terminate) { block = event_stream_process(system_event_stream()); event = 0; while ((event = event_next(block, event))) { switch (event->id) { case FOUNDATIONEVENT_START: #if FOUNDATION_PLATFORM_IOS || FOUNDATION_PLATFORM_ANDROID || FOUNDATION_PLATFORM_PNACL log_debug(HASH_TEST, STRING_CONST("Application start event received")); _test_should_start = true; #endif break; case FOUNDATIONEVENT_TERMINATE: #if FOUNDATION_PLATFORM_IOS || FOUNDATION_PLATFORM_ANDROID || FOUNDATION_PLATFORM_PNACL log_debug(HASH_TEST, STRING_CONST("Application stop/terminate event received")); _test_should_terminate = true; break; #else log_warn(HASH_TEST, WARNING_SUSPICIOUS, STRING_CONST("Terminating tests due to event")); process_exit(-2); #endif case FOUNDATIONEVENT_FOCUS_GAIN: _test_have_focus = true; break; case FOUNDATIONEVENT_FOCUS_LOST: _test_have_focus = false; break; default: break; } test_event(event); } thread_wait(); } log_debug(HASH_TEST, STRING_CONST("Application event thread exiting")); return 0; }
int main(int argc, char *argv[]) { thread_t *nThread; unsigned n = 0; gmactime_t st, en; setParam<unsigned>(&nIter, nIterStr, nIterDefault); setParam<unsigned>(&vecSize, vecSizeStr, vecSizeDefault); barrier_init(&barr, nIter + 1); assert(eclCompileSource(kernel) == eclSuccess); nThread = (thread_t *)malloc(nIter * sizeof(thread_t)); unsigned *ids = (unsigned *)malloc(nIter * sizeof(unsigned)); getTime(&st); for(n = 0; n < nIter; n++) { ids[n] = n; nThread[n] = thread_create(check, &ids[n]); } // Alloc & init input data ecl_error ret = eclMalloc((void **)&a, vecSize * sizeof(float)); assert(ret == eclSuccess); for(n = 0; n < 32; n++) { // Call the kernel size_t globalSize = vecSize; ecl_kernel kernel; assert(eclGetKernel("accum", &kernel) == eclSuccess); assert(eclSetKernelArgPtr(kernel, 0, a) == eclSuccess); assert(eclCallNDRange(kernel, 1, NULL, &globalSize, NULL) == eclSuccess); barrier_wait(&barr); // Wait for the threads to do stuff barrier_wait(&barr); } for(n = 0; n < nIter; n++) { thread_wait(nThread[n]); } getTime(&en); printTime(&st, &en, "Total: ", "\n"); free(ids); free(nThread); return 0; }
void *sensor_thread(void *args) { while(true) { thread_wait(5000); /* * read_and_send_sensor_event executes a command * (default here is a filtered output of lm-sensors) * then sends its output as a port event message */ if (sensor_active) read_and_send_sensor_event(); } return NULL; }
static void call_functions(kz_syscall_type_t type, kz_syscall_param_t *p) { /* システム・コールの実行中にcurrentが書き換わるので注意 */ switch (type) { case KZ_SYSCALL_TYPE_RUN: /* kz_run() */ p->un.run.ret = thread_run(p->un.run.func, p->un.run.name, p->un.run.priority, p->un.run.stacksize, p->un.run.argc, p->un.run.argv); break; case KZ_SYSCALL_TYPE_EXIT: /* kz_exit() */ /* TCBが消去されるので,戻り値を書き込んではいけない */ thread_exit(); break; case KZ_SYSCALL_TYPE_WAIT: /* kz_wait() */ p->un.wait.ret = thread_wait(); break; case KZ_SYSCALL_TYPE_SLEEP: /* kz_sleep() */ p->un.sleep.ret = thread_sleep(); break; case KZ_SYSCALL_TYPE_WAKEUP: /* kz_wakeup() */ p->un.wakeup.ret = thread_wakeup(p->un.wakeup.id); break; case KZ_SYSCALL_TYPE_GETID: /* kz_getid() */ p->un.getid.ret = thread_getid(); break; case KZ_SYSCALL_TYPE_CHPRI: /* kz_chpri() */ p->un.chpri.ret = thread_chpri(p->un.chpri.priority); break; case KZ_SYSCALL_TYPE_KMALLOC: /* kz_kmalloc() */ p->un.kmalloc.ret = thread_kmalloc(p->un.kmalloc.size); break; case KZ_SYSCALL_TYPE_KMFREE: /* kz_kmfree() */ p->un.kmfree.ret = thread_kmfree(p->un.kmfree.p); break; case KZ_SYSCALL_TYPE_SEND: /* kz_send() */ p->un.send.ret = thread_send(p->un.send.id, p->un.send.size, p->un.send.p); break; case KZ_SYSCALL_TYPE_RECV: /* kz_recv() */ p->un.recv.ret = thread_recv(p->un.recv.id, p->un.recv.sizep, p->un.recv.pp); break; case KZ_SYSCALL_TYPE_SETINTR: /* kz_setintr() */ p->un.setintr.ret = thread_setintr(p->un.setintr.type, p->un.setintr.handler); break; default: break; } }
net_timer(uint32_t arg) { struct timer_thread *t = (struct timer_thread *) arg; for (;;) { uint32_t cur = sys_time_msec(); lwip_core_lock(); t->func(); lwip_core_unlock(); thread_wait(0, 0, cur + t->msec); } }
int main() { int arg = 100; printf(1, "Chillin' for %d tics.\n", arg); thread_create(&chill, (void *)&arg); int toc; int tic = tim(); while((toc = tim()) < tic + 50); printf(1, "Just kidding! Chillin' for %d tics.\n", (arg = 500)); thread_wait(); printf(1, "\nChillin' complete.\n"); exit(); }
/* Run given thread until it reaches address stopat. */ bool fncall_runtil(thread_t * thread, address_t stopat) { /* TODO: Be more verbose about errors. */ insn_t insn; insn_kind_t kind; assert(!thread->state.slavemode); info("Running thread %s until it reaches %p (runtil).", str_thread(thread), (void *) stopat); thread->state.slavemode = true; kind = (stopat & 0x1) ? INSN_KIND_THUMB : INSN_KIND_ARM; if (!patch_read_insn_detect_kind(thread, stopat, &insn, &kind)) goto fail; /* Patch instruction. */ if (!patch_insn(thread, stopat, kind, get_breakpoint_insn(kind))) goto fail; thread_continue(thread, 0); while (1) { thread_wait(thread, false); if (thread->state.dead) goto fail; if ((thread->state.signo == SIGSEGV) || (thread->state.signo == SIGTRAP) || (thread->state.signo == SIGBUS)) { struct pt_regs regs; thread_get_regs(thread, ®s); if (arch_get_pc(®s) == (stopat & 0xfffffffe)) { /* This is the signal we were waiting for. */ break; } } warning("Unexpected signal %s received during runtil in thread %s. The signal will be delivered.", str_signal(thread->state.signo), str_thread(thread)); dump_thread(thread); void callback(segment_t * segment) { verbose("%s\n", str_segment(segment)); } segment_iter(thread->process, callback); /* Deliver the signal. */ thread_continue(thread, 0); }
void fork_handler(int sig) { change_sig(SIGUSR1, 1); thread_wait(¤t->thread.mode.skas.switch_buf, current->thread.mode.skas.fork_buf); force_flush_all(); if(current->thread.prev_sched == NULL) panic("blech"); schedule_tail(current->thread.prev_sched); current->thread.prev_sched = NULL; userspace(¤t->thread.regs.regs); }
int main() { /*用默认属性初始化互斥锁*/ // int a=1;long int b=2;//linux 32下int与long int均为4字节 // printf("%d\n", sizeof(a)); // printf("%d\n", sizeof(b)); pthread_mutex_init(&mut,NULL); printf("我是主函数哦,我正在创建线程,呵呵\n"); thread_create(); printf("我是主函数哦,我正在等待线程完成任务阿,呵呵\n"); thread_wait(); return 0; }
void usb_close(void) { unsigned int thread = usb_thread_entry; usb_thread_entry = 0; if (thread == 0) return; #ifndef USB_STATUS_BY_EVENT tick_remove_task(usb_tick); #endif usb_monitor_enabled = false; queue_post(&usb_queue, USB_QUIT, 0); thread_wait(thread); }
/* * Yield the cpu to another process, and go to sleep, on the specified * wait channel WC, whose associated spinlock is LK. Calling wakeup on * the channel will make the thread runnable again. The spinlock must * be locked. The call to thread_switch unlocks it; we relock it * before returning. */ void wchan_sleep(struct wchan* wc, struct spinlock* lk) { asm volatile ("mfence" ::: "memory"); /* may not sleep in an interrupt handler */ assert(!thisthread->in_interrupt); /* must hold the spinlock */ assert(spinlock_held(lk)); /* must not hold other spinlocks */ assert(thiscpu->spinlocks == 1); thread_wait(wc, lk); spinlock_acquire(lk); }
static void call_functions(kz_syscall_type_t type, kz_syscall_param_t *p) { switch(type){ case KZ_SYSCALL_TYPE_RUN: p->un.run.ret = thread_run(p->un.run.func, p->un.run.name, p->un.run.priority, p->un.run.stacksize, p->un.run.argc, p->un.run.argv); break; case KZ_SYSCALL_TYPE_EXIT: thread_exit(); break; case KZ_SYSCALL_TYPE_WAIT: p->un.wait.ret = thread_wait(); break; case KZ_SYSCALL_TYPE_SLEEP: p->un.sleep.ret = thread_sleep(); break; case KZ_SYSCALL_TYPE_WAKEUP: p->un.wakeup.ret = thread_wakeup(p->un.wakeup.id); break; case KZ_SYSCALL_TYPE_GETID: p->un.getid.ret = thread_getid(); break; case KZ_SYSCALL_TYPE_CHPRI: p->un.chpri.ret = thread_chpri(p->un.chpri.priority); break; case KZ_SYSCALL_TYPE_KMALLOC: p->un.kmalloc.ret = thread_kmalloc(p->un.kmalloc.size); break; case KZ_SYSCALL_TYPE_KMFREE: p->un.kmfree.ret = thread_kmfree(p->un.kmfree.p); break; case KZ_SYSCALL_TYPE_SEND: p->un.send.ret = thread_send(p->un.send.id, p->un.send.size, p->un.send.p); break; case KZ_SYSCALL_TYPE_RECV: p->un.recv.ret = thread_recv(p->un.recv.id, p->un.recv.sizep, p->un.recv.pp); break; default: break; } }
int main (void) { mutex_init(&lock); int i, rc; int arg[10] = {100,200,300,400,500,600,700,800,900,1000}; for (i=0; i<1; i++) { rc = thread_create (&thread_func, (void*) (arg)); mutex_lock(&lock); printf (1, "created thread with pid %d\n", rc); mutex_unlock(&lock); } thread_wait(); printf(1,"parent exiting\n"); exit(); }
int waitthreads(){ int ix; int tid; int fin = 0; int mytid = getthreadid(); for( ix = 0; ix < elnumof(threads); ix++ ){ if( tid = threads[ix].t_id ){ if( tid != mytid ){ if( thread_wait(tid,1) == 0 ){ fin++; } } } } return fin; }
/* * Allocate a buffer in the kernel heap. * Suspends calling thread until enough memory * is available to satisfy the request. * The returned buffer is filled with zeroes. * * Parameters: * size - size in bytes of buffer to allocate * * Returns: * pointer to allocated, zero-filled buffer */ void *mem_alloc(size_t size) { extern void *malloc(size_t); void *buf; bool iflag; iflag = int_begin_atomic(); while ((buf = malloc(size)) == 0) { thread_wait(&s_heap_waitqueue); } int_end_atomic(iflag); /* fill buffer with zeroes */ memset(buf, '\0', size); return buf; }
void fork_handler(int sig) { change_sig(SIGUSR1, 1); thread_wait(¤t->thread.mode.skas.switch_buf, current->thread.mode.skas.fork_buf); force_flush_all(); if(current->thread.prev_sched == NULL) panic("blech"); schedule_tail(current->thread.prev_sched); current->thread.prev_sched = NULL; /* Handle any immediate reschedules or signals */ interrupt_end(); userspace(¤t->thread.regs.regs); }
struct Act * ipc_target_block(struct ipc_target *ipt) { struct Act *act; ipt_lock(ipt); while ((act = ipt->ipt_acts) == 0) { /* XXX mp unsafe */ ipt->ipt_waiting = 1; ipt_unlock(ipt); thread_wait((int)&ipt->ipt_acts, FALSE); ipt_lock(ipt); } ipt->ipt_acts = act->ipt_next; ipt_unlock(ipt); return act; }
void* thread_func (void *arg) { int j; int *ptr; ptr = (int *) arg; mutex_lock(&lock); printf(1, "I am thread with pid %d and now I'll create 10 more threads and pass the array\n", getpid()); mutex_unlock(&lock); for (j=0; j<10; j++) { int rc = thread_create (&thread2_func, (void *)(ptr+j)); mutex_lock(&lock); printf (1, "created thread with pid %d\n", rc); mutex_unlock(&lock); } thread_wait(); mutex_lock(&lock); printf(1, "exiting from thread_func\n"); mutex_unlock(&lock); exit(); }
/* * Allocate a physical memory frame. * Suspends calling thread until a frame is available. */ struct frame *mem_alloc_frame(frame_state_t initial_state, int initial_refcount) { struct frame *frame; bool iflag; iflag = int_begin_atomic(); while (frame_list_is_empty(&s_freelist)) { thread_wait(&s_frame_waitqueue); } frame = frame_list_remove_first(&s_freelist); frame->state = initial_state; frame->refcount = initial_refcount; int_end_atomic(iflag); return frame; }
void main() { loops = 5; numconsumers = 3; mutex_init(&mutex); cond_init(&fill); cond_init(&empty); //printf(1,"lock after init %d\n",(&mutex)->lock); int cons[numconsumers]; int * arg = malloc(sizeof(int)); *arg = 1; // create threads (producer and consumers) int prod = thread_create(producer, arg); int i; for (i = 0; i < numconsumers; i++) *arg = i; cons[i] = thread_create(consumer, arg); // wait for producer and consumers to finish thread_wait(); exit(); }
void net_client_destroy (NetClient *client) { if (!client) { error (InvalidArgument); return; } if (client->epoll) { if (!net_client_epoll_stop (client->epoll)) { error (FunctionCall); return; } } if (client->thread) { thread_wait (client->thread); thread_destroy (client->thread); } if (client->epoll) { net_client_epoll_deallocate (client->epoll); } memory_destroy (client); }
int main(int argc, char *argv[]) { struct tm *cur_time; time_t begin, end; /* begin: time interval from 1970-01-01::00:00:00 */ begin = time(&begin); cur_time = localtime(&begin); printf("begin time is %d:%d:%d\r\n", cur_time->tm_hour, cur_time->tm_min, cur_time->tm_sec); /* wait for a while */ thread_wait(5); /* end: time interval from 1970-01-01::00:00:00 */ end = time(&end); cur_time = localtime(&end); printf("end time is %d:%d:%d\r\n", cur_time->tm_hour, cur_time->tm_min, cur_time->tm_sec); return 0; }
/* ** Usage: thread_wait ID ** ** Wait on thread ID to reach its idle state. */ static int tcl_thread_wait( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ int i; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID", 0); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } thread_wait(&threadset[i]); return TCL_OK; }
int main() { THREAD *t = thread_new(); thread_setFunction(t, sub, NULL); thread_run(t); int i; for (i = 0; i < 10; ++i) { if (thread_isRunning(t)) { printf ("Thread 1 is still running\n"); sleep(1); } } thread_wait(t); printf("Thread 1 has the status %d\n", thread_getStatus(t)); printf("Thread 1 has the exit-code %d\n", thread_getExit(t)); thread_free(t); return EXIT_SUCCESS; }
void new_thread_handler(int sig) { int (*fn)(void *), n; void *arg; fn = current->thread.request.u.thread.proc; arg = current->thread.request.u.thread.arg; change_sig(SIGUSR1, 1); thread_wait(¤t->thread.mode.skas.switch_buf, current->thread.mode.skas.fork_buf); if(current->thread.prev_sched != NULL) schedule_tail(current->thread.prev_sched); current->thread.prev_sched = NULL; /* The return value is 1 if the kernel thread execs a process, * 0 if it just exits */ n = run_kernel_thread(fn, arg, ¤t->thread.exec_buf); if(n == 1) userspace(¤t->thread.regs.regs); else do_exit(0); }
/** * The entry point for the imap thread. It just go into the wait state and * then frees all resources when finished * * @param test */ static void imap_thread_entry(void *test) { if (thread_parent_task_can_contiue()) { thread_wait(NULL,NULL,NULL,0); imap_free(imap_server); free(imap_folder); free(imap_local_path); if (imap_connection) { tcp_disconnect(imap_connection); imap_connection = NULL; } if (imap_socket_lib_open) { close_socket_lib(); imap_socket_lib_open = 0; } } }
int CMain::Run() { if(m_Server.Init(this, m_Config.m_aBindAddr, m_Config.m_Port)) return 1; if(ReadConfig()) return 1; // Start JSON Update Thread m_JSONUpdateThreadData.m_ReloadRequired = 2; m_JSONUpdateThreadData.pClients = m_aClients; m_JSONUpdateThreadData.pConfig = &m_Config; void *LoadThread = thread_create(JSONUpdateThread, &m_JSONUpdateThreadData); //thread_detach(LoadThread); while(gs_Running) { if(gs_ReloadConfig) { if(ReadConfig()) return 1; m_Server.NetBan()->UnbanAll(); gs_ReloadConfig = 0; } m_Server.Update(); // wait for incomming data net_socket_read_wait(*m_Server.Network()->Socket(), 10); } dbg_msg("server", "Closing."); m_Server.Network()->Close(); thread_wait(LoadThread); return 0; }