pthreads

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
hectic128
Posts: 12
Joined: Sat Nov 17, 2007 4:41 am

pthreads

Post by hectic128 »

I've finished up an implementation of the pthreads API for the PSP. The implementation is pretty complete, the only thing missing are things that don't really apply to the PSP platform. See the link below...

http://sourceforge.net/projects/pthreads-emb/
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

Cool! Nice work.
User avatar
jean
Posts: 489
Joined: Sat Jan 05, 2008 2:44 am

Post by jean »

Not looked into it yet, but it sounds good! Nice job man!
miniJB
Posts: 5
Joined: Sun Apr 06, 2008 8:54 pm

Post by miniJB »

Nice work. Tests run great on psp FAT (after some very slight changes to make printf work and compile to pbp), though a few take a while ;). Very nice test set too!

I like the abstraction layer you use to support multiple platforms... and that its inherited pthreads-win32's exception handling. I was in the middle of a pthreads port based on Pth (but using the psp threading libs rather than setjmp/etc), so you sure saved me some work!
pegasus2000
Posts: 160
Joined: Wed Jul 12, 2006 7:09 am

Re: pthreads

Post by pegasus2000 »

hectic128 wrote:I've finished up an implementation of the pthreads API for the PSP. The implementation is pretty complete, the only thing missing are things that don't really apply to the PSP platform. See the link below...

http://sourceforge.net/projects/pthreads-emb/
I have to ask you a thing.

I'm trying to port STLPort in nd environment. The previous
attempts have failed, because I haven't a ready pthread library
for PSP.

I want to ask you the permission to integrate the code of your
library in ndNanoCPP library. In this way, Nanodesktop will became able
also to support C++, and not only C.

Can I integrate your library ?

Thank you
Filippo Battaglia
miniJB
Posts: 5
Joined: Sun Apr 06, 2008 8:54 pm

Post by miniJB »

I have a suggestion regarding vfpu access..


I note that pthread.h defines

Code: Select all

      /*
       * pthread_{get,set}scope
       */
      PTHREAD_SCOPE_PROCESS         = 0,
      PTHREAD_SCOPE_SYSTEM          = 1,  /* Default */
      PTHREAD_SCOPE_PROCESS_VFPU    = 2,  /* PSP specific */
however in pthread_attr_setscope.c

Code: Select all

int
pthread_attr_setscope (pthread_attr_t * attr, int contentionscope)
{
#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
  switch (contentionscope)
    {
    case PTHREAD_SCOPE_SYSTEM:
      (*attr)->contentionscope = contentionscope;
      return 0;
    case PTHREAD_SCOPE_PROCESS:
      return ENOTSUP;
    default:
      return EINVAL;
    }
#else
  return ENOSYS;
#endif
}
only PTHREAD_SCOPE_SYSTEM seems honoured, and then only if _POSIX_THREAD_PRIORITY_SCHEDULING is defined.

Further, in create.c

Code: Select all

pthread_create (pthread_t * tid,
                const pthread_attr_t * attr,
                void *(*start) (void *), void *arg)
{
 //...
      stackSize = a->stacksize;
      tp->detachState = a->detachstate;
      priority = a->param.sched_priority;

 //...
}
does not examine the contentionscope (for any osal!).

and in pte_osal_generic.h

Code: Select all

pte_osResult pte_osThreadCreate(pte_osThreadEntryPoint entryPoint,
                                int stackSize,
                                int initialPriority,
                                void *argv,
                                pte_osThreadHandle* ppte_osThreadHandle);
no contentionscope is passed.

and in psp_osal.c,

Code: Select all

pte_osResult pte_osThreadCreate(pte_osThreadEntryPoint entryPoint,
                                int stackSize,
                                int initialPriority,
                                void *argv,
                                pte_osThreadHandle* ppte_osThreadHandle)
{

  //...
  pspAttr = 0;

  //  printf("%s %p %d %d %d\n",threadName, pspStubThreadEntry, initialPriority, stackSize, pspAttr);
  threadId = sceKernelCreateThread(threadName,
                                   pspStubThreadEntry,
                                   initialPriority,
                                   stackSize,
                                   pspAttr,
                                   NULL);
  //...
}
pspAttr is set to 0 without any check against the contentionscope.


Would it not be advantageous to add the contentionscope parameter to pte_osThreadCreate (since stacksize and priority are already here) and noop that for the osals that do not support it? We can then use this to set pspAttr.

