locking.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @file locking.h
  3. * @brief this file is the header file for functions that touch all locks allocated for software registers.
  4. * @details for more details about implementation choice, please read the file lock.h
  5. */
  6. #ifndef _PCILIB_LOCKING_H
  7. #define _PCILIB_LOCKING_H
  8. #define PCILIB_MAX_LOCKS 64 /**< number of maximum locks*/
  9. #define PCILIB_LOCKS_PER_PAGE (PCILIB_KMEM_PAGE_SIZE/PCILIB_LOCK_SIZE) /**< number of locks per page of kernel memory */
  10. #define PCILIB_LOCK_PAGES ((PCILIB_MAX_LOCKS * PCILIB_LOCK_SIZE)/PCILIB_KMEM_PAGE_SIZE) /**< number of pages allocated for locks in kernel memory */
  11. #include <pcilib/kmem.h>
  12. #include <pcilib/lock.h>
  13. typedef uint32_t pcilib_lock_id_t; /**< type to represent the index of a lock in the table of locks in the kernel space*/
  14. typedef struct pcilib_locking_s pcilib_locking_t;
  15. /**
  16. * structure defining the kernel space used for locks
  17. */
  18. struct pcilib_locking_s {
  19. pcilib_kmem_handle_t *kmem; /**< kmem used to store mutexes */
  20. pcilib_lock_t *locking; /**< lock used while intializing kernel space */
  21. // pcilib_lock_t *mmap; /**< lock used to protect mmap operation */
  22. };
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /**
  27. *this function gets the kernel space for the locks : if this space have been already initialized, then the previous space is returned. If not, the space is created. this function has to be protected, in order to avoid the simultaneous creation of 2 kernel spaces. For that, we use pcilib_lock_global.
  28. *@param[in,out] ctx - the pcilib_t structure running, getting filled with a ref to locks' kernel space
  29. */
  30. int pcilib_init_locking(pcilib_t *ctx);
  31. /**
  32. *this function cleans the memory from all locks : the kernel space is freed, and locks references in pcilib_t are destroyed by setting memory to 0
  33. *@param[in,out] ctx - the pcilib_t structure running
  34. */
  35. void pcilib_free_locking(pcilib_t *ctx);
  36. /**
  37. * this function use flock locking mechanism on the ALPS platform device file, to make sure to not create two kernel spaces for locks
  38. *@param[in,out] ctx - the pcilib_t structure running
  39. */
  40. int pcilib_lock_global(pcilib_t *ctx);
  41. /**
  42. *function to remove the lock created by flock on the ALPS platform device file
  43. *@param[in,out] ctx - the pcilib_t structure running
  44. */
  45. void pcilib_unlock_global(pcilib_t *ctx);
  46. /**
  47. * this function returns the lock at the index in the kernel space equal to id
  48. *@param[in] ctx - the pcilib_t structure running
  49. *@param[in] id - the index of the lock
  50. *@return the lock structure corresponding
  51. */
  52. pcilib_lock_t *pcilib_get_lock_by_id(pcilib_t *ctx, pcilib_lock_id_t id);
  53. /**
  54. *this function verify if the lock requested exists in the kernel space. If yes, then nothing is done, else we create the lock in the kernel space. This function also gives the number of processes that may request the lock afterwards, including the one that just created it.
  55. *@param[in] ctx - the pcilib_t structure running
  56. *@param[in] flags - the flag defining the property of the lock
  57. *@param[in] lock_id - the identifier name for the lock
  58. *@return the corresponding lock, or a new one if it did not exist before
  59. */
  60. pcilib_lock_t *pcilib_get_lock(pcilib_t *ctx, pcilib_lock_flags_t flags, const char *lock_id, ...);
  61. /**
  62. *this function is to decrement the variable in a lock containing the number of processes that may access to this lock(refs)
  63. *@param[in] ctx - the pcilib_t structure running
  64. *@param[in] flags - the flag defining the property of the lock
  65. *@param[in] lock - pointer to the lock we want to modify
  66. */
  67. void pcilib_return_lock(pcilib_t *ctx, pcilib_lock_flags_t flags, pcilib_lock_t *lock);
  68. /**
  69. * this function destroy all the locks that have been created(unref the locks + set memory to 0), and so is used when we want to clean properly the kernel space. If force is set to 1, then we don't care about other processes that may request locks. If not, if there is locks that may be requested by other processes, then the operation is stopped. Of course, destroying locks that may be requested by other processes results in an undefined behaviour. Thus, it is user responsibility to issue this command with force set to 1
  70. *@param[in,out] ctx - the pcilib_t structure running
  71. *@param[in] force - should the operation be forced or not
  72. * @return error code : 0 if everything was ok
  73. */
  74. int pcilib_destroy_all_locks(pcilib_t *ctx, int force);
  75. #ifdef __cplusplus
  76. }
  77. #endif
  78. #endif /* _PCILIB_LOCKING_H */