odarcy
odarcy

Reputation: 105

Does RISCV tool chain support pthread library?

I have cloned and built the riscv-tools repository on my ubuntu machine. Hello world program works well.

Now, I am trying to port an application from the X86 target to a riscv (RV32IM) target. My application depends on math and pthread libraries. I am experiencing problems when trying to use declarations from pthread.h header file in my code.

I made a very simple sample code to demonstrate my problem.

Here is my example.c file content

#include <stdio.h>
#include <math.h>
#include <pthread.h>

int main(void) 
{ 
  float my_float;
  int rc;

  pthread_t my_thread;
  pthread_mutex_t my_lock;

  printf("Example start!\n"); 

  my_float = sqrt( 16.0 );
  printf("sqrt(16.0) = %f\n", my_float);

  rc = pthread_mutex_init(&my_lock, NULL);
  printf("return code from pthread_mutex_init() is %d\n", rc);

  printf("Example End!\n"); 

  return 0; 
}

Ok and here is my command line to compile it for the RISCV target

riscv64-unknown-elf-gcc -Wall -m32 -march=RV32IM -o example example.c -lm -lpthread

Here is the compiler output:

example.c: In function 'main':
example.c:10:3: error: unknown type name 'pthread_t'
   pthread_t my_thread;
   ^
example.c:11:3: error: unknown type name 'pthread_mutex_t'
   pthread_mutex_t my_lock;
   ^
example.c:19:3: warning: implicit declaration of function 'pthread_mutex_init' [-Wimplicit-function-declaration]
   rc = pthread_mutex_init(&my_lock, NULL);
   ^
example.c:10:13: warning: unused variable 'my_thread' [-Wunused-variable]
   pthread_t my_thread;
             ^

Notice there is no problem with the math library, but the pthread library stuff yields errors.

Obviously compiling this simple example for X86 target works like a charm. The program output for X86 target is :

> ./example
 Example start!
 sqrt(16.0) = 4.000000
 return code from pthread_mutex_init() is 0
 Example End!

This is what we should eventually get when it compiles and runs on the RISCV target when doing this:

spike pk ./example

What is the problem with the pthread library in the RISCV tool chain ? Can anyone from RISCV community reproduce it ? Anyone experiencing the same issue ?

Any help is appreciated!

Upvotes: 3

Views: 1971

Answers (2)

odarcy
odarcy

Reputation: 105

Thanks for the answer.

FYI here is an email answer I got from Andrew Waterman (RISCV project insider!):

"Short answer is that there's no pthread support for the embedded Newlib/ELF toolchain, and there are no plans to add it. The GNU/Linux toolchain has working pthreads, but of course the binaries it produces require Linux to run."

In the short term I will not pursue to compile the Linux toolchain and Linux image because I found a way to work around my application code and get rid of the pthread library dependency.

However, it is highly probable that in the near future I will give a try at building the linux toolchain and following the steps above to evaluate a multithread application. I will definitely let you know how it goes.

Upvotes: 2

user2548418
user2548418

Reputation: 1571

For multithreaded applications, you will need to move up to linux compiler and kernel. The proxy kernel (pk) does not support multithreading in the user programs it runs. Furthermore, we haven't set up the newlib based compiler to support pthreads.

Steps

  1. Build the riscv-linux compiler
  2. Build riscv-linux
  3. Recompile your application with the new compiler (perhaps using -static)
  4. Build a disk image for linux
  5. Include your application in the disk image
  6. Let us know how it goes

Upvotes: 3

Related Questions