This would allow the PSP to use PTHREAD_SCOPE_PROCESS for user threads and PTHREAD_SCOPE_SYSTEM for kernel threads, and we could also alter PTHREAD_SCOPE_PROCESS_VFPU to simply PTHREAD_SCOPE_VFPU, allowing us to use the familiar OR ops to specify VFPU access.

I understand this may not work as the scope normally does on pthread-compliant systems, and that error detection when setting contentionscope values may not be as robust, but it would be handy to specify vfpu threads...

edit:
Just realised that using PTHREAD_SCOPE_SYSTEM to make a kernel pthread when in a user thread obviously wouldnt work. I guess this should give an error at thread creation, but this has little bearing on exposing the vfpu...
pegasus2000
Posts: 160
Joined: Wed Jul 12, 2006 7:09 am

Re: pthreads

Post by pegasus2000 »

hectic128 wrote:I've finished up an implementation of the pthreads API for the PSP. The implementation is pretty complete, the only thing missing are things that don't really apply to the PSP platform. See the link below...

http://sourceforge.net/projects/pthreads-emb/
I'm glab to tell that your library will be included in a new
component of the next Nanodesktop distribution called
NanoD (NanoC helper).

Naturally, in the documentation I'll do references to your
code and to your copyright.

Let's I know if you want some particular message or note
in nd documentation for the user, in way that I can fully
respect your license.

Bye
Filippo
hectic128
Posts: 12
Joined: Sat Nov 17, 2007 4:41 am

Re: pthreads

Post by hectic128 »

pegasus2000 wrote: Let's I know if you want some particular message or note
in nd documentation for the user, in way that I can fully
respect your license.

Bye
Filippo
That's cool!

The only thing I would ask is to also put a link to the sourceforge site.

If you find any problems or make any improvements I'd appreciate if you could send me patches.

I haven't actually used it in a real application yet, so I'd be interested to hear how it works out for you.
hectic128
Posts: 12
Joined: Sat Nov 17, 2007 4:41 am

Re: pthreads

Post by hectic128 »

miniJB wrote: Would it not be advantageous to add the contentionscope parameter to pte_osThreadCreate (since stacksize and priority are already here) and noop that for the osals that do not support it? We can then use this to set pspAttr.
You're exactly right, and I like your solution.

This is one of those areas that I glossed over when I originally did it, and apparently never got around to fixing it...

Thanks for catching this.
hectic128
Posts: 12
Joined: Sat Nov 17, 2007 4:41 am

Re: pthreads

Post by hectic128 »

miniJB wrote:Nice work. Tests run great on psp FAT (after some very slight changes to make printf work and compile to pbp), though a few take a while ;). Very nice test set too!

I like the abstraction layer you use to support multiple platforms... and that its inherited pthreads-win32's exception handling. I was in the middle of a pthreads port based on Pth (but using the psp threading libs rather than setjmp/etc), so you sure saved me some work!
How is the exception handling stuff working? This was one area that I was always concerned about - it seemed like it should work OK, and the tests passed but it still seemed like there could be problems.
miniJB
Posts: 5
Joined: Sun Apr 06, 2008 8:54 pm

Re: pthreads

Post by miniJB »

hectic128 wrote:How is the exception handling stuff working? This was one area that I was always concerned about - it seemed like it should work OK, and the tests passed but it still seemed like there could be problems.
I, like you, have so far only tested in under the unit tests that come with your pthreads-emb library. So far Ive only used your library to assist porting other software, and since (afaik) only the windows port of pthreads supports the exception handling, none of the software ive used pthreads-emb with required exception-handling.
User avatar
Wally
Posts: 663
Joined: Mon Sep 26, 2005 11:25 am

Post by Wally »

oooo

So this means sheepshaver can be ported now :D
miniJB
Posts: 5
Joined: Sun Apr 06, 2008 8:54 pm

Post by miniJB »

Sheepshaver? I (cautiously) googled that, not knowing what it was... Thank goodness that despite the dodgy-sounding name, googles first hits are benign... Seems like an interesting PowerPC emulator...

If it needs pthreads, then yes this definitely helps port it.
Unfortunately, the graphics for the linux version seem to use fbdev, you will need to write a fbdev "driver" for the psp or alter sheepshaver to use the ge... Theres a nice little project in itself!
paulotex
Posts: 19
Joined: Sun Jan 20, 2008 9:28 pm

Does it work in C++?

Post by paulotex »

Hi. I'm trying to compile an autotool project that needs pthreads and I was hoping that pthreads-emb could be a solution. The project uses g++ instead of gcc, and linking fails. I've reduced the problem to the conftest.cpp file:

Code: Select all

