forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

pthreads

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
hectic128



Joined: 17 Nov 2007
Posts: 12

PostPosted: Sat Apr 05, 2008 2:26 pm    Post subject: pthreads Reply with quote

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/
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Sat Apr 05, 2008 6:19 pm    Post subject: Reply with quote

Cool! Nice work.
Back to top
View user's profile Send private message
jean



Joined: 05 Jan 2008
Posts: 489

PostPosted: Sat Apr 05, 2008 6:37 pm    Post subject: Reply with quote

Not looked into it yet, but it sounds good! Nice job man!
Back to top
View user's profile Send private message
miniJB



Joined: 06 Apr 2008
Posts: 5

PostPosted: Mon Apr 07, 2008 8:21 am    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message
pegasus2000



Joined: 12 Jul 2006
Posts: 160

PostPosted: Tue Apr 08, 2008 7:14 pm    Post subject: Re: pthreads Reply with quote

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
Back to top
View user's profile Send private message
miniJB



Joined: 06 Apr 2008
Posts: 5

PostPosted: Wed Apr 09, 2008 1:09 pm    Post subject: Reply with quote

I have a suggestion regarding vfpu access..


I note that pthread.h defines
Code:
      /*
       * 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:
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:
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:
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:

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...
Back to top
View user's profile Send private message
pegasus2000



Joined: 12 Jul 2006
Posts: 160

PostPosted: Fri Apr 18, 2008 12:02 am    Post subject: Re: pthreads Reply with quote

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
Back to top
View user's profile Send private message
hectic128



Joined: 17 Nov 2007
Posts: 12

PostPosted: Sun May 11, 2008 7:55 am    Post subject: Re: pthreads Reply with quote

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.
Back to top
View user's profile Send private message
hectic128



Joined: 17 Nov 2007
Posts: 12

PostPosted: Sun May 11, 2008 8:05 am    Post subject: Re: pthreads Reply with quote

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.
Back to top
View user's profile Send private message
hectic128



Joined: 17 Nov 2007
Posts: 12

PostPosted: Sun May 11, 2008 8:19 am    Post subject: Re: pthreads Reply with quote

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.
Back to top
View user's profile Send private message
miniJB



Joined: 06 Apr 2008
Posts: 5

PostPosted: Sun May 11, 2008 12:06 pm    Post subject: Re: pthreads Reply with quote

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.
Back to top
View user's profile Send private message
Wally



Joined: 26 Sep 2005
Posts: 672

PostPosted: Sun May 11, 2008 2:47 pm    Post subject: Reply with quote

oooo

So this means sheepshaver can be ported now :D
Back to top
View user's profile Send private message AIM Address
miniJB



Joined: 06 Apr 2008
Posts: 5

PostPosted: Sun May 11, 2008 9:23 pm    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message
paulotex



Joined: 20 Jan 2008
Posts: 19

PostPosted: Mon Sep 22, 2008 1:58 am    Post subject: Does it work in C++? Reply with quote

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:
#include <pthread.h>
int
main ()
{
pthread_t th; pthread_join(th, 0);
                     pthread_attr_init(0); pthread_cleanup_push(0, 0);
                     pthread_create(0,0,0,0); pthread_cleanup_pop(0);
  ;
  return 0;
}


If I use gcc, it compiles fine:
Code:
$ 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:

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: In function `main':
/Users/paulo/Documents/PSP/Apps/Bookr/Arrr/djvulibre-3.5.21-PSP/pth.c:5: undefined reference to `pthread_join(pte_handle_t, void**)'


I noticed that pthreads-emb was compiled with gcc instead of g++. Could that be the problem?

Thanks.
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Mon Sep 22, 2008 5:54 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
hectic128



Joined: 17 Nov 2007
Posts: 12

PostPosted: Mon Sep 22, 2008 9:18 am    Post subject: Reply with quote

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...
Back to top
View user's profile Send private message
paulotex



Joined: 20 Jan 2008
Posts: 19

PostPosted: Mon Sep 22, 2008 5:47 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Rex_VF5



Joined: 26 Dec 2007
Posts: 44

PostPosted: Thu Oct 30, 2008 8:10 pm    Post subject: Reply with quote

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:
--------------------------- 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: error: POSIX threads are required to build this program


Can anybody advice on how to fix this so I can proceed? Thank you very much.
Back to top
View user's profile Send private message
paulotex



Joined: 20 Jan 2008
Posts: 19

PostPosted: Thu Oct 30, 2008 11:50 pm    Post subject: Reply with quote

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:

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...
Back to top
View user's profile Send private message
Rex_VF5



Joined: 26 Dec 2007
Posts: 44

PostPosted: Fri Oct 31, 2008 12:09 am    Post subject: Reply with quote

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

Code:
configure:22142: checking for pthread_join in LIBS=-lpthreads -lpspsdk with CFLAGS=
configure:22172: 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: cannot find -lpthreads
collect2: 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:
export PSPDEV="/opt/toolchains/psp/"
export PSPSDK="$PSPDEV/psp/sdk"
export PATH="$PATH:$PSPDEV/bin:$PSPSDK/bin"


I am obviously missing somethiink ;-)
Back to top
View user's profile Send private message
Rex_VF5



Joined: 26 Dec 2007
Posts: 44

PostPosted: Fri Oct 31, 2008 7:06 am    Post subject: Reply with quote

Stupid me - there was a typo -lpthreads-psp

Now I get something like this:

Code:
--------------------------- 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: error: pthread_rwlock_t not available


The relevant part of the config.log says:
Code:
configure:22608: checking if pthread_rwlock_t is available
configure:22637: /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: In function 'main':
conftest.c:52: error: 'pthread_rwlock_t' undeclared (first use in this function)
conftest.c:52: error: (Each undeclared identifier is reported only once
conftest.c:52: error: for each function it appears in.)
conftest.c:52: error: 'x' undeclared (first use in this function)


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:
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!!!
Back to top
View user's profile Send private message
Rex_VF5



Joined: 26 Dec 2007
Posts: 44

PostPosted: Sat Nov 08, 2008 1:26 am    Post subject: Reply with quote

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

Code:
host0:/> 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:0x00000000 at:0xDEADBEEF v0:0xFFFFFFFC v1:0xFFFFFFFC
Module ID - 0x01FF992D
a0:0x08A05110 a1:0x08970000 a2:0xFFFFFFFC a3:0x00000010
Mod Name  - upnpcmd
t0:0x00000020 t1:0x0BBF4C04 t2:0x00000000 t3:0x08A05110
EPC       - 0x0883A1C4
t4:0x00000001 t5:0x0FFFFFFF t6:0xFFFFFFFF t7:0x0886A129
Cause     - 0x10000010
s0:0xDEADBEEF s1:0xDEADBEEF s2:0xDEADBEEF s3:0xDEADBEEF
BadVAddr  - 0xFFFFFFFC
s4:0xDEADBEEF s5:0xDEADBEEF s6:0xDEADBEEF s7:0xDEADBEEF
Status    - 0x20088613
t8:0x00000010 t9:0x00000057 k0:0x0BBF4F00 k1:0x00000000
zr:0x00000000 at:0xDEADBEEF v0:0xFFFFFFFC v1:0xFFFFFFFC
gp:0x08875F60 sp:0x0BBF4EB8 fp:0x0BBF4EC0 ra:0x08839690
a0:0x00000000 a1:0x08970000 a2:0xFFFFFFFC a3:0x0BBBFC10
0x0883A1C4: 0x8C430000 '..C.' - lw         $v1, 0($v0)
t0:0xFFFFFFFF t1:0x0BBBFC10 t2:0xDEADBEEF t3:0xDEADBEEF
t4:0xDEADBEEF t5:0xDEADBEEF t6:0xDEADBEEF t7:0xDEADBEEF
s0:0x00000000 s1:0x04E4FD75 s2:0x04E4FD75 s3:0x08A04F7C
s4:0x00000000 s5:0x00000013 s6:0xDEADBEEF s7:0xDEADBEEF
t8:0xDEADBEEF t9:0xDEADBEEF k0:0x0BBBFF00 k1:0x00000000
gp:0x08875F60 sp:0x0BBBFCA8 fp:0x0BBBFD98 ra:0x088399D4
0x0883A1C4: 0x8C430000 '..C.' - lw         $v1, 0($v0)

host0:/> disasm $epc
0x0883A1C4: 0x8C430000 '..C.' - lw         $v1, 0($v0)


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:
FAIL0:
  if (result != 0)
    {

      pte_threadDestroy (thread);
      tp = NULL;

      if (parms != NULL)
        {
          free (parms);
        }
    }
  else
    {
      if (tid != NULL)
pspDebugScreenPrintf("\ntid=%lX", tid);
pspDebugScreenPrintf("\nthread=%lX", thread);
        {
          *tid = thread;
        }
    }

  return (result);

}
and the result is like this:
Code:
tid=BBBFDA0
thread=BBBFD70


Please anybody help me. I am really lost about what possibly do next.
Back to top
View user's profile Send private message
cosmito



Joined: 04 Mar 2007
Posts: 314
Location: Portugal

PostPosted: Sat Nov 08, 2008 9:21 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message Visit poster's website
Rex_VF5



Joined: 26 Dec 2007
Posts: 44

PostPosted: Mon Nov 10, 2008 7:07 am    Post subject: Reply with quote

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...
Back to top
View user's profile Send private message
mase



Joined: 21 Aug 2009
Posts: 23

PostPosted: Sun Aug 23, 2009 9:37 pm    Post subject: Reply with quote

I have include pthread.h.
But I get an error:
Code:
‘pthread_t’ does not name a type

_________________
May the force be with us!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group