test_mutex.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_MUTEX_H_
  23. #define _TEST_MUTEX_H_
  24. int mutex_thread_started = 0;
  25. int safe_func(tsk_mutex_handle_t *mutex, const char* caller)
  26. {
  27. int ret = 0;
  28. ret = tsk_mutex_lock(mutex);
  29. TSK_DEBUG_INFO("threadfunc_mutex/// Start doing job [%s]... %d\n", caller, ret);
  30. tsk_thread_sleep(5000);
  31. TSK_DEBUG_INFO("threadfunc_mutex/// Stop doing job [%s]... %d\n", caller, ret);
  32. ret = tsk_mutex_unlock(mutex);
  33. return ret;
  34. }
  35. void *threadfunc_mutex(void *parm)
  36. {
  37. tsk_mutex_handle_t *mutex = (tsk_mutex_handle_t *)parm;
  38. safe_func(mutex, "caller1");
  39. return 0;
  40. }
  41. /* Pthread mutex */
  42. void test_mutex()
  43. {
  44. tsk_mutex_handle_t *mutex = tsk_mutex_create();
  45. void* tid[1] = {0};
  46. TSK_DEBUG_INFO("test_mutex//\n");
  47. //tsk_mutex_lock(mutex);
  48. tsk_thread_create(&tid[0], threadfunc_mutex, mutex);
  49. tsk_thread_sleep(1000);
  50. safe_func(mutex, "caller0");
  51. /*while(!mutex_thread_started) tsk_thread_sleep(1000);
  52. printf("test_mutex// threadfunc_mutex is locked for 1 second\n");
  53. tsk_thread_sleep(1000);
  54. printf("test_mutex// Now unlock threadfunc_mutex\n");
  55. tsk_mutex_unlock(mutex);*/
  56. tsk_thread_join(&(tid[0]));
  57. tsk_mutex_destroy(&mutex);
  58. }
  59. #endif /* _TEST_MUTEX_H_ */