NativeEventCalls.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // System.Threading.AutoResetEvent.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. // Veronica De Santis ([email protected])
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Runtime.CompilerServices;
  34. using Microsoft.Win32.SafeHandles;
  35. #if !MOBILE && !NETCORE
  36. using System.Security.AccessControl;
  37. using System.IO;
  38. #endif
  39. namespace System.Threading
  40. {
  41. internal static class NativeEventCalls
  42. {
  43. public unsafe static IntPtr CreateEvent_internal (bool manual, bool initial, string name, out int errorCode)
  44. {
  45. // FIXME check for embedded nuls in name
  46. fixed (char *fixed_name = name)
  47. return CreateEvent_icall (manual, initial, fixed_name, name?.Length ?? 0, out errorCode);
  48. }
  49. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  50. private unsafe static extern IntPtr CreateEvent_icall (bool manual, bool initial, char *name, int name_length, out int errorCode);
  51. public static bool SetEvent (SafeWaitHandle handle)
  52. {
  53. bool release = false;
  54. try {
  55. handle.DangerousAddRef (ref release);
  56. return SetEvent_internal (handle.DangerousGetHandle ());
  57. } finally {
  58. if (release)
  59. handle.DangerousRelease ();
  60. }
  61. }
  62. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  63. static extern bool SetEvent_internal(IntPtr handle);
  64. public static bool ResetEvent (SafeWaitHandle handle)
  65. {
  66. bool release = false;
  67. try {
  68. handle.DangerousAddRef (ref release);
  69. return ResetEvent_internal (handle.DangerousGetHandle ());
  70. } finally {
  71. if (release)
  72. handle.DangerousRelease ();
  73. }
  74. }
  75. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  76. static extern bool ResetEvent_internal(IntPtr handle);
  77. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  78. public static extern void CloseEvent_internal (IntPtr handle);
  79. #if !MOBILE && !NETCORE
  80. public unsafe static IntPtr OpenEvent_internal (string name, EventWaitHandleRights rights, out int errorCode)
  81. {
  82. // FIXME check for embedded nuls in name
  83. fixed (char *fixed_name = name)
  84. return OpenEvent_icall (fixed_name, name?.Length ?? 0, rights, out errorCode);
  85. }
  86. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  87. private unsafe static extern IntPtr OpenEvent_icall (char *name, int name_length, EventWaitHandleRights rights, out int errorCode);
  88. #endif
  89. }
  90. }