Skip to content

Commit

Permalink
implement arm_irq_safe for PC platform
Browse files Browse the repository at this point in the history
  • Loading branch information
GorgonMeducer committed Nov 13, 2024
1 parent a8bd03a commit 3fb9115
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
64 changes: 64 additions & 0 deletions examples/[template][pc][vscode]/platform/Virtual_TFT_Port.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,70 @@ int quit_filter(void * userdata, SDL_Event * event)
return 1;
}

typedef struct RecursiveMutex {
SDL_mutex *mutex;
SDL_threadID owner;
int lock_count;
} RecursiveMutex;

RecursiveMutex* RecursiveMutex_Create(void) {
RecursiveMutex* rmutex = (RecursiveMutex*)malloc(sizeof(RecursiveMutex));
rmutex->mutex = SDL_CreateMutex();
rmutex->owner = 0;
rmutex->lock_count = 0;
return rmutex;
}

void RecursiveMutex_Lock(RecursiveMutex* rmutex) {
assert(NULL != rmutex);

SDL_threadID tid = SDL_ThreadID();
if (rmutex->owner == tid) {
// 如果是同一线程,增加锁计数
rmutex->lock_count++;
} else {
// 不同线程则锁定
SDL_LockMutex(rmutex->mutex);
rmutex->owner = tid;
rmutex->lock_count = 1;
}
}

void RecursiveMutex_Unlock(RecursiveMutex* rmutex) {
assert(NULL != rmutex);
if (rmutex->owner == SDL_ThreadID()) {
rmutex->lock_count--;
if (rmutex->lock_count == 0) {
rmutex->owner = 0;
SDL_UnlockMutex(rmutex->mutex);
}
}
}

void RecursiveMutex_Destroy(RecursiveMutex* rmutex) {
assert(NULL != rmutex);
SDL_DestroyMutex(rmutex->mutex);
free(rmutex);
}

static
RecursiveMutex *s_ptGlobalMutex = NULL;

void VT_enter_global_mutex(void)
{
RecursiveMutex_Lock(s_ptGlobalMutex);
}

void VT_leave_global_mutex(void)
{
RecursiveMutex_Unlock(s_ptGlobalMutex);
}


static void monitor_sdl_clean_up(void)
{
RecursiveMutex_Destroy(s_ptGlobalMutex);

SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
Expand All @@ -98,6 +160,8 @@ static void monitor_sdl_init(void)
/*Initialize the SDL*/
SDL_Init(SDL_INIT_VIDEO);

s_ptGlobalMutex = RecursiveMutex_Create();

SDL_SetEventFilter(quit_filter, NULL);

window = SDL_CreateWindow( "Arm-2D v"
Expand Down
2 changes: 2 additions & 0 deletions examples/[template][pc][vscode]/platform/Virtual_TFT_Port.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ extern bool VT_is_request_quit(void);
extern void VT_deinit(void);
extern void VT_sdl_flush(int32_t nMS);
extern void VT_sdl_refresh_task(void);
extern void VT_enter_global_mutex(void);
extern void VT_leave_global_mutex(void);

/*******************************************************************************
* @name :VT_Mouse_Get_Point
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@

#undef arm_irq_safe
#undef arm_exit_irq_safe
#define arm_irq_safe arm_using( uint32_t ARM_2D_SAFE_NAME(temp) = 0 )
#define arm_irq_safe arm_using( uint32_t ARM_2D_SAFE_NAME(temp) = 0, \
{ ARM_2D_UNUSED(ARM_2D_SAFE_NAME(temp)); \
VT_enter_global_mutex();}, \
{ VT_leave_global_mutex();} )
#define arm_exit_irq_safe continue


Expand All @@ -77,6 +80,9 @@ __STATIC_FORCEINLINE uint32_t __REV16(uint32_t wValue)
return (wHigh >> 8) | (wLow << 8);
}

extern void VT_enter_global_mutex(void);
extern void VT_leave_global_mutex(void);


#endif /* end of __ARM_2D_USER_ARCH_PORT_H__ */

0 comments on commit 3fb9115

Please sign in to comment.