PosixHelpers.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. #if IL2CPP_TARGET_POSIX
  3. #include <pthread.h>
  4. #include <time.h>
  5. #include <sys/poll.h>
  6. #include "os/Thread.h"
  7. #include "os/Socket.h"
  8. namespace il2cpp
  9. {
  10. namespace os
  11. {
  12. namespace posix
  13. {
  14. inline timespec Ticks100NanosecondsToTimespec(int64_t ticks)
  15. {
  16. timespec result;
  17. result.tv_sec = ticks / 10000000;
  18. result.tv_nsec = (ticks % 10000000) * 100;
  19. return result;
  20. }
  21. inline timespec MillisecondsToTimespec(uint32_t ms)
  22. {
  23. timespec result;
  24. result.tv_sec = ms / 1000;
  25. result.tv_nsec = (ms % 1000) * 1000000;
  26. return result;
  27. }
  28. inline Thread::ThreadId PosixThreadIdToThreadId(pthread_t thread)
  29. {
  30. Thread::ThreadId threadId = 0;
  31. memcpy(&threadId, &thread, std::min(sizeof(threadId), sizeof(thread)));
  32. return threadId;
  33. }
  34. struct PosixAutoLock
  35. {
  36. pthread_mutex_t* mutex;
  37. PosixAutoLock(pthread_mutex_t* m)
  38. : mutex(m) { pthread_mutex_lock(mutex); }
  39. ~PosixAutoLock()
  40. { pthread_mutex_unlock(mutex); }
  41. };
  42. inline short PollFlagsToPollEvents(PollFlags flags)
  43. {
  44. return (short)flags;
  45. }
  46. inline PollFlags PollEventsToPollFlags(short events)
  47. {
  48. return (PollFlags)events;
  49. }
  50. int Poll(pollfd* handles, int numHandles, int timeout);
  51. }
  52. }
  53. }
  54. #endif // IL2CPP_TARGET_POSIX