lock.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. (c) Copyright 2001-2009 The world wide DirectFB Open Source Community (directfb.org)
  3. (c) Copyright 2000-2004 Convergence (integrated media) GmbH
  4. All rights reserved.
  5. Written by Denis Oliver Kropp <dok@directfb.org>,
  6. Andreas Hundt <andi@fischlustig.de>,
  7. Sven Neumann <neo@directfb.org>,
  8. Ville Syrjälä <syrjala@sci.fi> and
  9. Claudio Ciccani <klan@users.sf.net>.
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 2 of the License, or (at your option) any later version.
  14. This library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, write to the
  20. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.
  22. */
  23. #ifndef __FUSION__LOCK_H__
  24. #define __FUSION__LOCK_H__
  25. #include <pthread.h>
  26. #include <fusion/types.h>
  27. #include <direct/messages.h>
  28. #include <direct/util.h>
  29. typedef struct {
  30. pthread_mutex_t lock;
  31. pthread_cond_t cond;
  32. int count;
  33. char *name;
  34. } FusionSkirmishSingle;
  35. typedef union {
  36. /* multi app */
  37. struct {
  38. int id;
  39. const FusionWorldShared *shared;
  40. /* builtin impl */
  41. struct {
  42. unsigned int locked;
  43. pid_t owner;
  44. DirectLink *waiting;
  45. bool requested;
  46. bool destroyed;
  47. } builtin;
  48. } multi;
  49. /* single app */
  50. FusionSkirmishSingle *single;
  51. } FusionSkirmish;
  52. /*
  53. * Initialize.
  54. */
  55. DirectResult fusion_skirmish_init ( FusionSkirmish *skirmish,
  56. const char *name,
  57. const FusionWorld *world );
  58. /*
  59. * Lock.
  60. */
  61. DirectResult fusion_skirmish_prevail( FusionSkirmish *skirmish );
  62. /*
  63. * Try lock.
  64. */
  65. DirectResult fusion_skirmish_swoop ( FusionSkirmish *skirmish );
  66. /*
  67. * Find out how many times current thread has acquired lock.
  68. */
  69. DirectResult fusion_skirmish_lock_count( FusionSkirmish *skirmish, int *lock_count );
  70. /*
  71. * Unlock.
  72. */
  73. DirectResult fusion_skirmish_dismiss( FusionSkirmish *skirmish );
  74. /*
  75. * Deinitialize.
  76. */
  77. DirectResult fusion_skirmish_destroy( FusionSkirmish *skirmish );
  78. /*
  79. * Wait & Notify.
  80. *
  81. * Must be locked!
  82. */
  83. DirectResult fusion_skirmish_wait ( FusionSkirmish *skirmish,
  84. unsigned int timeout );
  85. DirectResult fusion_skirmish_notify ( FusionSkirmish *skirmish );
  86. #if D_DEBUG_ENABLED
  87. #define FUSION_SKIRMISH_ASSERT(skirmish) \
  88. do { \
  89. int lock_count; \
  90. \
  91. D_ASSERT( skirmish != NULL ); \
  92. \
  93. D_ASSERT( fusion_skirmish_lock_count( skirmish, &lock_count ) == DR_OK ); \
  94. D_ASSERT( lock_count > 0 ); \
  95. } while (0)
  96. #else
  97. #define FUSION_SKIRMISH_ASSERT(skirmish) \
  98. do { \
  99. } while (0)
  100. #endif
  101. #endif