syncobj.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*------------------------------------------------------------------------*/
  2. /* Sample code of OS dependent synchronization object controls */
  3. /* for FatFs R0.07a (C)ChaN, 2009 */
  4. /*------------------------------------------------------------------------*/
  5. #include <windows.h> // Win32
  6. //#include <ucos_ii.h> // uC/OS-II
  7. #include "../ff.h"
  8. #if _FS_REENTRANT
  9. /*------------------------------------------------------------------------*/
  10. /* Create a Synchronization Object for a Volume
  11. /*------------------------------------------------------------------------*/
  12. /* This function is called in f_mount function to create a new
  13. / synchronization object, such as semaphore and mutex. When a FALSE is
  14. / returned, the f_mount function fails with FR_INT_ERR.
  15. */
  16. BOOL ff_cre_syncobj ( /* TRUE:Function succeeded, FALSE:Could not create due to any error */
  17. BYTE vol, /* Corresponding logical drive being processed */
  18. _SYNC_t *sobj /* Pointer to return the created sync object */
  19. )
  20. {
  21. BOOL ret;
  22. *sobj = CreateMutex(NULL, FALSE, NULL); // Win32
  23. ret = (*sobj != INVALID_HANDLE_VALUE) ? TRUE : FALSE; //
  24. // *sobj = VolumeSemId[vol]; // uITRON (give a static created sync object)
  25. // ret = TRUE; // The initial value of the semaphore must be 1.
  26. // *sobj = OSMutexCreate(0, &err); // uC/OS-II
  27. // ret = (err == OS_NO_ERR) ? TRUE : FALSE; //
  28. return ret;
  29. }
  30. /*------------------------------------------------------------------------*/
  31. /* Delete a Synchronization Object */
  32. /*------------------------------------------------------------------------*/
  33. /* This function is called in f_mount function to delete a synchronization
  34. / object that created with ff_cre_syncobj function. When a FALSE is
  35. / returned, the f_mount function fails with FR_INT_ERR.
  36. */
  37. BOOL ff_del_syncobj ( /* TRUE:Function succeeded, FALSE:Could not delete due to any error */
  38. _SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
  39. )
  40. {
  41. BOOL ret;
  42. ret = CloseHandle(sobj); // Win32
  43. // ret = TRUE; // uITRON (nothing to do)
  44. // OSMutexDel(sobj, OS_DEL_ALWAYS, &err); // uC/OS-II
  45. // ret = (err == OS_NO_ERR) ? TRUE : FALSE; //
  46. return ret;
  47. }
  48. /*------------------------------------------------------------------------*/
  49. /* Request Grant to Access the Volume */
  50. /*------------------------------------------------------------------------*/
  51. /* This function is called on entering file functions to lock the volume.
  52. / When a FALSE is returned, the file function fails with FR_TIMEOUT.
  53. */
  54. BOOL ff_req_grant ( /* TRUE:Got a grant to access the volume, FALSE:Could not get a grant */
  55. _SYNC_t sobj /* Sync object to wait */
  56. )
  57. {
  58. BOOL ret;
  59. ret = (WaitForSingleObject(sobj, _TIMEOUT) == WAIT_OBJECT_0) ? TRUE : FALSE; // Win32
  60. // ret = (wai_sem(sobj) == E_OK) ? TRUE : FALSE; // uITRON
  61. // OSMutexPend(sobj, _TIMEOUT, &err)); // uC/OS-II
  62. // ret = (err == OS_NO_ERR) ? TRUE : FALSE; //
  63. return ret;
  64. }
  65. /*------------------------------------------------------------------------*/
  66. /* Release Grant to Access the Volume */
  67. /*------------------------------------------------------------------------*/
  68. /* This function is called on leaving file functions to unlock the volume.
  69. */
  70. void ff_rel_grant (
  71. _SYNC_t sobj /* Sync object to be signaled */
  72. )
  73. {
  74. ReleaseMutex(sobj); // Win32
  75. // sig_sem(sobj); // uITRON
  76. // OSMutexPost(sobj); // uC/OS-II
  77. }
  78. #else
  79. #error This file is not needed in this configuration.
  80. #endif