eathread_pthread_stack_info.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include <eathread/eathread_callstack.h>
  5. #include <stdlib.h>
  6. namespace EA
  7. {
  8. namespace Thread
  9. {
  10. // With some implementations of pthread_disabled, the stack base is returned by pthread_disabled as NULL if it's the main thread,
  11. // or possibly if it's a thread you created but didn't call pthread_disabled_attr_setstack manually to provide your
  12. // own stack. It's impossible for us to tell here whether will be such a NULL return value, so we just do what
  13. // we can and the user nees to beware that a NULL return value means that the system doesn't provide the
  14. // given information for the current thread. This function returns false and sets pBase and pLimit to NULL in
  15. // the case that the thread base and limit weren't returned by the system or were returned as NULL.
  16. bool GetPthreadStackInfo(void** pBase, void** pLimit)
  17. {
  18. bool returnValue = false;
  19. size_t stackSize;
  20. void* pBaseTemp = NULL;
  21. void* pLimitTemp = NULL;
  22. ScePthreadAttr attr;
  23. scePthreadAttrInit(&attr);
  24. int result = scePthreadAttrGet(scePthreadSelf(), &attr);
  25. if(result == 0) // SCE_OK (=0)
  26. {
  27. result = scePthreadAttrGetstack(&attr, &pLimitTemp, &stackSize);
  28. if((result == 0) && (pLimitTemp != NULL)) // If success...
  29. {
  30. pBaseTemp = (void*)((uintptr_t)pLimitTemp + stackSize); // p is returned by pthread_disabled_attr_getstack as the lowest address in the stack, and not the stack base.
  31. returnValue = true;
  32. }
  33. else
  34. {
  35. pBaseTemp = NULL;
  36. pLimitTemp = NULL;
  37. }
  38. }
  39. scePthreadAttrDestroy(&attr);
  40. if(pBase)
  41. *pBase = pBaseTemp;
  42. if(pLimit)
  43. *pLimit = pLimitTemp;
  44. return returnValue;
  45. }
  46. } // namespace Callstack
  47. } // namespace EA