freeRTOS操作系统机器人实现
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
5.1 KiB

3 days ago
/******************************************************************************
(C), 2018-2099, Radkil
******************************************************************************
: rd_thread.h
: 稿
: Radkil
: 202635
:
: rd_thread.c
:
1. : 202635
: Radkil
:
******************************************************************************/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
#ifndef __RD_THREAD_H__
#define __RD_THREAD_H__
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
/*==============================================*
* include header files *
*----------------------------------------------*/
#include "common.h"
#include <pthread.h>
/*==============================================*
* constants or macros define *
*----------------------------------------------*/
#ifdef BUILD_PLATFORM_LINUX
typedef pthread_mutex_t rd_mutex_t;
#define RD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
#else
typedef struct
{
uint8_t dummy;// 占位,无特殊意义
} rd_mutex_t;
#define RD_MUTEX_INITIALIZER {0}
#endif
typedef enum {
AUTO_MODE_ANDROID = 0, // mmap + file (Android 推荐)
AUTO_MODE_POSIX = 1, // shm_open (Linux 标准)
AUTO_MODE_SYSV = 2 // shmget (老式 Linux)
} rd_shm_mode_t;
#ifdef __ANDROID__
#define RD_SHM_DEFAULT_MODE AUTO_MODE_ANDROID
#else
#define RD_SHM_DEFAULT_MODE AUTO_MODE_POSIX
#endif
typedef struct {
pthread_mutex_t mutex;
pthread_cond_t cond;
int count;
} rd_sem_t;
#if RD_SHM_DEFAULT_MODE == AUTO_MODE_POSIX || RD_SHM_DEFAULT_MODE == AUTO_MODE_ANDROID
typedef rd_sem_t Rd_sem_t;
#elif RD_SHM_DEFAULT_MODE == AUTO_MODE_SYSV
#include <semaphore.h>
typedef sem_t Rd_sem_t;
#endif
#define SYSV_SHM_ID 'R' // System V共享内存默认ID
#define IF_NOT_KEY "/dev/null" //找不到key文件时使用的文件名
typedef struct {
void *addr; // 映射后的内存地址
int size; // 内存大小
int fd; // Android/POSIX: 文件描述符
int shmid; // System V: 共享内存 ID
char path[256]; // Android: 文件路径 (用于 unlink)
char name[64]; // POSIX/System V: 名称或 Key 标识
rd_shm_mode_t mode; // 当前使用的模式
int is_creator; // 是否是创建者
} rd_shm_t;
/*==============================================*
* project-wide global variables *
*----------------------------------------------*/
/*==============================================*
* routines' or functions' implementations *
*----------------------------------------------*/
int Rd_MutexInit(void *_pMutex);
void Rd_MutexLock(void *_pMutex, const char *_pcFunc, int *_piCnt);
void Rd_MutexUnlock(void *_pMutex, const char *_pcFunc, int *_piCnt);
int Rd_MutexDestroy(void *_pMutex);
int Rd_SemInit(void *_sem, int _iPshared, int _iValue);
void Rd_SemPost(void *_sem, const char *_pcFunc, int *_piCnt);
void Rd_SemWait(void *_sem, const char *_pcFunc, int *_piCnt);
int Rd_SemDestroy(void *_sem);
/**
* @brief
* @param shm
* @param name (POSIX/ShmOpen , SysV key , Android )
* @param size
* @param is_creator 1=, 0=
* @param mode (0=Auto, 1=POSIX, 2=SYSV). 0 .
* @return 0 , -1
*/
int Rd_ShmOpen(rd_shm_t *_ptShm, const char *_psName, int _iSize, int _iIsCreator, rd_shm_mode_t _eMode);
/**
* @brief (unmap)
*/
int Rd_ShmClose(rd_shm_t *_ptShm);
/**
* @brief (unlink rmid)
* @note 退
*/
int Rd_ShmUnlink(rd_shm_t *_ptShm);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* __RD_THREAD_H__ */