test_safeobject.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_SAFEOBJ_H_
  23. #define _TEST_SAFEOBJ_H_
  24. int so_mutex_count = 0;
  25. typedef struct mysafeobj_s {
  26. TSK_DECLARE_SAFEOBJ;
  27. int test1;
  28. int test2;
  29. }
  30. mysafeobj_t;
  31. void *threadfunc_safeobj1(void *parm)
  32. {
  33. mysafeobj_t *safeobj = (mysafeobj_t *)parm;
  34. int ret = 0;
  35. so_mutex_count++;
  36. ret = tsk_safeobj_lock(safeobj);
  37. printf("threadfunc_safeobj1/// %d\n", ret);
  38. return 0;
  39. }
  40. void *threadfunc_safeobj2(void *parm)
  41. {
  42. mysafeobj_t *safeobj = (mysafeobj_t *)parm;
  43. int ret = 0;
  44. so_mutex_count++;
  45. ret = tsk_safeobj_lock(safeobj);
  46. printf("threadfunc_safeobj2/// %d\n", ret);
  47. return 0;
  48. }
  49. /* test safeobject */
  50. void test_safeobject()
  51. {
  52. mysafeobj_t* obj = calloc(1, sizeof(mysafeobj_t));
  53. void* tid[2] = {0, 0};
  54. int i;
  55. printf("test_safeobject//\n");
  56. tsk_safeobj_init(obj);
  57. //assert(!tsk_safeobj_lock(obj));
  58. tsk_thread_create(&tid[0], threadfunc_safeobj1, obj);
  59. tsk_thread_create(&tid[1], threadfunc_safeobj2, obj);
  60. /* VERY BAD */
  61. while(so_mutex_count<2);
  62. for(i=0; i<10000000; i++);
  63. /*assert(!*/tsk_safeobj_unlock(obj)/*)*/;
  64. /*assert(!*/tsk_safeobj_unlock(obj)/*)*/;
  65. tsk_thread_join(&tid[0]);
  66. tsk_thread_join(&tid[1]);
  67. tsk_safeobj_deinit(obj);
  68. tsk_free((void**)&obj);
  69. }
  70. #endif /* _TEST_SAFEOBJ_H_ */