#include <pthread.h>
int
main &#40;&#41;
&#123;
pthread_t th; pthread_join&#40;th, 0&#41;;
                     pthread_attr_init&#40;0&#41;; pthread_cleanup_push&#40;0, 0&#41;;
                     pthread_create&#40;0,0,0,0&#41;; pthread_cleanup_pop&#40;0&#41;;
  ;
  return 0;
&#125;
If I use gcc, it compiles fine:

Code: Select all

$ psp-gcc -G0 -O2 -Wall -g -fno-strict-aliasing -G0 -L`psp-config -p`/lib -L`psp-config -P` -lc -lpspuser pth.c -lpthreads -lc -lpspuser -lpspsdk
However, if I use g++, things fail:

Code: Select all

psp-g++ -G0 -O2 -Wall -g -fno-strict-aliasing -G0 -I/Users/paulo/include  -L/Users/paulo/pspdev/psp/sdk/lib -lc -lpspuser pth.c -lpthreads -lc -lpspuser -lpspsdk
/var/folders/o4/o4pFMcJ3HoqF-VfCYAAZzk+++TI/-Tmp-//cchOjnUH.o&#58; In function `main'&#58;
/Users/paulo/Documents/PSP/Apps/Bookr/Arrr/djvulibre-3.5.21-PSP/pth.c&#58;5&#58; undefined reference to `pthread_join&#40;pte_handle_t, void**&#41;'
I noticed that pthreads-emb was compiled with gcc instead of g++. Could that be the problem?

Thanks.
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

I would assume that the header does not extern C the function definitions so when compiled g++ is trying to find the mangled form which doesn't exist.
hectic128
Posts: 12
Joined: Sat Nov 17, 2007 4:41 am

Post by hectic128 »

TyRaNiD wrote:I would assume that the header does not extern C the function definitions so when compiled g++ is trying to find the mangled form which doesn't exist.
Yup. Fix committed to SVN trunk.

Not sure how I missed that one...
paulotex
Posts: 19
Joined: Sun Jan 20, 2008 9:28 pm

Post by paulotex »

hectic128 wrote:Yup. Fix committed to SVN trunk.
It works now, thanks. I had to add -lpspsdk, but I guess that is normal.

Two other minor issues though:

* I noticed that "make install" works partially for the PSP. pthreads.h is not installed so I copied it manually. Perhaps you can add that too?

* I had to change "#include <sched.h>" to "#include <sys/sched.h>" so that the project I was compiling could find it. I don't know if this is a fault of my project (missing the correct -I path) or if it is your source that should be changed.

Thanks for the great work.
User avatar
Rex_VF5
Posts: 44
Joined: Wed Dec 26, 2007 6:24 am

Post by Rex_VF5 »

I am quite a lamer with C/C++ and even more with auto configure/build tools on Linux. However I am attempting to compile some library that uses standard configure script. When it looks for pthreads it just doesn't find it altough I have compiled it and installed them to PSP SDK (make install). The output is like this:

Code: Select all

--------------------------- pthread stuff -------------------------------------
checking for the pthreads library -lpthreads... no
checking whether pthreads work without any flags... no
checking whether pthreads work with -Kthread... no
checking whether pthreads work with -kthread... no
checking for the pthreads library -llthread... no
checking whether pthreads work with -pthread... no
checking whether pthreads work with -pthreads... no
checking whether pthreads work with -mthreads... no
checking for the pthreads library -lpthread... no
checking whether pthreads work with --thread-safe... no
checking whether pthreads work with -mt... no
checking for pthread-config... no
configure&#58; error&#58; POSIX threads are required to build this program
Can anybody advice on how to fix this so I can proceed? Thank you very much.
paulotex
Posts: 19
Joined: Sun Jan 20, 2008 9:28 pm

Post by paulotex »

Hi Rex_VF5.

Have a look in config.log, it will tell you why it failed. I compiled djvu, and this lib needed "-lpspuser" to pass the configure tests, so I had to fix "configure.ac". Besides, I also had to point it to the correct pthreads lib. So my configure command was:

Code: Select all

CXXFLAGS="-I`psp-config -p`/include -I`psp-config -P`/include -G0 -fno-rtti" LDFLAGS="-L`psp-config -P`/lib -L`psp-config -p`/lib -lc -lpspuser" PTHREAD_LIBS="-lpthreads -lpspsdk" ./configure --host=psp --prefix=`psp-config -P` --disable-shared --enable-static --without-x --without-qt
Quite a mouthfull :) but YMMV...
User avatar
Rex_VF5
Posts: 44
Joined: Wed Dec 26, 2007 6:24 am

Post by Rex_VF5 »

