refcnt.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*------------------------------------------------------------------------
  2. * Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
  3. *
  4. * This file is part of the ZBar Bar Code Reader.
  5. *
  6. * The ZBar Bar Code Reader is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU Lesser Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * The ZBar Bar Code Reader is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser Public License
  17. * along with the ZBar Bar Code Reader; if not, write to the Free
  18. * Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  19. * Boston, MA 02110-1301 USA
  20. *
  21. * http://sourceforge.net/projects/zbar
  22. *------------------------------------------------------------------------*/
  23. #ifndef _REFCNT_H_
  24. #define _REFCNT_H_
  25. #include <config.h>
  26. #include "debug.h"
  27. //#include <assert.h>
  28. #if defined(_WIN32)
  29. # include <windows.h>
  30. typedef volatile LONG refcnt_t; /* FIXME where did volatile come from? */
  31. static inline int _zbar_refcnt (refcnt_t *cnt,
  32. int delta)
  33. {
  34. int rc = -1;
  35. if(delta > 0)
  36. while(delta--)
  37. rc = InterlockedIncrement(cnt);
  38. else if(delta < 0)
  39. while(delta++)
  40. rc = InterlockedDecrement(cnt);
  41. assert(rc >= 0);
  42. return(rc);
  43. }
  44. #elif defined(HAVE_LIBPTHREAD)
  45. # include <pthread.h>
  46. typedef int refcnt_t;
  47. extern pthread_mutex_t _zbar_reflock;
  48. static inline int _zbar_refcnt (refcnt_t *cnt,
  49. int delta)
  50. {
  51. pthread_mutex_lock(&_zbar_reflock);
  52. int rc = (*cnt += delta);
  53. pthread_mutex_unlock(&_zbar_reflock);
  54. assert(rc >= 0);
  55. return(rc);
  56. }
  57. #else
  58. typedef int refcnt_t;
  59. static inline int _zbar_refcnt (refcnt_t *cnt,
  60. int delta)
  61. {
  62. int rc = (*cnt += delta);
  63. assert(rc >= 0);
  64. return(rc);
  65. }
  66. #endif
  67. void _zbar_refcnt_init(void);
  68. #endif