windows-rwlock.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Read-write locks (native Windows implementation).
  2. Copyright (C) 2005-2020 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Bruno Haible <bruno@clisp.org>, 2005.
  14. Based on GCC's gthr-win32.h. */
  15. #ifndef _WINDOWS_RWLOCK_H
  16. #define _WINDOWS_RWLOCK_H
  17. #define WIN32_LEAN_AND_MEAN /* avoid including junk */
  18. #include <windows.h>
  19. #include "windows-initguard.h"
  20. /* It is impossible to implement read-write locks using plain locks, without
  21. introducing an extra thread dedicated to managing read-write locks.
  22. Therefore here we need to use the low-level Event type. */
  23. typedef struct
  24. {
  25. HANDLE *array; /* array of waiting threads, each represented by an event */
  26. unsigned int count; /* number of waiting threads */
  27. unsigned int alloc; /* length of allocated array */
  28. unsigned int offset; /* index of first waiting thread in array */
  29. }
  30. glwthread_carray_waitqueue_t;
  31. typedef struct
  32. {
  33. glwthread_initguard_t guard; /* protects the initialization */
  34. CRITICAL_SECTION lock; /* protects the remaining fields */
  35. glwthread_carray_waitqueue_t waiting_readers; /* waiting readers */
  36. glwthread_carray_waitqueue_t waiting_writers; /* waiting writers */
  37. int runcount; /* number of readers running, or -1 when a writer runs */
  38. }
  39. glwthread_rwlock_t;
  40. #define GLWTHREAD_RWLOCK_INIT { GLWTHREAD_INITGUARD_INIT }
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. extern void glwthread_rwlock_init (glwthread_rwlock_t *lock);
  45. extern int glwthread_rwlock_rdlock (glwthread_rwlock_t *lock);
  46. extern int glwthread_rwlock_wrlock (glwthread_rwlock_t *lock);
  47. extern int glwthread_rwlock_tryrdlock (glwthread_rwlock_t *lock);
  48. extern int glwthread_rwlock_trywrlock (glwthread_rwlock_t *lock);
  49. extern int glwthread_rwlock_unlock (glwthread_rwlock_t *lock);
  50. extern int glwthread_rwlock_destroy (glwthread_rwlock_t *lock);
  51. #ifdef __cplusplus
  52. }
  53. #endif
  54. #endif /* _WINDOWS_RWLOCK_H */