test_semaphore.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (C) 2009 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. #ifndef _TEST_SEMAPHORE_H_
  23. #define _TEST_SEMAPHORE_H_
  24. void *threadfunc_semaphore1(void *parm)
  25. {
  26. tsk_semaphore_handle_t *semaphore = (tsk_semaphore_handle_t *)parm;
  27. int ret = 0;
  28. printf("threadfunc_semaphore1/// START %d\n", ret);
  29. ret = tsk_semaphore_decrement(semaphore);
  30. printf("threadfunc_semaphore1/// STOP %d\n", ret);
  31. return 0;
  32. }
  33. void *threadfunc_semaphore2(void *parm)
  34. {
  35. tsk_semaphore_handle_t *semaphore = (tsk_semaphore_handle_t *)parm;
  36. int ret = 0;
  37. printf("threadfunc_semaphore2/// START %d\n", ret);
  38. ret = tsk_semaphore_decrement(semaphore);
  39. printf("threadfunc_semaphore2/// STOP %d\n", ret);
  40. return 0;
  41. }
  42. /* Pthread semaphore */
  43. void test_semaphore()
  44. {
  45. tsk_semaphore_handle_t *semaphore = tsk_semaphore_create();
  46. void* tid[2] = {0,0};
  47. printf("test_semaphore//\n");
  48. tsk_thread_create(&tid[0], threadfunc_semaphore1, semaphore);
  49. tsk_thread_create(&tid[1], threadfunc_semaphore2, semaphore);
  50. tsk_thread_sleep(1000);
  51. assert(!tsk_semaphore_increment(semaphore));
  52. assert(!tsk_semaphore_increment(semaphore));
  53. tsk_thread_sleep(1000);
  54. tsk_thread_join(&tid[0]);
  55. tsk_thread_join(&tid[1]);
  56. tsk_semaphore_destroy(&semaphore);
  57. }
  58. #endif /* _TEST_SEMAPHORE_H_ */