pthread_arrt_XXXX(调度相关)

pthread_arrt_XXXX:
线程属性相关:
linux

头文件:
#include <rtl_sched.h> 安全


函数定义: async

设置/获得cpu相关属性: tcp

int pthread_attr_setcpu_np(pthread_attr_t *attr, int cpu);
int pthread_attr_getcpu_np(pthread_attr_t *attr, int *cpu); 函数

此函数为不可移植的 RTLinux 扩展函数。在存在多个CPU的机器上,能够指定线程在一特定CPU上运行。RTLinux的调度器并不猜想线程与CUP的对应关系。默认状况下,线程在哪一个CPU上建立即在哪里运行,但RTLinux仍是建议应用程序向调度器指定运行线程的CPU. spa

pthread_arrt_setcpu_np()函数修改了线程的属性对象,attribute,这样在pthread_create()建立线程时,线程就会被调度到由attr所定的CPU上去执行。在此以前线程属性对象attr必须首先由pthread_attr_init()来进行初始化。
pthread_attr_getcpu_np()函数中,指针cpu将指向attr结构中的CPU id域。 操作系统

设置/获得浮点运算支持属性: 线程

int pthread_attr_setfp_np(pthread_attr_t *attr, int use_fp);
int pthread_attr_getfp_np(pthread_attr_t *attr, int *use_fp); 指针

此函数为不可移植的 RTLinux 扩展函数。
参数:usr_fp为0表明不容许浮点操做,非零值表示容许浮点操做。
      int*use_fp参数指向attr结构中的use_fp标识位。 code

建立线程:

int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);

这函数是标准posix线程功能的RTLinux版本。
参数:attr指定建立线程的属性,若为NULL则使用默认的属性。start_routine指定线程执行函数。

标准的线程属性设置函数支持包括:
pthread_attr_setshedparam,
pthread_arrt_setdetachstate,
pthread_attr_setstacksize,
pthread_attr_setstackaddr.
另外还有两具线程属性设置函数:
pthread_attr_setcup_np,
pthread_attr_setfp_np.

pthread_attr_setstackaddr()容许用户为线程堆栈分配内存,所以线程可使用预先分配好的内存来在RT操做模式下被建立。这时,应该使用pthread_join来安全地同步释放包括内存在内的线程资源。

产生周期实时线程:

int pthread_make_periodic_np(pthread_t thread, hrtime_t start_time, hrtime_t period);
此函数为不可移植的RTlinux扩展。
此函数标识线程thread为准备好ready状态。线程将会在start_time时开始执行,并以period做为周期循环执行。
建议配合使用clock_nanosleep()来产生周期线程。


删除实时线程:

int pthread_delete_np(pthread_t thread);
此函数为不可移植的 RTLinux 扩展函数。

等待停止线程:
int pthread_join(pthread_t thread, void **arg);

这一功能是标准POSIX线程功能的RTLinux版。
pthread_join将挂起调用线程,直至目标线程(thread)停止。若是arg为非NULL值,则目标线程的返回值也将是(*arg).若是目标线程己经停止,pthread_join则会当即返回。若是该函数正确执行,那么能够肯定目标线程己经停止了。固然,这一调用将会用去多长时间,也是没有上限的。

挂起实时线程的执行:

int pthread_suspend_np(pthread_t thread);

此函数为不可移植的RTLinux扩展。此函数挂起线程,直至pthread_wakeup_np()被调用。
若是目标线程运行于另外一处理器,则不保证当即被挂起。
调用pthread_suspend_np(pthread_self())会当即执行。

注意:pthread_suspend_np可在信号或中断处理中调用,但应当心。RTLinux硬信号处理(interrupt handlers)是在被中断所打断的任意的线程中的上下文中运行的。若是当中断处理开始运行时,线程A正在运行,而且在中断处理中调用了pthread_suspend_np函数,则调度器会保存线程A,而后运行其它线程。
若是是在linux的上下文中运行了中断处理,并调用了pthread_suspend_np,则全部linux中的操做和进程都将被中止直到线程被唤醒。

唤醒实时线程:

int pthread_wakeup_np(pthread_t thread);

注意:这一函数能够在中断处理或线程中调用,但不能够用内核中调用。调用这一函数时,若是被唤醒的线程有更高的优先级,则将会引发一个线程的转换。被唤醒的线程将会当即挂起调用pthread_wakeup的线程。当没有更高优先级的线程执行时,才会继续这个处理。

例如,有一个很是简单的中断,但在中断后有很是复杂的处理。能够将这个处理线程指定最高优先级。当中断发生,并执行pthread_wakeup后,将会当即转换线程至处理线程。(应该在调用以前完成重要的硬件处理)

挂起当前线程直至下一周期:

int pthread_wait_np(void);
使用此函数前须调用pthread_make_periodic_np来使线程周期运行。

停止取消线程(不建议使用):

头文件:
#include <rtl_signal.h>
#include <rtl_sched.h>

int pthread_cancel(pthread_t thread);

这个函数是标准的POSIX线程功能,然而也是POSIX线程规范中最薄弱的部分。缘由是:
pthread_cancel()将请求停止目标线程,而这停止或者会被不一样步地执行,或者会因为目标线程的cancel state而被忽略。若是目标线程处于disabled状态,停止动做将会被挂起,直至线程达到enable状态。若是目标线程处于enabled状态,则其或者处于deferred或是asynchronous模式。deferred模式下将会推迟停止动做直至线程处于一个停止点(对应于线程的睡眠状态)。语法很复杂。

停止动做有一个灾难性的反作用。若是线程正在试图停止,它将使用pthread_cleanup_push()来处理全部进栈的内容。然而,持有互拆信号量的被停止的线程并不被强制清除互斥锁(mutex lock),所以会留下可怕的后果。所以建议不要使用此函数。

获得Linux thread的线程标识符:

pthread_t pthread_linux(void);


向线程发送信号:
#include <rtl_signal.h>
#include <rtl_sched.h>

int pthread_kill(pthread_t thread,signo);


这是标准POSIX线程功能。pthread_kill发送信号signo给线程thread. 若是called on threads与calling on threads 或interrupt handler在同一处理器上运行,则pthread_kill是一个快速的,肯定性的功能。

pthread_kill()不管从signal handler或是从thread 或是从Linux kernel code访问,都很安全。
pthread_kill()是向通用操做系统(linux)发送信号的主要方法之一。
调用:pthread_kill(pthread_linux(),RTL_LINUX_MIN_SIGNA+n),n是一个整数值 ,将会在Linux thread(我以为此处应该是linux系统,即优先级最低的linux系统)中产生n号中断的。

pthread_kill(pthread_linux(),RTL_SIG_SUSPEND_LINUX)将会给在the highest numbered 处理器上的linux thread发送一个中断,这中断将会强制linux来挂起本身并通知硬件来忽略全部的non-RTlinux的中断。
这一功能可使多处理器系统中的一个处理器专用于RTlinux. 使用RTL_SIG_RESTART_LINUX选项来恢复被挂起的linux threads.


在线程中容许使用浮点操做:

int pthread_setfp_np(pthread_t thread, int flag);

此函数为不可移植的RTLinux扩展。用来在实时线程中容许或禁止使用浮点算术运算。flag为0则不容许浮点操做,flag为非0值则容许。 此函数不适于指定其它cpu上的线程,建议用pthread_arrt_setfp_np. 默认下,RT线程不容许进行浮点操做。

相关文章
相关标签/搜索