2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* sched/mqueue/mq_notify.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-10 16:26:30 +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
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-10 16:26:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-10 16:26:30 +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.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2009-12-14 19:39:29 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#include <mqueue.h>
|
|
|
|
#include <sched.h>
|
2015-12-30 20:20:31 +01:00
|
|
|
#include <string.h>
|
2011-12-04 19:08:33 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2014-09-29 23:33:34 +02:00
|
|
|
#include <nuttx/sched.h>
|
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-08 20:31:23 +02:00
|
|
|
#include "mqueue/mqueue.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: mq_notify
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* If "notification" is not NULL, this function connects the task with
|
|
|
|
* the message queue such that the specified signal will be sent to the
|
|
|
|
* task whenever the message changes from empty to non-empty. Only one
|
|
|
|
* notification can be attached to a message queue.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2012-07-14 21:30:31 +02:00
|
|
|
* If "notification" is NULL, the attached notification is detached (if
|
|
|
|
* it was held by the calling task) and the queue is available to attach
|
|
|
|
* another notification.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2012-07-14 21:30:31 +02:00
|
|
|
* When the notification is sent to the registered process, its
|
|
|
|
* registration will be removed. The message queue will then be
|
|
|
|
* available for registration.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-02-18 00:21:28 +01:00
|
|
|
* mqdes - Message queue descriptor
|
|
|
|
* notification - Real-time signal structure containing:
|
2015-12-30 20:20:31 +01:00
|
|
|
* sigev_notify - Should be SIGEV_SIGNAL or SIGEV_THREAD
|
2007-02-18 00:21:28 +01:00
|
|
|
* sigev_signo - The signo to use for the notification
|
|
|
|
* sigev_value - Value associated with the signal
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-12-05 20:59:10 +01:00
|
|
|
* On success mq_notify() returns 0; on error, -1 is returned, with
|
|
|
|
* errno set to indicate the error.
|
2011-12-04 19:08:33 +01:00
|
|
|
*
|
2014-04-13 22:32:20 +02:00
|
|
|
* EBADF The descriptor specified in mqdes is invalid.
|
2011-12-05 20:59:10 +01:00
|
|
|
* EBUSY Another process has already registered to receive notification
|
2014-04-13 22:32:20 +02:00
|
|
|
* for this message queue.
|
2011-12-05 20:59:10 +01:00
|
|
|
* EINVAL sevp->sigev_notify is not one of the permitted values; or
|
|
|
|
* sevp->sigev_notify is SIGEV_SIGNAL and sevp->sigev_signo is not a
|
2014-04-13 22:32:20 +02:00
|
|
|
* valid signal number.
|
2011-12-05 20:59:10 +01:00
|
|
|
* ENOMEM
|
|
|
|
* Insufficient memory.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
* POSIX Compatibility:
|
|
|
|
* int mq_notify(mqd_t mqdes, const struct sigevent *notification);
|
|
|
|
*
|
|
|
|
* The notification will be sent to the registered task even if another
|
|
|
|
* task is waiting for the message queue to become non-empty. This is
|
|
|
|
* inconsistent with the POSIX specification which says, "If a process
|
|
|
|
* has registered for notification of message a arrival at a message
|
2017-10-10 17:57:40 +02:00
|
|
|
* queue and some process is blocked in [nx]mq_receive() waiting to receive
|
2007-02-18 00:21:28 +01:00
|
|
|
* a message when a message arrives at the queue, the arriving message
|
2017-10-10 17:57:40 +02:00
|
|
|
* message shall satisfy [nx]mq_receive()... The resulting behavior is as
|
|
|
|
* if the message queue remains empty, and no notification shall be sent."
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-07 00:44:41 +01:00
|
|
|
int mq_notify(mqd_t mqdes, FAR const struct sigevent *notification)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2016-02-07 00:44:41 +01:00
|
|
|
FAR struct mqueue_inode_s *msgq;
|
2020-12-22 15:02:48 +01:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct file *filep;
|
|
|
|
FAR struct tcb_s *rtcb;
|
2013-02-04 19:46:28 +01:00
|
|
|
int errval;
|
2011-12-04 19:08:33 +01:00
|
|
|
|
2020-12-22 15:02:48 +01:00
|
|
|
errval = fs_getfilep(mqdes, &filep);
|
|
|
|
if (errval < 0)
|
|
|
|
{
|
|
|
|
goto errout_without_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2011-12-04 19:08:33 +01:00
|
|
|
/* Was a valid message queue descriptor provided? */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-07-23 11:30:05 +02:00
|
|
|
if (!inode || !inode->i_private)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2011-12-04 19:08:33 +01:00
|
|
|
/* No.. return EBADF */
|
|
|
|
|
|
|
|
errval = EBADF;
|
2020-12-22 15:02:48 +01:00
|
|
|
goto errout_without_lock;
|
2011-12-04 19:08:33 +01:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-12-04 19:08:33 +01:00
|
|
|
/* Get a pointer to the message queue */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-12-04 19:08:33 +01:00
|
|
|
sched_lock();
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-12-04 19:08:33 +01:00
|
|
|
/* Get the current process ID */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-07 00:44:41 +01:00
|
|
|
rtcb = this_task();
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-12-04 19:08:33 +01:00
|
|
|
/* Is there already a notification attached */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-07-23 11:30:05 +02:00
|
|
|
msgq = inode->i_private;
|
2020-12-22 15:02:48 +01:00
|
|
|
if (msgq->ntpid == INVALID_PROCESS_ID)
|
2011-12-04 19:08:33 +01:00
|
|
|
{
|
|
|
|
/* No... Have we been asked to establish one? */
|
|
|
|
|
|
|
|
if (notification)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2011-12-04 19:08:33 +01:00
|
|
|
/* Yes... Was a valid signal number supplied? */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-12-04 19:08:33 +01:00
|
|
|
if (!GOOD_SIGNO(notification->sigev_signo))
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2011-12-04 19:08:33 +01:00
|
|
|
/* No... Return EINVAL */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-12-04 19:08:33 +01:00
|
|
|
errval = EINVAL;
|
|
|
|
goto errout;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2011-12-04 19:08:33 +01:00
|
|
|
|
|
|
|
/* Yes... Assign it to the current task. */
|
|
|
|
|
2018-07-30 14:50:04 +02:00
|
|
|
memcpy(&msgq->ntevent, notification,
|
2015-12-30 20:20:31 +01:00
|
|
|
sizeof(struct sigevent));
|
|
|
|
|
2020-12-22 15:02:48 +01:00
|
|
|
msgq->ntpid = rtcb->pid;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2011-12-04 19:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes... a notification is attached. Does this task own it?
|
|
|
|
* Is it trying to remove it?
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-03-21 23:47:09 +01:00
|
|
|
else if ((msgq->ntpid != rtcb->pid) || (notification != NULL))
|
2011-12-04 19:08:33 +01:00
|
|
|
{
|
|
|
|
/* This thread does not own the notification OR it is
|
|
|
|
* not trying to remove it. Return EBUSY.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2011-12-04 19:08:33 +01:00
|
|
|
errval = EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes, the notification belongs to this thread. Allow the
|
|
|
|
* thread to detach the notification.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-12-30 20:20:31 +01:00
|
|
|
memset(&msgq->ntevent, 0, sizeof(struct sigevent));
|
2020-12-22 15:02:48 +01:00
|
|
|
msgq->ntpid = INVALID_PROCESS_ID;
|
2019-01-27 16:28:59 +01:00
|
|
|
nxsig_cancel_notification(&msgq->ntwork);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2011-12-04 19:08:33 +01:00
|
|
|
sched_unlock();
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
sched_unlock();
|
2020-12-22 15:02:48 +01:00
|
|
|
|
|
|
|
errout_without_lock:
|
|
|
|
set_errno(errval);
|
2011-12-04 19:08:33 +01:00
|
|
|
return ERROR;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|