userspace/wqueue: move exclusive access lock to mqueue inside
Change-Id: I885d5641bc81fedf698c241d4719cb3561700f17 Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
This commit is contained in:
parent
855c78bb9d
commit
00854f0f94
@ -23,7 +23,6 @@ ifeq ($(CONFIG_LIB_USRWORK),y)
|
||||
# Add the work queue C files to the build
|
||||
|
||||
CSRCS += work_usrthread.c work_queue.c work_cancel.c work_signal.c
|
||||
CSRCS += work_lock.c
|
||||
|
||||
# Add the wqueue directory to the build
|
||||
|
||||
|
@ -69,7 +69,7 @@ static int work_qcancel(FAR struct usr_wqueue_s *wqueue,
|
||||
|
||||
/* Get exclusive access to the work queue */
|
||||
|
||||
while (work_lock() < 0);
|
||||
while (_SEM_WAIT(&wqueue->lock) < 0);
|
||||
|
||||
/* Cancelling the work is simply a matter of removing the work structure
|
||||
* from the work queue. This must be done with interrupts disabled because
|
||||
@ -94,7 +94,7 @@ static int work_qcancel(FAR struct usr_wqueue_s *wqueue,
|
||||
ret = OK;
|
||||
}
|
||||
|
||||
work_unlock();
|
||||
_SEM_POST(&wqueue->lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -1,105 +0,0 @@
|
||||
/****************************************************************************
|
||||
* libs/libc/wqueue/work_lock.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <pthread.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/semaphore.h>
|
||||
|
||||
#include "wqueue/wqueue.h"
|
||||
|
||||
#if defined(CONFIG_LIB_USRWORK) && !defined(__KERNEL__)
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: work_lock
|
||||
*
|
||||
* Description:
|
||||
* Lock the user-mode work queue.
|
||||
*
|
||||
* Input Parameters:
|
||||
* None
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) on success, a negated errno on failure. This error may be
|
||||
* reported:
|
||||
*
|
||||
* -EINTR - Wait was interrupted by a signal
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int work_lock(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
#ifdef CONFIG_BUILD_PROTECTED
|
||||
ret = _SEM_WAIT(&g_usrsem);
|
||||
if (ret < 0)
|
||||
{
|
||||
DEBUGASSERT(_SEM_ERRNO(ret) == EINTR ||
|
||||
_SEM_ERRNO(ret) == ECANCELED);
|
||||
return -EINTR;
|
||||
}
|
||||
#else
|
||||
ret = pthread_mutex_lock(&g_usrmutex);
|
||||
if (ret != 0)
|
||||
{
|
||||
DEBUGASSERT(ret == EINTR);
|
||||
return -EINTR;
|
||||
}
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: work_unlock
|
||||
*
|
||||
* Description:
|
||||
* Unlock the user-mode work queue.
|
||||
*
|
||||
* Input Parameters:
|
||||
* None
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void work_unlock(void)
|
||||
{
|
||||
#ifdef CONFIG_BUILD_PROTECTED
|
||||
_SEM_POST(&g_usrsem);
|
||||
#else
|
||||
pthread_mutex_unlock(&g_usrmutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* CONFIG_LIB_USRWORK && !__KERNEL__*/
|
@ -78,7 +78,7 @@ static int work_qqueue(FAR struct usr_wqueue_s *wqueue,
|
||||
|
||||
/* Get exclusive access to the work queue */
|
||||
|
||||
while (work_lock() < 0);
|
||||
while (_SEM_WAIT(&wqueue->lock) < 0);
|
||||
|
||||
/* Is there already pending work? */
|
||||
|
||||
@ -104,7 +104,7 @@ static int work_qqueue(FAR struct usr_wqueue_s *wqueue,
|
||||
dq_addlast((FAR dq_entry_t *)work, &wqueue->q);
|
||||
kill(wqueue->pid, SIGWORK); /* Wake up the worker thread */
|
||||
|
||||
work_unlock();
|
||||
_SEM_POST(&wqueue->lock);
|
||||
return OK;
|
||||
}
|
||||
|
||||
|
@ -77,14 +77,6 @@
|
||||
|
||||
struct usr_wqueue_s g_usrwork;
|
||||
|
||||
/* This semaphore supports exclusive access to the user-mode work queue */
|
||||
|
||||
#ifdef CONFIG_BUILD_PROTECTED
|
||||
sem_t g_usrsem;
|
||||
#else
|
||||
pthread_mutex_t g_usrmutex;
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
@ -125,7 +117,7 @@ void work_process(FAR struct usr_wqueue_s *wqueue)
|
||||
*/
|
||||
|
||||
next = WORK_DELAY_MAX;
|
||||
ret = work_lock();
|
||||
ret = _SEM_WAIT(&wqueue->lock);
|
||||
if (ret < 0)
|
||||
{
|
||||
/* Break out earlier if we were awakened by a signal */
|
||||
@ -188,7 +180,7 @@ void work_process(FAR struct usr_wqueue_s *wqueue)
|
||||
* performed... we don't have any idea how long this will take!
|
||||
*/
|
||||
|
||||
work_unlock();
|
||||
_SEM_POST(&wqueue->lock);
|
||||
worker(arg);
|
||||
|
||||
/* Now, unfortunately, since we unlocked the work queue we
|
||||
@ -196,7 +188,7 @@ void work_process(FAR struct usr_wqueue_s *wqueue)
|
||||
* start back at the head of the list.
|
||||
*/
|
||||
|
||||
ret = work_lock();
|
||||
ret = _SEM_WAIT(&wqueue->lock);
|
||||
if (ret < 0)
|
||||
{
|
||||
/* Break out earlier if we were awakened by a signal */
|
||||
@ -255,7 +247,7 @@ void work_process(FAR struct usr_wqueue_s *wqueue)
|
||||
*/
|
||||
|
||||
sigprocmask(SIG_BLOCK, &sigset, &oldset);
|
||||
work_unlock();
|
||||
_SEM_POST(&wqueue->lock);
|
||||
|
||||
if (next == WORK_DELAY_MAX)
|
||||
{
|
||||
@ -347,10 +339,11 @@ static pthread_addr_t work_usrthread(pthread_addr_t arg)
|
||||
|
||||
int work_usrstart(void)
|
||||
{
|
||||
#ifdef CONFIG_BUILD_PROTECTED
|
||||
/* Set up the work queue lock */
|
||||
|
||||
_SEM_INIT(&g_usrsem, 0, 1);
|
||||
_SEM_INIT(&g_usrwork.lock, 0, 1);
|
||||
|
||||
#ifdef CONFIG_BUILD_PROTECTED
|
||||
|
||||
/* Start a user-mode worker thread for use by applications. */
|
||||
|
||||
@ -375,10 +368,6 @@ int work_usrstart(void)
|
||||
struct sched_param param;
|
||||
int ret;
|
||||
|
||||
/* Set up the work queue lock */
|
||||
|
||||
pthread_mutex_init(&g_usrmutex, NULL);
|
||||
|
||||
/* Start a user-mode worker thread for use by applications. */
|
||||
|
||||
pthread_attr_init(&attr);
|
||||
|
@ -47,6 +47,7 @@
|
||||
struct usr_wqueue_s
|
||||
{
|
||||
struct dq_queue_s q; /* The queue of pending work */
|
||||
sem_t lock; /* exclusive access to user-mode work queue */
|
||||
pid_t pid; /* The task ID of the worker thread(s) */
|
||||
};
|
||||
|
||||
@ -58,54 +59,9 @@ struct usr_wqueue_s
|
||||
|
||||
extern struct usr_wqueue_s g_usrwork;
|
||||
|
||||
/* This semaphore/mutex supports exclusive access to the user-mode work
|
||||
* queue
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_BUILD_PROTECTED
|
||||
extern sem_t g_usrsem;
|
||||
#else
|
||||
extern pthread_mutex_t g_usrmutex;
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: work_lock
|
||||
*
|
||||
* Description:
|
||||
* Lock the user-mode work queue.
|
||||
*
|
||||
* Input Parameters:
|
||||
* None
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) on success, a negated errno on failure. This error may be
|
||||
* reported:
|
||||
*
|
||||
* -EINTR - Wait was interrupted by a signal
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int work_lock(void);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: work_unlock
|
||||
*
|
||||
* Description:
|
||||
* Unlock the user-mode work queue.
|
||||
*
|
||||
* Input Parameters:
|
||||
* None
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void work_unlock(void);
|
||||
|
||||
#endif /* CONFIG_LIB_USRWORK && !__KERNEL__*/
|
||||
#endif /* __LIBC_WQUEUE_WQUEUE_H */
|
||||
|
Loading…
x
Reference in New Issue
Block a user