Git Product home page Git Product logo

tinythreads's Introduction

TinyThreads

TinyThreads is POSIX threads (pthreads) compatible tiny multi-threading library for 32-bit microcontrollers.

Currently, following processors are supported:

  • Intel(Altera) Nios II
  • Xilinx Zynq-7000 (Cortex-A9)
  • Microchip PIC32 (MIPS32 Architecture)

マニュアル / Manuals

See TinyThreads wiki.

ライセンス / License

The MIT License

対応している機能 / Supported features

ほとんどの機能は <pthread.h> ヘッダファイルで提供されます。
Most features are provided in <pthread.h> header file.

スレッド操作とスケジューリング / Thread operation and scheduling

Interface Name Conforming to Remarks
Type pthread_t POSIX.1-2001
Function pthread_create() POSIX.1-2001
Function pthread_exit() POSIX.1-2001
Function pthread_join() POSIX.1-2001
Function pthread_detach() POSIX.1-2001
Function pthread_self() POSIX.1-2001
Function pthread_equal() POSIX.1-2001
Type pthread_attr_t POSIX.1-2001
Function pthread_attr_init() POSIX.1-2001
Function pthread_attr_destroy() POSIX.1-2001
Function pthread_attr_setdetachstate() POSIX.1-2001
Function pthread_attr_getdetachstate() POSIX.1-2001
Function pthread_attr_setschedparam() POSIX.1-2001
Function pthread_attr_getschedparam() POSIX.1-2001
Function pthread_attr_setschedpolicy() POSIX.1-2001
Function pthread_attr_getschedpolicy() POSIX.1-2001
Function pthread_attr_setstack() POSIX.1-2001
Function pthread_attr_getstack() POSIX.1-2001
Function pthread_attr_setstacksize() POSIX.1-2001
Function pthread_attr_getstacksize() POSIX.1-2001
Function sched_get_priority_max() POSIX.1-2001 <sched.h>
Function sched_get_priority_min() POSIX.1-2001 <sched.h>
Function shced_yield() POSIX.1-2001 <sched.h>

ミューテックス / Mutex

Interface Name Conforming to Remarks
Type pthread_mutex_t POSIX.1-2001
Macro PTHREAD_MUTEX_INITIALIZER POSIX.1-2001
Function pthread_mutex_destroy() POSIX.1-2001
Function pthread_mutex_init() POSIX.1-2001
Function pthread_mutex_lock() POSIX.1-2001
Function pthread_mutex_trylock() POSIX.1-2001
Function pthread_mutex_unlock() POSIX.1-2001

セマフォ / Semaphore

Interface Name Conforming to Remarks
Type sem_t POSIX.1-2001 <semaphore.h>
Function sem_destroy() POSIX.1-2001 <semaphore.h>
Function sem_init() POSIX.1-2001 <semaphore.h>
Function sem_post() POSIX.1-2001 <semaphore.h> Can be used in ISR
Function sem_wait() POSIX.1-2001 <semaphore.h>
Function sem_trywait() POSIX.1-2001 <semaphore.h>

条件付き変数 / Conditional variables

Interface Name Conforming to Remarks
Type pthread_cond_t POSIX.1-2001
Macro PTHREAD_COND_INITIALIZER POSIX.1-2001
Function pthread_cond_destroy() POSIX.1-2001
Function pthread_cond_init() POSIX.1-2001
Function pthread_cond_broadcast() POSIX.1-2001
Function pthread_cond_signal() POSIX.1-2001
Function pthread_cond_wait() POSIX.1-2001

1回きりの初期化 / Once control

Interface Name Conforming to Remarks
Type pthread_once_t POSIX.1-2001
Macro PTHREAD_ONCE_INIT POSIX.1-2001
Function pthread_once() POSIX.1-2001

pthreads以外の機能 / Features outside of pthreads

以下のAPIは、本来pthreadsのカバーする範囲ではありませんが、マルチスレッド環境に対して効率的な実装をTinyThreadsが提供できるものです。 The following APIs are outside of pthreads but TinyThreads can provide a more effective implementation for multi-threading environment.

Interface Name Conforming to Remarks
Function sleep() POSIX.1-2001 <unistd.h>
Function usleep() POSIX.1-2001 <unistd.h>

削除されている機能 / Dropped features

以下に挙げる機能はTinyThreadsが目指す「小さなマルチスレッド環境」に不要と判断され、 削除された機能です。原則として、将来も対応する予定はありません。
The features below are dropped because they are not useful in "tiny" multi-threading environment. As a general rule, they have never been supported in the future.

  • pthread_atfork()
  • pthread_cancel()
  • pthread_key*()
  • pthread_getspecific()
  • pthread_setspecific()
  • sem_open()
  • sem_unlink()

tinythreads's People

Contributors

