tryentercs2.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * tryentercs.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-win32 - POSIX Threads Library for Win32
  8. * Copyright(C) 1998 John E. Bossom
  9. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. * The current list of contributors is contained
  14. * in the file CONTRIBUTORS included with the source
  15. * code distribution. The list can also be seen at the
  16. * following World Wide Web location:
  17. * http://sources.redhat.com/pthreads-win32/contributors.html
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library in the file COPYING.LIB;
  31. * if not, write to the Free Software Foundation, Inc.,
  32. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  33. *
  34. * --------------------------------------------------------------------------
  35. *
  36. * See if we have the TryEnterCriticalSection function.
  37. * Does not use any part of pthreads.
  38. */
  39. #include <windows.h>
  40. #include <process.h>
  41. #include <stdio.h>
  42. /*
  43. * Function pointer to TryEnterCriticalSection if it exists
  44. * - otherwise NULL
  45. */
  46. BOOL (WINAPI *_try_enter_critical_section)(LPCRITICAL_SECTION) = NULL;
  47. /*
  48. * Handle to kernel32.dll
  49. */
  50. static HINSTANCE _h_kernel32;
  51. int
  52. main()
  53. {
  54. LPCRITICAL_SECTION lpcs = NULL;
  55. SetLastError(0);
  56. printf("Last Error [main enter] %ld\n", (long) GetLastError());
  57. /*
  58. * Load KERNEL32 and try to get address of TryEnterCriticalSection
  59. */
  60. _h_kernel32 = LoadLibrary(TEXT("KERNEL32.DLL"));
  61. _try_enter_critical_section =
  62. (BOOL (PT_STDCALL *)(LPCRITICAL_SECTION))
  63. GetProcAddress(_h_kernel32,
  64. (LPCSTR) "TryEnterCriticalSection");
  65. if (_try_enter_critical_section != NULL)
  66. {
  67. SetLastError(0);
  68. (*_try_enter_critical_section)(lpcs);
  69. printf("Last Error [try enter] %ld\n", (long) GetLastError());
  70. }
  71. (void) FreeLibrary(_h_kernel32);
  72. printf("This system %s TryEnterCriticalSection.\n",
  73. (_try_enter_critical_section == NULL) ? "DOES NOT SUPPORT" : "SUPPORTS");
  74. printf("POSIX Mutexes will be based on Win32 %s.\n",
  75. (_try_enter_critical_section == NULL) ? "Mutexes" : "Critical Sections");
  76. return(0);
  77. }