fsemaphore.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fsemaphore.h
  15. * Date: 2022-02-10 14:53:42
  16. * LastEditTime: 2022-02-18 08:25:35
  17. * Description:  This files is for semaphore user api definition
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 zhugengyu 2022/5/23 init commit
  23. */
  24. #ifndef DRIVERS_IPC_FSEMAPHORE_H
  25. #define DRIVERS_IPC_FSEMAPHORE_H
  26. #ifdef __cplusplus
  27. extern "C"
  28. {
  29. #endif
  30. /***************************** Include Files *********************************/
  31. #include "ftypes.h"
  32. #include "ferror_code.h"
  33. /************************** Constant Definitions *****************************/
  34. #define FSEMA_NUM_OF_LOCKER 32U
  35. #define FSEMA_OWNER_NONE 0U
  36. #define FSEMA_SUCCESS FT_SUCCESS
  37. #define FSEMA_ERR_NOT_INIT FT_MAKE_ERRCODE(ErrModBsp, ErrSema, 0U)
  38. #define FSEMA_ERR_NO_AVAILABLE_LOCKER FT_MAKE_ERRCODE(ErrModBsp, ErrSema, 1U)
  39. #define FSEMA_ERR_LOCK_TIMEOUT FT_MAKE_ERRCODE(ErrModBsp, ErrSema, 2U)
  40. #define FSEMA_ERR_NO_PERMISSION FT_MAKE_ERRCODE(ErrModBsp, ErrSema, 3U)
  41. /**************************** Type Definitions *******************************/
  42. typedef struct
  43. {
  44. u32 id; /* Semaphore控制器id */
  45. uintptr base_addr; /* Semaphore控制器基地址 */
  46. } FSemaConfig; /* Semaphore控制器配置 */
  47. typedef struct _FSema FSema;
  48. typedef struct
  49. {
  50. u32 index; /* Semaphore锁id */
  51. #define FSEMA_LOCKER_NAME_LEN 32U
  52. char name[FSEMA_LOCKER_NAME_LEN]; /* Semaphore锁的名字 */
  53. u32 owner; /* Semaphore锁的拥有者, 当前持有锁的人, 如果没有上锁就标记FSEMA_OWNER_NONE */
  54. FSema *sema; /* Semaphore控制器实例 */
  55. } FSemaLocker; /* Semaphore锁实例 */
  56. typedef struct _FSema
  57. {
  58. FSemaConfig config; /* Semaphore控制器配置 */
  59. u32 is_ready; /* Semaphore控制器初始化是否完成 */
  60. FSemaLocker *locker[FSEMA_NUM_OF_LOCKER]; /* Semaphore锁实例,locker[i] == NULL 表示锁尚未分配 */
  61. } FSema; /* Semaphore控制器实例 */
  62. typedef void (*FSemaRelaxHandler)(FSema *const instance); /* 等待下一次上锁的relax函数 */
  63. /************************** Variable Definitions *****************************/
  64. /***************** Macros (Inline Functions) Definitions *********************/
  65. /************************** Function Prototypes ******************************/
  66. /* 获取Semaphore的默认配置 */
  67. const FSemaConfig *FSemaLoopkupConfig(u32 instance_id);
  68. /* 初始化Semaphore控制器 */
  69. FError FSemaCfgInitialize(FSema *const instance, const FSemaConfig *config);
  70. /* 去初始化Semaphore控制器 */
  71. void FSemaDeInitialize(FSema *const instance);
  72. /* 分配和创建Semaphore锁 */
  73. FError FSemaCreateLocker(FSema *const instance, FSemaLocker *const locker);
  74. /* 强制解除Semaphore锁并删除锁实例 */
  75. FError FSemaDeleteLocker(FSemaLocker *const locker);
  76. /* 尝试获取指定Semaphore锁 */
  77. FError FSemaTryLock(FSemaLocker *const locker, u32 owner, u32 try_times, FSemaRelaxHandler relax_handler);
  78. /* 尝试释放指定Semaphore锁 */
  79. FError FSemaUnlock(FSemaLocker *const locker, u32 owner);
  80. /* 强制解除所有Semaphore锁 */
  81. FError FSemaUnlockAll(FSema *const instance);
  82. /* 检查指定Semaphore锁是否处于锁定状态 */
  83. boolean FSemaIsLocked(FSemaLocker *locker);
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. #endif