Skip to content

Commit

Permalink
pthread: remove enter_critical_section in pthread_mutex
Browse files Browse the repository at this point in the history
reason:
We would like to replace the critical section with a small lock.

Signed-off-by: hujun5 <[email protected]>
  • Loading branch information
hujun260 authored and xiaoxiang781216 committed Jan 12, 2025
1 parent 19bca74 commit 5f4a15b
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 96 deletions.
5 changes: 5 additions & 0 deletions include/nuttx/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <nuttx/net/net.h>
#include <nuttx/mm/map.h>
#include <nuttx/tls.h>
#include <nuttx/spinlock_type.h>

#include <arch/arch.h>

Expand Down Expand Up @@ -737,6 +738,10 @@ struct tcb_s
size_t level_deepest;
size_t level;
#endif

#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
spinlock_t mutex_lock;
#endif
};

/* struct task_tcb_s ********************************************************/
Expand Down
4 changes: 4 additions & 0 deletions sched/init/nx_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,10 @@ static void idle_group_initialize(void)

nxtask_joininit(tcb);

#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
spin_lock_init(&tcb->mutex_lock);
#endif

#ifdef CONFIG_SMP
/* Create a stack for all CPU IDLE threads (except CPU0 which already
* has a stack).
Expand Down
3 changes: 1 addition & 2 deletions sched/pthread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ if(NOT CONFIG_DISABLE_PTHREAD)
pthread_setschedprio.c)

if(NOT CONFIG_PTHREAD_MUTEX_UNSAFE)
list(APPEND SRCS pthread_mutex.c pthread_mutexconsistent.c
pthread_mutexinconsistent.c)
list(APPEND SRCS pthread_mutex.c pthread_mutexconsistent.c)
endif()

if(CONFIG_SMP)
Expand Down
2 changes: 1 addition & 1 deletion sched/pthread/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ CSRCS += pthread_completejoin.c pthread_findjoininfo.c
CSRCS += pthread_release.c pthread_setschedprio.c

ifneq ($(CONFIG_PTHREAD_MUTEX_UNSAFE),y)
CSRCS += pthread_mutex.c pthread_mutexconsistent.c pthread_mutexinconsistent.c
CSRCS += pthread_mutex.c pthread_mutexconsistent.c
endif

ifeq ($(CONFIG_SMP),y)
Expand Down
4 changes: 4 additions & 0 deletions sched/pthread/pthread_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,

nxtask_joininit(&ptcb->cmn);

#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
spin_lock_init(&ptcb->cmn.mutex_lock);
#endif

/* Bind the parent's group to the new TCB (we have not yet joined the
* group).
*/
Expand Down
54 changes: 50 additions & 4 deletions sched/pthread/pthread_mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ static void pthread_mutex_add(FAR struct pthread_mutex_s *mutex)

/* Add the mutex to the list of mutexes held by this pthread */

flags = enter_critical_section();
flags = spin_lock_irqsave(&rtcb->mutex_lock);
mutex->flink = rtcb->mhead;
rtcb->mhead = mutex;
leave_critical_section(flags);
spin_unlock_irqrestore(&rtcb->mutex_lock, flags);
}

/****************************************************************************
Expand All @@ -92,7 +92,7 @@ static void pthread_mutex_remove(FAR struct pthread_mutex_s *mutex)
FAR struct pthread_mutex_s *prev;
irqstate_t flags;

flags = enter_critical_section();
flags = spin_lock_irqsave(&rtcb->mutex_lock);

/* Remove the mutex from the list of mutexes held by this task */

Expand All @@ -118,7 +118,7 @@ static void pthread_mutex_remove(FAR struct pthread_mutex_s *mutex)
}

mutex->flink = NULL;
leave_critical_section(flags);
spin_unlock_irqrestore(&rtcb->mutex_lock, flags);
}

/****************************************************************************
Expand Down Expand Up @@ -346,3 +346,49 @@ int pthread_mutex_restorelock(FAR struct pthread_mutex_s *mutex,

return ret;
}

/****************************************************************************
* Name: pthread_mutex_inconsistent
*
* Description:
* This function is called when a pthread is terminated via either
* pthread_exit() or pthread_cancel(). It will check for any mutexes
* held by exitting thread. It will mark them as inconsistent and
* then wake up the highest priority waiter for the mutex. That
* instance of pthread_mutex_lock() will then return EOWNERDEAD.
*
* Input Parameters:
* tcb -- a reference to the TCB of the exitting pthread.
*
* Returned Value:
* None.
*
****************************************************************************/

void pthread_mutex_inconsistent(FAR struct tcb_s *tcb)
{
FAR struct pthread_mutex_s *mutex;
irqstate_t flags;

DEBUGASSERT(tcb != NULL);

flags = spin_lock_irqsave(&tcb->mutex_lock);

/* Remove and process each mutex held by this task */

while (tcb->mhead != NULL)
{
/* Remove the mutex from the TCB list */

mutex = tcb->mhead;
tcb->mhead = mutex->flink;
mutex->flink = NULL;

/* Mark the mutex as INCONSISTENT and wake up any waiting thread */

mutex->flags |= _PTHREAD_MFLAGS_INCONSISTENT;
mutex_unlock(&mutex->mutex);
}

spin_unlock_irqrestore(&tcb->mutex_lock, flags);
}
89 changes: 0 additions & 89 deletions sched/pthread/pthread_mutexinconsistent.c

This file was deleted.

4 changes: 4 additions & 0 deletions sched/task/task_fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ FAR struct task_tcb_s *nxtask_setup_fork(start_t retaddr)

nxtask_joininit(&child->cmn);

#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
spin_lock_init(&child->cmn.mutex_lock);
#endif

/* Allocate a new task group with the same privileges as the parent */

ret = group_initialize(child, ttype);
Expand Down
4 changes: 4 additions & 0 deletions sched/task/task_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ int nxtask_init(FAR struct task_tcb_s *tcb, const char *name, int priority,
nxtask_joininit(&tcb->cmn);
#endif

#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
spin_lock_init(&tcb->cmn.mutex_lock);
#endif

/* Duplicate the parent tasks environment */

ret = env_dup(tcb->cmn.group, envp);
Expand Down

0 comments on commit 5f4a15b

Please sign in to comment.