2014-10-10 06:22:51 -06:00
|
|
|
/****************************************************************************
|
2021-03-08 18:39:04 -03:00
|
|
|
* libs/libc/wqueue/wqueue.h
|
2014-10-10 06:22:51 -06:00
|
|
|
*
|
2024-09-25 14:05:00 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* 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
|
2014-10-10 06:22:51 -06:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-10-10 06:22:51 -06:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* 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.
|
2014-10-10 06:22:51 -06:00
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2022-01-15 04:44:35 +02:00
|
|
|
#ifndef __LIBS_LIBC_WQUEUE_WQUEUE_H
|
|
|
|
|
#define __LIBS_LIBC_WQUEUE_WQUEUE_H
|
2014-10-10 08:35:58 -06:00
|
|
|
|
2014-10-10 06:22:51 -06:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Included Files
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <sys/types.h>
|
2014-10-10 14:52:04 -06:00
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
#include <nuttx/mutex.h>
|
|
|
|
|
#include <nuttx/semaphore.h>
|
2025-04-16 19:44:35 +08:00
|
|
|
#include <nuttx/list.h>
|
2014-10-10 14:52:04 -06:00
|
|
|
#include <nuttx/wqueue.h>
|
|
|
|
|
|
2021-08-01 15:27:08 +08:00
|
|
|
#if defined(CONFIG_LIBC_USRWORK) && !defined(__KERNEL__)
|
2014-10-10 06:22:51 -06:00
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Pre-processor Definitions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-10-10 08:35:58 -06:00
|
|
|
* Public Type Definitions
|
2014-10-10 06:22:51 -06:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
/* Forward reference */
|
|
|
|
|
|
|
|
|
|
struct usr_wqueue_s;
|
|
|
|
|
|
|
|
|
|
/* This structure describes one user-mode worker thread. */
|
|
|
|
|
|
|
|
|
|
struct usr_worker_s
|
|
|
|
|
{
|
|
|
|
|
pid_t tid; /* Worker thread ID */
|
|
|
|
|
FAR struct work_s *work; /* Work currently being processed */
|
|
|
|
|
FAR struct usr_wqueue_s *wqueue; /* Parent work queue */
|
|
|
|
|
sem_t wait; /* Wait for the current work */
|
|
|
|
|
uint16_t wait_count; /* Number of synchronous waiters */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* This structure defines the state of one user-mode work queue. */
|
2014-10-10 16:24:50 -06:00
|
|
|
|
|
|
|
|
struct usr_wqueue_s
|
|
|
|
|
{
|
2026-08-27 20:52:26 +08:00
|
|
|
struct list_node q; /* The queue of pending work */
|
|
|
|
|
mutex_t lock; /* Exclusive access to the queue */
|
|
|
|
|
sem_t wake; /* Wake-up semaphore */
|
|
|
|
|
FAR struct usr_worker_s *worker; /* Worker thread state array */
|
|
|
|
|
int nthreads; /* Number of worker threads */
|
|
|
|
|
bool exit; /* Request worker thread exit */
|
|
|
|
|
bool dynamic; /* Dynamically allocated queue */
|
2014-10-10 16:24:50 -06:00
|
|
|
};
|
|
|
|
|
|
2014-10-10 06:22:51 -06:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Public Data
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2014-10-10 08:35:58 -06:00
|
|
|
/* The state of the user mode work queue */
|
|
|
|
|
|
2014-10-10 16:24:50 -06:00
|
|
|
extern struct usr_wqueue_s g_usrwork;
|
2014-10-10 14:52:04 -06:00
|
|
|
|
2014-10-10 06:22:51 -06:00
|
|
|
/****************************************************************************
|
2026-08-27 20:52:26 +08:00
|
|
|
* Inline Functions
|
2014-10-10 06:22:51 -06:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: work_remove
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Remove work from a user-mode work queue. The caller must hold
|
|
|
|
|
* wqueue->lock, and work must be queued on wqueue.
|
|
|
|
|
*
|
|
|
|
|
* Returned Value:
|
|
|
|
|
* true if removing work changed the head of the queue; otherwise false.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static inline_function bool
|
|
|
|
|
work_remove(FAR struct usr_wqueue_s *wqueue,
|
|
|
|
|
FAR struct work_s *work)
|
|
|
|
|
{
|
|
|
|
|
FAR struct work_s *head;
|
|
|
|
|
|
|
|
|
|
head = list_first_entry(&wqueue->q, struct work_s, node);
|
|
|
|
|
|
|
|
|
|
work->worker = NULL;
|
|
|
|
|
list_delete(&work->node);
|
|
|
|
|
|
|
|
|
|
return head == work;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline_function void work_wake(FAR struct usr_wqueue_s *wqueue)
|
|
|
|
|
{
|
|
|
|
|
int semcount;
|
|
|
|
|
|
|
|
|
|
/* Keep enough wake tokens for the worker pool while bounding stale
|
|
|
|
|
* tokens when workers are already running.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
nxsem_get_value(&wqueue->wake, &semcount);
|
|
|
|
|
if (semcount < wqueue->nthreads)
|
|
|
|
|
{
|
|
|
|
|
nxsem_post(&wqueue->wake);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* CONFIG_LIBC_USRWORK && !__KERNEL__ */
|
2022-01-15 04:44:35 +02:00
|
|
|
#endif /* __LIBS_LIBC_WQUEUE_WQUEUE_H */
|