static void _tc_cleanup() { /* lock scheduler */ rt_enter_critical(); if (thread1.stat != RT_THREAD_CLOSE) rt_thread_detach(&thread1); if (thread2.stat != RT_THREAD_CLOSE) rt_thread_detach(&thread2); /* unlock scheduler */ rt_exit_critical(); if (t1_count / t2_count != 2) tc_stat(TC_STAT_END | TC_STAT_FAILED); }
void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c) { struct super_block *sb=OFNI_BS_2SFFJ(c); cyg_mtab_entry *mte; rt_uint32_t e; //RT_ASSERT(sb->s_gc_thread_handle); D1(printk("jffs2_stop_garbage_collect_thread\n")); /* Stop the thread and wait for it if necessary */ rt_event_send(&sb->s_gc_thread_flags,GC_THREAD_FLAG_STOP); D1(printk("jffs2_stop_garbage_collect_thread wait\n")); rt_event_recv(&sb->s_gc_thread_flags, GC_THREAD_FLAG_HAS_EXIT, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e); // Kill and free the resources ... this is safe due to the flag // from the thread. rt_thread_detach(&sb->s_gc_thread); rt_sem_detach(&sb->s_lock); rt_event_detach(&sb->s_gc_thread_flags); }
int pthread_cancel(pthread_t thread) { _pthread_data_t* ptd; /* cancel self */ if (thread == rt_thread_self()) return 0; /* get posix thread data */ ptd = _pthread_get_data(thread); RT_ASSERT(ptd != RT_NULL); /* set canceled */ if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE) { ptd->canceled = 1; if (ptd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS) { /* * to detach thread. * this thread will be removed from scheduler list * and because there is a cleanup function in the * thread (pthread_cleanup), it will move to defunct * thread list and wait for handling in idle thread. */ rt_thread_detach(thread); } } return 0; }
static void _tc_cleanup() { /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ rt_enter_critical(); /* 执行线程脱离 */ if (thread1.stat != RT_THREAD_CLOSE) rt_thread_detach(&thread1); if (thread2.stat != RT_THREAD_CLOSE) rt_thread_detach(&thread2); /* 调度器解锁 */ rt_exit_critical(); /* 设置TestCase状态 */ tc_done(TC_STAT_PASSED); }
void pthread_exit (void* value) { _pthread_data_t* ptd; _pthread_cleanup_t* cleanup; extern _pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX]; ptd = _pthread_get_data(rt_thread_self()); rt_enter_critical(); /* disable cancel */ ptd->cancelstate = PTHREAD_CANCEL_DISABLE; /* set return value */ ptd->return_value = value; rt_exit_critical(); /* invoke pushed cleanup */ while (ptd->cleanup != RT_NULL) { cleanup = ptd->cleanup; ptd->cleanup = cleanup->next; cleanup->cleanup_func(cleanup->parameter); /* release this cleanup function */ rt_free(cleanup); } /* destruct thread local key */ if (ptd->tls != RT_NULL) { void* data; rt_uint32_t index; for (index = 0; index < PTHREAD_KEY_MAX; index ++) { if (_thread_keys[index].is_used) { data = ptd->tls[index]; if (data) _thread_keys[index].destructor(data); } } /* release tls area */ rt_free(ptd->tls); ptd->tls = RT_NULL; } if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE) { /* release the joinable pthread */ rt_sem_release(ptd->joinable_sem); } /* detach thread */ rt_thread_detach(ptd->tid); /* reschedule thread */ rt_schedule(); }
static void _tc_cleanup() { /* lock scheduler */ rt_enter_critical(); if (thread1.stat != RT_THREAD_CLOSE) rt_thread_detach(&thread1); if (thread2.stat != RT_THREAD_CLOSE) rt_thread_detach(&thread2); /* unlock scheduler */ rt_exit_critical(); rt_kprintf("t1_count=%d t2_count=%d\n",t1_count,t2_count); if (t1_count / t2_count != 2) tc_stat(TC_STAT_END | TC_STAT_FAILED); else tc_done(TC_STAT_PASSED); }
void benchmark() { rt_err_t result; rt_thread_t tid; rt_uint8_t* ptr; result = rt_sem_init(&ack, "ack", 0, RT_IPC_FLAG_FIFO); if (result != RT_EOK) { rt_kprintf("init ack semaphore failed\n"); return; } rt_kprintf("internal SRAM benchmark\n"); result = rt_thread_init(&benchmark_thread, "t1", benchmark_thread_entry, benchmark_buffer, &benchmark_thread_stack[0], sizeof(benchmark_thread_stack), 20, 10); if (result == RT_EOK) rt_thread_startup(&benchmark_thread); /* wait thread complete */ rt_sem_take(&ack, RT_WAITING_FOREVER); rt_thread_detach(&benchmark_thread); rt_kprintf("external SRAM benchmark\n"); ptr = rt_malloc(BENCHMARK_SIZE); /* create benchmark thread */ tid = rt_thread_create("t2", benchmark_thread_entry, ptr, 512, 20, 10); if (tid != RT_NULL) rt_thread_startup(tid); /* wait thread complete */ rt_sem_take(&ack, RT_WAITING_FOREVER); /* release memory */ rt_free(ptr); /* detach semaphore */ rt_sem_detach(&ack); }
int pthread_detach(pthread_t thread) { _pthread_data_t* ptd; ptd = _pthread_get_data(thread); if (thread->stat == RT_THREAD_CLOSE) { /* delete joinable semaphore */ if (ptd->joinable_sem != RT_NULL) rt_sem_delete(ptd->joinable_sem); /* detach thread object */ rt_thread_detach(ptd->tid); /* release thread resource */ if (ptd->attr.stack_base == RT_NULL) { /* release thread allocated stack */ rt_free(ptd->tid->stack_addr); } /* * if this thread create the local thread data, * delete it */ if (ptd->tls != RT_NULL) rt_free(ptd->tls); rt_free(ptd->tid); rt_free(ptd); } else { rt_enter_critical(); /* change to detach state */ ptd->attr.detachstate = PTHREAD_CREATE_DETACHED; /* detach joinable semaphore */ rt_sem_delete(ptd->joinable_sem); ptd->joinable_sem = RT_NULL; rt_exit_critical(); } return 0; }
void tc_stop() { _tc_stat &= ~TC_STAT_RUNNING; rt_thread_delay(RT_TICK_PER_SECOND/2); if (_tc_thread.stat != RT_THREAD_INIT) { /* lock scheduler */ rt_enter_critical(); /* detach old tc thread */ rt_thread_detach(&_tc_thread); rt_sem_detach(&_tc_sem); /* unlock scheduler */ rt_exit_critical(); } rt_thread_delay(RT_TICK_PER_SECOND/2); }
static void _tc_cleanup(void) { /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ rt_enter_critical(); /* 执行线程脱离 */ if (thread.stat != RT_THREAD_CLOSE) { rt_thread_detach(&thread); /* 执行信号量对象脱离 */ rt_sem_detach(&sem); } /* 调度器解锁 */ rt_exit_critical(); /* 设置TestCase状态 */ tc_done(TC_STAT_PASSED); }
/* 线程2入口 */ static void thread2_entry(void* parameter) { /* 线程2拥有较高的优先级,以抢占线程1而获得执行 */ /* 线程2启动后先睡眠10个OS Tick */ rt_thread_delay(10); /* * 线程2唤醒后直接执行线程1脱离,线程1将从就绪线程队列中删除 */ rt_thread_detach(&thread1); /* * 线程2继续休眠10个OS Tick然后退出 */ rt_thread_delay(10); /* * 线程2运行结束后也将自动被从就绪队列中删除,并脱离线程队列 */ }
int pthread_create (pthread_t *tid, const pthread_attr_t *attr, void *(*start) (void *), void *parameter) { int result; void* stack; char name[RT_NAME_MAX]; static rt_uint16_t pthread_number = 0; _pthread_data_t *ptd; /* tid shall be provided */ RT_ASSERT(tid != RT_NULL); /* allocate posix thread data */ ptd = (_pthread_data_t*)rt_malloc(sizeof(_pthread_data_t)); if (ptd == RT_NULL) return ENOMEM; /* clean posix thread data memory */ rt_memset(ptd, 0, sizeof(_pthread_data_t)); ptd->canceled = 0; ptd->cancelstate = PTHREAD_CANCEL_DISABLE; ptd->canceltype = PTHREAD_CANCEL_DEFERRED; ptd->magic = PTHREAD_MAGIC; if (attr != RT_NULL) ptd->attr = *attr; else { /* use default attribute */ pthread_attr_init(&ptd->attr); } rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++); if (ptd->attr.stack_base == 0) { stack = (void*)rt_malloc(ptd->attr.stack_size); } else stack = (void*)(ptd->attr.stack_base); if (stack == RT_NULL) { rt_free(ptd); return ENOMEM; } /* pthread is a static thread object */ ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread)); if (ptd->tid == RT_NULL) { if (ptd->attr.stack_base ==0) rt_free(stack); rt_free(ptd); return ENOMEM; } if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE) { ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO); if (ptd->joinable_sem == RT_NULL) { if (ptd->attr.stack_base !=0) rt_free(stack); rt_free(ptd); return ENOMEM; } } else ptd->joinable_sem = RT_NULL; /* set parameter */ ptd->thread_entry = start; ptd->thread_parameter = parameter; /* initial this pthread to system */ if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd, stack, ptd->attr.stack_size, ptd->attr.priority, 5) != RT_EOK) { if (ptd->attr.stack_base ==0) rt_free(stack); if (ptd->joinable_sem != RT_NULL) rt_sem_delete(ptd->joinable_sem); rt_free(ptd); return EINVAL; } /* set pthread id */ *tid = ptd->tid; /* set pthread cleanup function and ptd data */ (*tid)->cleanup = _pthread_cleanup; (*tid)->user_data = (rt_uint32_t)ptd; /* start thread */ result = rt_thread_startup(*tid); if (result == RT_EOK) return 0; /* start thread failed */ rt_thread_detach(ptd->tid); if (ptd->attr.stack_base ==0) rt_free(stack); if (ptd->joinable_sem != RT_NULL) rt_sem_delete(ptd->joinable_sem); rt_free(ptd); return EINVAL; }