Thank you for answer but this is going to be more complicated (due to my newbieness):

Code: Select all

configure&#58;22142&#58; checking for pthread_join in LIBS=-lpthreads -lpspsdk with CFLAGS=
configure&#58;22172&#58; psp-gcc -o conftest -g -O2 -Os -Wall   -L/opt/toolchains/psp//psp/lib -L/opt/toolchains/psp//psp/sdk/lib conftest.c -lpthreads -lpspsdk -lc -lpsplibc -lpspuser >&5
/opt/toolchains/psp/bin/../lib/gcc/psp/4.3.1/../../../../psp/bin/ld&#58; cannot find -lpthreads
collect2&#58; ld returned 1 exit status
So it says it cannot find -lpthreads. However I have successfully built (make) and installed (make install) pthreads-emb. so now I have libpthread-psp.a in /opt/toolchains/psp/psp/lib. My environment variable for PSP are:

Code: Select all

export PSPDEV="/opt/toolchains/psp/"
export PSPSDK="$PSPDEV/psp/sdk"
export PATH="$PATH&#58;$PSPDEV/bin&#58;$PSPSDK/bin"
I am obviously missing somethiink ;-)
User avatar
Rex_VF5
Posts: 44
Joined: Wed Dec 26, 2007 6:24 am

Post by Rex_VF5 »

Stupid me - there was a typo -lpthreads-psp

Now I get something like this:

Code: Select all

--------------------------- pthread stuff -------------------------------------
checking for pthread_join in LIBS=-lpthread-psp -lpspsdk with CFLAGS=... yes
checking for joinable pthread attribute... unknown
checking if more special flags are required for pthreads... no
----------------------- pthread_rwlock_t stuff --------------------------------
checking if pthread_rwlock_t is available... no, needs to fallback to pthread_mutex
configure&#58; error&#58; pthread_rwlock_t not available
The relevant part of the config.log says:

Code: Select all

configure&#58;22608&#58; checking if pthread_rwlock_t is available
configure&#58;22637&#58; /opt/toolchains/psp/bin/psp-gcc -c  -I/opt/toolchains/psp/psp/include/c++/4.3.1     -I/opt/toolchains/psp/psp/include     -I/opt/toolchains/psp/psp/sdk/include  -L
/opt/toolchains/psp/lib/gcc/psp/4.3.1 -static -Os -Wall  conftest.c >&5
conftest.c&#58; In function 'main'&#58;
conftest.c&#58;52&#58; error&#58; 'pthread_rwlock_t' undeclared &#40;first use in this function&#41;
conftest.c&#58;52&#58; error&#58; &#40;Each undeclared identifier is reported only once
conftest.c&#58;52&#58; error&#58; for each function it appears in.&#41;
conftest.c&#58;52&#58; error&#58; 'x' undeclared &#40;first use in this function&#41;
Looking up my toolchain there is some pthread.h in /opt/toolchains/psp/psp/include and pthread_rwlock_t is declared in /opt/toolchains/psp/psp/include/sys/types.h that says:

Code: Select all