kimushu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tinythreads's Issues

System stops when the initial (main) thread terminated

When the initial thread terminated by returning from main() function, the system oddly stops by exit() even if there are any alive threads other than the initial thread.

The PTHREAD expects that the system (=process) keeps alive when any threads are running.

#include <pthread.h>
static void *worker(void *)
{
    for (;;); // this thread never ends
    return NULL;
}
int main(void)
{
    pthread_t subthread;
    pthread_create(&sub_thread, NULL, worker, NULL);
    return 0;
    // subthread is still working, but exit() function will be called soon
    // and the system will stop.
}

stdin/stdout/stderr are left as NULL

  • TinyThreads 1.1
  • Target: Nios II

In a new thread created by pthread_create(), standard I/O file pointer (reent->stdin/stdout/stderr) are NULL.
Because Altera HAL does not check if fp != NULL in their i/o functions, this may cause memory corruption.

Unrecognised instruction error in alt_hooks.h

Wikiのマニュアルに沿って、Nios IIでTinyThreadsを使おうとしています。
BSP TypeをTinyThreadsにしたBSPプロジェクトを作成し、ビルドしてみたところ以下のようなエラーが出ました。

make all 
Info: Building ../candy_gw_tt_bsp
C:/intelFPGA_lite/18.1/nios2eds/bin/gnu/H-x86_64-mingw32/bin/make --no-print-directory -C ../candy_gw_tt_bsp
Compiling altera_vic_funnel_non_preemptive.S...
nios2-elf-gcc -MP -MMD -c   -O0 -g -Wall  -mno-hw-div -mhw-mul -mno-hw-mulx -mgpopt=global    -I./TINYTH/inc -I./HAL/inc -I. -I./drivers/inc -pipe -D__hal__ -DALT_NO_INSTRUCTION_EMULATION -D__tinythreads__  -Wa,-gdwarf2  -o obj/drivers/src/altera_vic_funnel_non_preemptive.o drivers/src/altera_vic_funnel_non_preemptive.S
./TINYTH/inc/os/alt_hooks.h: Assembler messages:
./TINYTH/inc/os/alt_hooks.h:4: Error: unrecognised instruction extern
./TINYTH/inc/os/alt_hooks.h:5: Error: unrecognised instruction extern
./TINYTH/inc/os/alt_hooks.h:6: Error: unrecognised instruction extern
./TINYTH/inc/os/alt_hooks.h:7: Error: unrecognised instruction extern
drivers/src/altera_vic_funnel_non_preemptive.S:77: Error: unrecognised instruction alt_os_int_enter_asm
drivers/src/altera_vic_funnel_non_preemptive.S:92: Error: unrecognised instruction alt_os_int_exit_asm
make[1]: *** [obj/drivers/src/altera_vic_funnel_non_preemptive.o] Error 1
make: *** [../candy_gw_tt_bsp-recurs-make-lib] Error 2

alt_hooks.hの4~7行目(externで始まる宣言部)をコメントアウトして、

#define ALT_OS_INT_ENTER_ASM
#define ALT_OS_INT_EXIT_ASM

を加えると、ビルドは通りましたが、プログラムを実行するとpthread_createで指定した関数が実行される前にデバッガーが強制的にNIOS2_BREAK()でbreakしてしまいます。

Using shadow register sets for Nios II

Nios II 実装における、Shadow register setのサポートについて検討。

目的:

  • Shadow register setの活用によるコンテキストスイッチの高速化が技術的に可能かどうか、実装コストが見合うかどうかを検討する。

調査結果:

  • Nios II/f では、最大63セットのShadow register setを追加できる。(外部割り込みコントローラの使用有無にかかわらず)
  • 現在使用中のregister setは、status.CRSで表されるが、CRSをソフトウェアから直接書き換えることはできず、CRS変更は以下のタイミングのみで発生する。
    • 外部割り込みコントローラからの割り込み発生で、RRSを複写するとき
    • eret命令でstatusが書き換わるとき (←ソフトウェアのみで書き換えるのはこれが唯一の方法)
  • 標準HALのalt_exception_entry.Sでは、Shadow register setに対する処置はなし。無条件にr15までのレジスタをスタックに待避する。
    • この動作をTinyThreadsで変更する場合、alt_exception_entry.Sの置換が必要。
  • r16以降のレジスタ待避は、eretによる強制CRS書き換えとtth_arch_nios2_asm.Sの実装のみで(おそらく)なんとかなる。
  • Register setの数よりスレッドの数が増えたときの、Register set番号の管理機構を追加で実装する必要あり。

結論:

  • TinyThreadsでは、Shadow register setを活用したコンテキストスイッチを実装しない
    • 技術的には不可能ではないが、実装コストが高い。特に全コンテキストのスイッチには、標準HALの大幅な置換が必要となる。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.