lock.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @file lock.h
  3. * @brief this file is the header file for the functions that implement a semaphore API for the pcitool program, using pthread robust mutexes.
  4. * @details the use of pthread robust mutexes was chosen due to the fact we privilege security over fastness, and that pthread mutexes permits to recover semaphores even with crash ,and that it does not require access to resources that can be easily accessible from extern usage as flock file locking mechanism. A possible other locking mechanism could be the sysv semaphores, but we have a problem of how determine a perfect hash for the init function, and more, benchmarks proves that sysv semaphore aren't that stable. For pure locking/unlocking, pthread is better in performance than sysV, but it suffers from big initialization times. In this sense, a kernel memory space is used for saving the locks, and persistence permits to avoid initializations over uses.
  5. *
  6. * We considered that mutex implmentation is enough compared to a reader/writer implementation. If it should change, please go to sysv semaphore.
  7. *
  8. * Basic explanation on how semaphores here work: a semaphore here is a positive integer, thus that can't go below zero, which is initiated with a value. when a process want access to the critical resource, it asks to decrement the value of the semaphore, and when it has finished, it reincrements it.basically, when the semaphore is equal to zero, any process must have to wait for it to be reincremented before decrementing it again. Here are defined two types of access to the semaphore corresponding to the reader/writer problem : an exclusive lock, which means that no other process than the one who have the resource can access it; a shared lock, which means that other processes who want to access to the resource with a shared lock can have the access, but a concurrent process who want to access the semaphore with an exclusive lock won't be able to.
  9. * explanation on locks here : here locks are registered in kernel memory, where they are defined by a pthread_mutex_t and an identifier name, which corresponds most of the time to a mix of the register associated name and processus (but it's up to the user). The iterations like searching a lock are done on this id name.
  10. */
  11. #ifndef _PCILIB_LOCK_H
  12. #define _PCILIB_LOCK_H
  13. #define PCILIB_LOCK_SIZE 128 /**< size of one lock. indeed, as we can't allocate easily on the fly memory in the kernel, fixed size have been chosen. determines so the size of the identifier name in the way locks are registered. 40 bytes are necessary for the mutex structure, so we have an id name of length LOCK_SIZE-40*/
  14. #include <pcilib.h>
  15. /**
  16. * type that defines possible flags for a lock, defining how a lock should be handled by the locking functions
  17. */
  18. typedef enum {
  19. PCILIB_LOCK_FLAGS_DEFAULT = 0, /**< Default flags */
  20. PCILIB_LOCK_FLAG_UNLOCKED = 1, /**< Perform operations unlocked, thus without taking care of the lock (protected by global flock during initialization of locking subsystem) */
  21. PCILIB_LOCK_FLAG_PERSISTENT = 2 /**< Do not create robust mutexes, but preserve the lock across application launches */
  22. } pcilib_lock_flags_t;
  23. /** structure defining a lock*/
  24. typedef struct pcilib_lock_s pcilib_lock_t;
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /**
  29. *this function initializes a lock, by setting correctly its property given the flags associated.
  30. * @param[in,out] lock - pointer to lock to initialize
  31. * @param[in] flags - flags: if it's set to two, then not a robust mutex is created
  32. * @param[in] lock_id - lock identificator
  33. * @return error code or 0 on success
  34. */
  35. int pcilib_init_lock(pcilib_lock_t *lock, pcilib_lock_flags_t flags, const char *lock_id);
  36. /**
  37. * this function will unref the defined lock. Any subsequent tries to lock it without reinitializaing it will fail.
  38. * @param[in,out] lock_ctx - the pointer that points to the lock.
  39. */
  40. void pcilib_free_lock(pcilib_lock_t *lock_ctx);
  41. /**
  42. * this function gives the identifier name associated to a lock in the kernel space
  43. * @param[in] lock - pointer to the lock we want the name
  44. * @return string corresponding to the name
  45. */
  46. const char *pcilib_lock_get_name(pcilib_lock_t *lock);
  47. /**
  48. * Increment reference count(number of processes that may access the given lock).
  49. * Not thread/process safe unless system supports stdatomic (gcc 4.9+). In this case, the access should be synchronized by the caller.
  50. * @param[in,out] lock - pointer to initialized lock
  51. */
  52. void pcilib_lock_ref(pcilib_lock_t *lock);
  53. /**
  54. * Decrement reference count (number of processes that may access the given lock)
  55. * Not thread/process safe unless system supports stdatomic (gcc 4.9+). In this case, the access should be synchronized by the caller
  56. * @param[in,out] lock - pointer to initialized lock
  57. */
  58. void pcilib_lock_unref(pcilib_lock_t *lock);
  59. /**
  60. * Return _approximate_ number of lock references as the crashed applications will may not unref.
  61. * @param[in,out] lock - pointer to initialized lock
  62. * @return the number of lock refs
  63. */
  64. size_t pcilib_lock_get_refs(pcilib_lock_t *lock);
  65. /**
  66. * gets the flags associated to the given lock
  67. * @param[in] lock - the lock we want to know the flags
  68. * @return value of the flag associated
  69. */
  70. pcilib_lock_flags_t pcilib_lock_get_flags(pcilib_lock_t *lock);
  71. /**
  72. * this function will call different locking functions to acquire the given lock. Given the flags, it is thus possible to:
  73. * 1) the process requesting the lock will be held till it can acquire it
  74. * 2)the lock is tried to be acquired, if the lock can be acquired then it is, if not, then the function returns immediatly and the lock is not taken at all
  75. * 3) same than previous, but it's possible to define a waiting time to acquire the lock before returning
  76. *
  77. * @param[in] lock - the pointer to the mutex
  78. * @param[in] flags - define the type of lock wanted
  79. * @param[in] timeout - the waiting time if asked, before the function returns without having obtained the lock, in micro seconds
  80. * @return error code or 0 for correctness
  81. */
  82. int pcilib_lock_custom(pcilib_lock_t* lock, pcilib_lock_flags_t flags, pcilib_timeout_t timeout);
  83. /**
  84. * function to acquire a lock, and wait till the lock can be acquire
  85. * @param[in] lock - the pointer to the mutex
  86. * @return error code or 0 for correctness
  87. */
  88. int pcilib_lock(pcilib_lock_t* lock);
  89. /**
  90. * this function will try to take a lock for the mutex pointed by lockfunction to acquire a lock, but that returns immediatly if the lock can't be acquired on first try
  91. * @param[in] lock - the pointer to the mutex
  92. * @return error code or 0 for correctness
  93. */
  94. int pcilib_try_lock(pcilib_lock_t* lock);
  95. /**
  96. * this function unlocks the lock pointed by lock
  97. * @param[in] lock - the integer that points to the semaphore
  98. */
  99. void pcilib_unlock(pcilib_lock_t* lock);
  100. #ifdef __cplusplus
  101. }
  102. #endif
  103. #endif /* _PCILIB_LOCK_H */