Interop.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Haukcode.HighResolutionTimer
  4. {
  5. // Disable these StyleCop rules for this file, as we are using native names here.
  6. #pragma warning disable SA1300 // Element should begin with upper-case letter
  7. internal partial class Interop
  8. {
  9. private const string LibcLibrary = "libc";
  10. public enum ClockIds : int
  11. {
  12. CLOCK_REALTIME = 0,
  13. CLOCK_MONOTONIC = 1,
  14. CLOCK_PROCESS_CPUTIME_ID = 2,
  15. CLOCK_THREAD_CPUTIME_ID = 3,
  16. CLOCK_MONOTONIC_RAW = 4,
  17. CLOCK_REALTIME_COARSE = 5,
  18. CLOCK_MONOTONIC_COARSE = 6,
  19. CLOCK_BOOTTIME = 7,
  20. CLOCK_REALTIME_ALARM = 8,
  21. CLOCK_BOOTTIME_ALARM = 9
  22. }
  23. [StructLayout(LayoutKind.Explicit)]
  24. public class Timespec
  25. {
  26. [FieldOffset(0)]
  27. public uint tv_sec; /* seconds */
  28. [FieldOffset(4)]
  29. public uint tv_nsec; /* nanoseconds */
  30. };
  31. [StructLayout(LayoutKind.Explicit)]
  32. public class Itimerspec
  33. {
  34. [FieldOffset(0)]
  35. public Timespec it_interval= new Timespec(); /* timer period */
  36. [FieldOffset(8)]
  37. public Timespec it_value=new Timespec(); /* timer expiration */
  38. };
  39. [DllImport(LibcLibrary, SetLastError = true,EntryPoint = "timerfd_create")]
  40. internal static extern int Timerfd_Create(ClockIds clockId, int flags);
  41. [DllImport(LibcLibrary, SetLastError = true,EntryPoint = "timerfd_settime")]
  42. internal static extern int Timerfd_Settime(int fd, int flags, Itimerspec new_value, Itimerspec old_value);
  43. [DllImport(LibcLibrary, SetLastError = true,EntryPoint ="read")]
  44. internal static extern int Read(int fd, IntPtr buf, int count);
  45. [DllImport(LibcLibrary,EntryPoint ="close")]
  46. internal static extern int Close(int fd);
  47. }
  48. }