typedef __uint32_t pthread_rwlock_t;         /* POSIX RWLock Object */
However the pthread.h differs greatly from the one in pthreds_emb. When I overwrote the toolchain one with pthreads_emb one (and changed the include #include <sys/sched.h>) it started to work!!!
User avatar
Rex_VF5
Posts: 44
Joined: Wed Dec 26, 2007 6:24 am

Post by Rex_VF5 »

I have used this library to compile another library. When it tries to create a thread it crashes like this:

Code: Select all

host0&#58;/> Exception - Address load/inst fetch
Thread ID - 0x04E49B7D
Th Name   - pthread0002__8a05110
Module ID - 0x01FF992D
Mod Name  - upnpcmd
EPC       - 0x0883A1C4
Cause     - 0x10000010
Exception - Address load/inst fetch
BadVAddr  - 0xFFFFFFFC
Thread ID - 0x04D27239
Status    - 0x00088613
Th Name   - user_main
zr&#58;0x00000000 at&#58;0xDEADBEEF v0&#58;0xFFFFFFFC v1&#58;0xFFFFFFFC
Module ID - 0x01FF992D
a0&#58;0x08A05110 a1&#58;0x08970000 a2&#58;0xFFFFFFFC a3&#58;0x00000010
Mod Name  - upnpcmd
t0&#58;0x00000020 t1&#58;0x0BBF4C04 t2&#58;0x00000000 t3&#58;0x08A05110
EPC       - 0x0883A1C4
t4&#58;0x00000001 t5&#58;0x0FFFFFFF t6&#58;0xFFFFFFFF t7&#58;0x0886A129
Cause     - 0x10000010
s0&#58;0xDEADBEEF s1&#58;0xDEADBEEF s2&#58;0xDEADBEEF s3&#58;0xDEADBEEF
BadVAddr  - 0xFFFFFFFC
s4&#58;0xDEADBEEF s5&#58;0xDEADBEEF s6&#58;0xDEADBEEF s7&#58;0xDEADBEEF
Status    - 0x20088613
t8&#58;0x00000010 t9&#58;0x00000057 k0&#58;0x0BBF4F00 k1&#58;0x00000000
zr&#58;0x00000000 at&#58;0xDEADBEEF v0&#58;0xFFFFFFFC v1&#58;0xFFFFFFFC
gp&#58;0x08875F60 sp&#58;0x0BBF4EB8 fp&#58;0x0BBF4EC0 ra&#58;0x08839690
a0&#58;0x00000000 a1&#58;0x08970000 a2&#58;0xFFFFFFFC a3&#58;0x0BBBFC10
0x0883A1C4&#58; 0x8C430000 '..C.' - lw         $v1, 0&#40;$v0&#41;
t0&#58;0xFFFFFFFF t1&#58;0x0BBBFC10 t2&#58;0xDEADBEEF t3&#58;0xDEADBEEF
t4&#58;0xDEADBEEF t5&#58;0xDEADBEEF t6&#58;0xDEADBEEF t7&#58;0xDEADBEEF
s0&#58;0x00000000 s1&#58;0x04E4FD75 s2&#58;0x04E4FD75 s3&#58;0x08A04F7C
s4&#58;0x00000000 s5&#58;0x00000013 s6&#58;0xDEADBEEF s7&#58;0xDEADBEEF
t8&#58;0xDEADBEEF t9&#58;0xDEADBEEF k0&#58;0x0BBBFF00 k1&#58;0x00000000
gp&#58;0x08875F60 sp&#58;0x0BBBFCA8 fp&#58;0x0BBBFD98 ra&#58;0x088399D4
0x0883A1C4&#58; 0x8C430000 '..C.' - lw         $v1, 0&#40;$v0&#41;

host0&#58;/> disasm $epc
0x0883A1C4&#58; 0x8C430000 '..C.' - lw         $v1, 0&#40;$v0&#41;
It crashes on the final statement *tid = thread; of pthread_create in create.c. I have added some debugging outputs into the code like this:

Code: Select all

FAIL0&#58;
  if &#40;result != 0&#41;
    &#123;

      pte_threadDestroy &#40;thread&#41;;
      tp = NULL;

      if &#40;parms != NULL&#41;
        &#123;
          free &#40;parms&#41;;
        &#125;
    &#125;
  else
    &#123;
      if &#40;tid != NULL&#41;
pspDebugScreenPrintf&#40;"\ntid=%lX", tid&#41;;
pspDebugScreenPrintf&#40;"\nthread=%lX", thread&#41;;
        &#123;
          *tid = thread;
        &#125;
    &#125;

  return &#40;result&#41;;

&#125;
and the result is like this:

Code: Select all

tid=BBBFDA0
thread=BBBFD70
Please anybody help me. I am really lost about what possibly do next.
cosmito
Posts: 307
Joined: Sun Mar 04, 2007 4:26 am
Location: Portugal
Contact:

Post by cosmito »

hectic128 wrote:I've finished up an implementation of the pthreads API for the PSP.
Do you see as viable to take this PSP port as starting point for a PS2 port or would be simpler to port from the original instead?
User avatar
Rex_VF5
Posts: 44
Joined: Wed Dec 26, 2007 6:24 am

Post by Rex_VF5 »

I have been trying to figure this out whole weekend but didn't progress much. However I compiled pthread-psp-test and ran it. It gets stuck at Semaphore test #4. I thought maybe the FW was to blame - I was on 5.00 M33-3 so I downgraded to 3.90 M33-3 but the result is the same. Under 5.00 I tried to run the test through pslink but there was no exception. Maybe the exception is not related to the hang in Semaphore test #4...

Can anybody please advice on what to do next? I have uploaded my compiled pthread-psp-test here: http://d01.megashares.com/?d01=12e1682 so other people can try and tell me if it is something on my side... Any help is greatly welcome...
mase
Posts: 23
Joined: Fri Aug 21, 2009 3:04 am

Post by mase »

I have include pthread.h.
But I get an error:

Code: Select all

&#8216;pthread_t&#8217; does not name a type
May the force be with us!
Post Reply