refcnt.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <assert.h>
  27. #if defined(_WIN32)
  28. # include <windows.h>
  29. typedef volatile LONG refcnt_t; /* FIXME where did volatile come from? */
  30. static inline int _zbar_refcnt (refcnt_t *cnt,
  31. int delta)
  32. {
  33. int rc = -1;
  34. if(delta > 0)
  35. while(delta--)
  36. rc = InterlockedIncrement(cnt);
  37. else if(delta < 0)
  38. while(delta++)
  39. rc = InterlockedDecrement(cnt);
  40. assert(rc >= 0);
  41. return(rc);
  42. }
  43. #elif defined(HAVE_LIBPTHREAD)
  44. # include <pthread.h>
  45. typedef int refcnt_t;
  46. extern pthread_mutex_t _zbar_reflock;
  47. static inline int _zbar_refcnt (refcnt_t *cnt,
  48. int delta)
  49. {
  50. pthread_mutex_lock(&_zbar_reflock);
  51. int rc = (*cnt += delta);
  52. pthread_mutex_unlock(&_zbar_reflock);
  53. assert(rc >= 0);
  54. return(rc);
  55. }
  56. #else
  57. typedef int refcnt_t;
  58. static inline int _zbar_refcnt (refcnt_t *cnt,
  59. int delta)
  60. {
  61. int rc = (*cnt += delta);
  62. assert(rc >= 0);
  63. return(rc);
  64. }
  65. #endif
  66. void _zbar_refcnt_init(void);
  67. #endif