int pthread_dmutex_lock(pthread_dmutex_t *m) {pthread_t tid = pthread_self(); sleep_queue_t *dq = (sleep_queue_t *) pthread_getspecific(sleep_queue_key); if (dq == NULL) {dq = (sleep_queue_t *) malloc(sizeof(sleep_queue_t)); pthread_setspecific(sleep_queue_key, dq); dq->tid = tid; } pthread_mutex_lock(&(m->lock)); if (pthread_equal(m->owner, tid)) {if (m->name) printf("Error! %s deadlocking dmutex %s.\n", thread_name(tid), m->name); else printf("Error! %s deadlocking dmutex x%x.\n", thread_name(tid), m); abort(); /* I don't want it to exit. I want it to crash. */ } while (m->owned) {dq->next = m->sleepers; m->sleepers = dq; m->n_failed++; pthread_cond_wait(&(m->cv), &(m->lock)); remove_sleeper(&(m->sleepers), tid); } m->owner = tid; m->owned = TRUE; m->n_locked++; pthread_mutex_unlock(&(m->lock)); return(0); }
void syscall_exit(int value) { struct exit_struct *ec = thread_current()->exit_controler; printf("%s: exit(%d)\n", thread_name(), value); ec->value = value; sema_up(&ec->parent); thread_exit(); }
extern "C" JNIEXPORT void JNICALL Java_java_lang_Thread_setName(JNIEnv *env, jobject obj, jstring name) { thread *thrd = static_cast<thread *>(obj) ; string thread_name(env->GetStringUTFChars(name, 0)) ; thrd->name = thread_name ; }
void thread_cleanup_and_exit (int status) { printf ("%s: exit(%d)\n", thread_name (), status); /* close all open file descriptors */ struct thread *t = thread_current (); struct list_elem *e; /* close all the files opened and free spaces allocated for the file list */ while (!list_empty (&t->file_list)) { e = list_pop_back (&t->file_list); struct file_elem *f_elem = list_entry (e, struct file_elem, elem); file_close (f_elem->file); free (f_elem); } /* free waited_children_list and children_list */ while (!list_empty (&t->children_list)) { e = list_pop_back (&t->children_list); struct child_elem *c_elem = list_entry (e, struct child_elem, elem); // free children from the global exit_list free_thread_from_exit_list (c_elem->pid); free (c_elem); } while (!list_empty (&t->waited_children_list)) { e = list_pop_back (&t->waited_children_list); struct wait_child_elem *w_elem = list_entry (e, struct wait_child_elem, elem); free (w_elem); } add_thread_to_exited_list (t->tid, status); /* allow file write to executable */ if (t->exec_file) { file_allow_write (t->exec_file); file_close (t->exec_file); } /* release all the locks that have not already been released */ while (!list_empty (&t->acquired_locks)) { struct list_elem *e = list_front (&t->acquired_locks); struct lock *l = list_entry (e, struct lock, elem); lock_release (l); } /* wake parent up if its waiting on it */ struct thread *parent = get_thread (thread_current ()->parent_id); if (parent) { sema_up (&parent->waiting_on_child_exit_sema); } thread_exit (); }
/* Sleeps for approximately TICKS timer ticks. Interrupts must be turned on. */ void timer_sleep (int64_t ticks) { int64_t start = timer_ticks (); printf("Thread Name: %s\n", thread_name ()); ASSERT (intr_get_level () == INTR_ON); while (timer_elapsed (start) < ticks) thread_yield (); }
void print_dmutex(pthread_dmutex_t *m) {sleep_queue_t *sq; pthread_mutex_lock(&(m->lock)); flockfile (stdout); if (!m->owned) printf("%s ---- Times locked: %3d, failed: %3d. Sleepers: ( ", m->name, m->n_locked, m->n_failed); else printf("%s %s Times locked: %3d, failed: %3d. Sleepers: ( ", m->name, thread_name(m->owner), m->n_locked, m->n_failed); for (sq = m->sleepers; sq != NULL; sq = sq->next) printf("%s ", thread_name(sq->tid)); printf(")\n"); funlockfile (stdout); pthread_mutex_unlock(&(m->lock)); }
/* Exits the current process with status "status", and prints out the current process's name and exit status */ static void sys_exit (int status) { char* save_ptr; char* name = strtok_r(thread_name(), " ", &save_ptr); printf("%s: exit(%d)\n", name, status); thread_exit_status (status); NOT_REACHED(); }
int pthread_dmutex_unlock(pthread_dmutex_t *m) {pthread_t tid = pthread_self(); pthread_mutex_lock(&(m->lock)); if (!pthread_equal(m->owner, tid)) {if (m->name) printf("Error! %s unlocking dmutex %s owned by %s\n", thread_name(tid), m->name, thread_name(m->owner)); else printf("Error! %s unlocking dmutex x%x owned by %s\n", thread_name(tid), m, thread_name(m->owner)); abort(); /* I don't want it to exit. I want it to crash. */ } m->owned = FALSE; m->owner = NULL_TID; pthread_mutex_unlock(&(m->lock)); pthread_cond_signal(&(m->cv)); return(0); }
void test_priority_donate_chain (void) { int i; struct lock locks[NESTING_DEPTH - 1]; struct lock_pair lock_pairs[NESTING_DEPTH]; /* This test does not work with the MLFQS. */ ASSERT (!thread_mlfqs); thread_set_priority (PRI_MIN); for (i = 0; i < NESTING_DEPTH - 1; i++) lock_init (&locks[i]); lock_acquire (&locks[0]); msg ("%s got lock.", thread_name ()); for (i = 1; i < NESTING_DEPTH; i++) { char name[16]; int thread_priority; snprintf (name, sizeof name, "thread %d", i); thread_priority = PRI_MIN + i * 3; lock_pairs[i].first = i < NESTING_DEPTH - 1 ? locks + i: NULL; lock_pairs[i].second = locks + i - 1; thread_create (name, thread_priority, donor_thread_func, lock_pairs + i); msg ("%s should have priority %d. Actual priority: %d.", thread_name (), thread_priority, thread_get_priority ()); snprintf (name, sizeof name, "interloper %d", i); thread_create (name, thread_priority - 1, interloper_thread_func, NULL); } lock_release (&locks[0]); msg ("%s finishing with priority %d.", thread_name (), thread_get_priority ()); }
static void donor_thread_func (void *locks_) { struct lock_pair *locks = locks_; if (locks->first) lock_acquire (locks->first); lock_acquire (locks->second); msg ("%s got lock", thread_name ()); lock_release (locks->second); msg ("%s should have priority %d. Actual priority: %d", thread_name (), (NESTING_DEPTH - 1) * 3, thread_get_priority ()); if (locks->first) lock_release (locks->first); msg ("%s finishing with priority %d.", thread_name (), thread_get_priority ()); }
void do_backtrace( int signum, void *ip ) { #ifndef __CYGWIN__ void *array[100]; size_t size; char **strings; size_t i; char *name; static char *unknown = "unknown"; if( ip ) { /* This was a signal, so print the thread name */ name = thread_name( pthread_self() ); if( !name ) { name = unknown; } LogPrint( LOG_DEBUG, "Thread: %s backtrace", name ); } else { name = NULL; } size = backtrace( array, 100 ); #if 0 /* replace the sigaction/pthread_kill with the caller's address */ if( ip ) { array[1] = ip; } #endif strings = backtrace_symbols( array, size ); LogPrint( LOG_DEBUG, "%s%sObtained %zd stack frames.", (name ? name : ""), (name ? ": " : ""), size ); for( i = 0; i < size; i++ ) { LogPrint( LOG_DEBUG, "%s%s%s", (name ? name : ""), (name? ": " : ""), strings[i] ); } free( strings ); #endif }
void CRenderDevice::Run () { // DUMP_PHASE; g_bLoaded = FALSE; Log ("Starting engine..."); thread_name ("X-RAY Primary thread"); // Startup timers and calculate timer delta dwTimeGlobal = 0; Timer_MM_Delta = 0; { u32 time_mm = timeGetTime (); while (timeGetTime()==time_mm); // wait for next tick u32 time_system = timeGetTime (); u32 time_local = TimerAsync (); Timer_MM_Delta = time_system-time_local; } // Start all threads // InitializeCriticalSection (&mt_csEnter); // InitializeCriticalSection (&mt_csLeave); mt_csEnter.Enter (); mt_bMustExit = FALSE; thread_spawn (mt_Thread,"X-RAY Secondary thread",0,0); // Message cycle seqAppStart.Process (rp_AppStart); //CHK_DX(HW.pDevice->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1,0)); m_pRender->ClearTarget (); message_loop (); seqAppEnd.Process (rp_AppEnd); // Stop Balance-Thread mt_bMustExit = TRUE; mt_csEnter.Leave (); while (mt_bMustExit) Sleep(0); // DeleteCriticalSection (&mt_csEnter); // DeleteCriticalSection (&mt_csLeave); }
static thread_t thread_run(char *name, void (*start)(void), void *stack, size_t stack_size, int nice) { thread_t th = 0; int rc; if ((rc = thread_create(task_self(), &th)) != 0) errx(1, "thread_create: %s", strerror(rc)); if ((rc = thread_load(th, start, (uint8_t*)stack + stack_size)) != 0) errx(1, "thread_load: %s", strerror(rc)); thread_name(th, name); if ((rc = thread_setprio(th, PRIO_DFLT + nice)) != 0) errx(1, "thread_setprio: %s", strerror(rc)); if ((rc = thread_resume(th)) != 0) errx(1, "thread_resume: %s", strerror(rc)); return th; }
HPX_EXPORT void throw_exception(Exception const& e, std::string const& func, std::string const& file, long line) { boost::int64_t pid_ = ::getpid(); std::string back_trace(backtrace()); std::string hostname_ = ""; if (get_runtime_ptr()) { util::osstream strm; strm << get_runtime().here(); hostname_ = util::osstream_get_string(strm); } // if this is not a HPX thread we do not need to query neither for // the shepherd thread nor for the thread id boost::uint32_t node = 0; std::size_t shepherd = std::size_t(-1); std::size_t thread_id = 0; std::string thread_name(""); threads::thread_self* self = threads::get_self_ptr(); if (NULL != self) { if (threads::threadmanager_is(running)) { node = get_locality_id(); shepherd = threads::threadmanager_base::get_worker_thread_num(); } thread_id = reinterpret_cast<std::size_t>(self->get_thread_id()); thread_name = threads::get_thread_description(self->get_thread_id()); } rethrow_exception(e, func, file, line, back_trace, node, hostname_, pid_, shepherd, thread_id, thread_name); }
/* This test does not work with the MLFQS. */ ASSERT (!thread_mlfqs); sema_init (&sema, 0); thread_set_priority (PRI_MIN); for (i = 0; i < 10; i++) { int priority = PRI_DEFAULT - (i + 3) % 10 - 1; char name[16]; snprintf (name, sizeof name, "priority %d", priority); thread_create (name, priority, priority_sema_thread, NULL); } for (i = 0; i < 10; i++) { sema_up (&sema); msg ("Back in main thread."); } } static void priority_sema_thread (void *aux UNUSED) { sema_down (&sema); msg ("Thread %s woke up.", thread_name ()); }
void remove (struct intr_frame *f); void syscall_init (void) { intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall"); } static void syscall_handler (struct intr_frame *f UNUSED) { if(f->esp==NULL || lookup_page(thread_current()->pagedir,f->esp,false)== NULL){ f->eax=-1; printf("%s: exit(%d)\n",thread_name(),f->eax); thread_exit(); return; } if(!is_user_vaddr(((f->esp)+4))){ f->eax=-1; printf("%s: exit(%d)\n",thread_name(),f->eax); thread_exit(); return; } switch(*(int *)(f->esp)){ case SYS_HALT: // Halt the operating system. power_off(); //threads/init.h break;
void LogItemOutput( void *vitem ) { LoggingItem_t *item; struct tm ts; char line[MAX_STRING_LENGTH]; char usPart[9]; char timestamp[TIMESTAMP_MAX]; int length; LinkedListItem_t *listItem, *next; LogFileChain_t *logFile; static char *unknown = "thread_unknown"; char *threadName; if( !vitem ) { return; } item = (LoggingItem_t *)vitem; localtime_r( (const time_t *)&(item->tv.tv_sec), &ts ); strftime( timestamp, TIMESTAMP_MAX-8, "%Y-%b-%d %H:%M:%S", (const struct tm *)&ts ); snprintf( usPart, 9, ".%06d ", (int)(item->tv.tv_usec) ); strcat( timestamp, usPart ); length = strlen( timestamp ); LinkedListLock( LogList ); for( listItem = LogList->head; listItem; listItem = next ) { logFile = (LogFileChain_t *)listItem; next = listItem->next; switch( logFile->type ) { case LT_SYSLOG: syslog( item->level, "%s", item->message ); break; case LT_CONSOLE: sprintf( line, "%s %s\n", timestamp, item->message ); LogWrite( logFile, line, strlen(line) ); break; case LT_FILE: threadName = thread_name( item->threadId ); if( !threadName ) { threadName = unknown; } sprintf( line, "%s %s %s:%d (%s) - %s\n", timestamp, threadName, item->file, item->line, item->function, item->message ); LogWrite( logFile, line, strlen(line) ); break; case LT_NCURSES: sprintf( line, "%s %s\n", timestamp, item->message ); cursesLogWrite( line ); break; default: break; } if( logFile->aborted ) { LogOutputRemove( logFile ); } } LinkedListUnlock( LogList ); free( item->message ); free( item ); }
static void donor_thread_func (void *locks_) { struct lock_pair *locks = locks_; if (locks->first) lock_acquire (locks->first); lock_acquire (locks->second); msg ("%s got lock", thread_name ()); lock_release (locks->second); msg ("%s should have priority %d. Actual priority: %d", thread_name (), (NESTING_DEPTH - 1) * 3, thread_get_priority ()); if (locks->first) lock_release (locks->first); msg ("%s finishing with priority %d.", thread_name (), thread_get_priority ()); } static void interloper_thread_func (void *arg_ UNUSED) { msg ("%s finished.", thread_name ()); } // vim: sw=2
/* Syscall handler calls the appropriate function. */ static void syscall_handler (struct intr_frame *f) { int ret = 0; int *syscall_nr = (int *) f->esp; void *arg1 = (int *) f->esp + 1; void *arg2 = (int *) f->esp + 2; void *arg3 = (int *) f->esp + 3; /* Check validate pointer. */ if (!is_user_vaddr (syscall_nr) || !is_user_vaddr (arg1) || !is_user_vaddr (arg2) || !is_user_vaddr (arg3)) sys_exit (-1); switch (*syscall_nr) { case SYS_HALT: sys_halt (); break; case SYS_EXIT: sys_exit (*(int *) arg1); break; case SYS_EXEC: ret = sys_exec (*(char **) arg1); break; case SYS_WAIT: ret = sys_wait (*(pid_t *) arg1); break; case SYS_CREATE: ret = sys_create (*(char **) arg1, *(unsigned *) arg2); break; case SYS_REMOVE: ret = sys_remove (*(char **) arg1); break; case SYS_OPEN: ret = sys_open (*(char **) arg1); break; case SYS_FILESIZE: ret = sys_filesize (*(int *) arg1); break; case SYS_READ: ret = sys_read (*(int *) arg1, *(void **) arg2, *(unsigned *) arg3); break; case SYS_WRITE: ret = sys_write (*(int *) arg1, *(void **) arg2, *(unsigned *) arg3); break; case SYS_SEEK: sys_seek (*(int *) arg1, *(unsigned *) arg2); break; case SYS_TELL: ret = sys_tell (*(int *) arg1); break; case SYS_CLOSE: sys_close (*(int *) arg1); break; default: printf (" (%s) system call! (%d)\n", thread_name (), *syscall_nr); sys_exit (-1); break; } f->eax = ret; }
lock_init (&lock); cond_init (&condition); thread_set_priority (PRI_MIN); for (i = 0; i < 10; i++) { int priority = PRI_DEFAULT - (i + 7) % 10 - 1; char name[16]; snprintf (name, sizeof name, "priority %d", priority); thread_create (name, priority, priority_condvar_thread, NULL); } for (i = 0; i < 10; i++) { lock_acquire (&lock); msg ("Signaling..."); cond_signal (&condition, &lock); lock_release (&lock); } } static void priority_condvar_thread (void *aux UNUSED) { msg ("Thread %s starting.", thread_name ()); lock_acquire (&lock); cond_wait (&condition, &lock); msg ("Thread %s woke up.", thread_name ()); lock_release (&lock); }