2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2014-08-09 00:57:47 +02:00
|
|
|
* sched/task/task_getpid.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +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
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +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
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sched.h>
|
2018-12-30 18:58:19 +01:00
|
|
|
#include <errno.h>
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-09 00:57:47 +02:00
|
|
|
#include "task/task.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
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Name: getpid
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get the task ID of the currently executing task.
|
|
|
|
*
|
2018-12-30 15:41:06 +01:00
|
|
|
* Input parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Normally when called from user applications, getpid() will return the
|
|
|
|
* task ID of the currently executing task, that is, the task at the head
|
|
|
|
* of the ready-to-run list. There is no specification for any errors
|
|
|
|
* returned from getpid().
|
|
|
|
*
|
|
|
|
* getpid(), however, may be called from within the OS in some cases.
|
|
|
|
* There are certain situations during context switching when the OS data
|
|
|
|
* structures are in flux and where the current task at the head of the
|
2018-12-30 18:58:19 +01:00
|
|
|
* ready-to-run task list is not actually running. In that case,
|
2018-12-30 15:41:06 +01:00
|
|
|
* getpid() will return the error: -ESRCH
|
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
pid_t getpid(void)
|
|
|
|
{
|
2014-04-10 18:20:44 +02:00
|
|
|
FAR struct tcb_s *rtcb;
|
|
|
|
|
2017-05-11 21:35:56 +02:00
|
|
|
/* Get the TCB at the head of the ready-to-run task list. That
|
2018-12-30 15:41:06 +01:00
|
|
|
* will usually be the currently executing task. There is are two
|
|
|
|
* exceptions to this:
|
|
|
|
*
|
|
|
|
* 1. Early in the start-up sequence, the ready-to-run list may be
|
2018-12-30 18:58:19 +01:00
|
|
|
* empty! In this case, of course, the CPU0 start-up/IDLE thread with
|
2018-12-30 15:41:06 +01:00
|
|
|
* pid == 0 must be running, and
|
|
|
|
* 2. As described above, during certain context-switching conditions the
|
|
|
|
* task at the head of the ready-to-run list may not actually be
|
|
|
|
* running.
|
2014-04-10 18:20:44 +02:00
|
|
|
*/
|
|
|
|
|
2016-02-07 00:44:41 +01:00
|
|
|
rtcb = this_task();
|
2018-12-30 15:41:06 +01:00
|
|
|
if (rtcb != NULL)
|
2014-04-10 18:20:44 +02:00
|
|
|
{
|
2018-12-30 15:41:06 +01:00
|
|
|
/* Check if the task is actually running */
|
|
|
|
|
|
|
|
if (rtcb->task_state == TSTATE_TASK_RUNNING)
|
|
|
|
{
|
|
|
|
/* Yes.. Return the task ID from the TCB at the head of the
|
|
|
|
* ready-to-run task list
|
|
|
|
*/
|
|
|
|
|
|
|
|
return rtcb->pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. return -ESRCH to indicate this condition */
|
2014-04-10 18:20:44 +02:00
|
|
|
|
2018-12-30 15:41:06 +01:00
|
|
|
return (pid_t)-ESRCH;
|
2014-04-10 18:20:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We must have been called earlier in the start up sequence from the
|
2016-02-10 23:53:34 +01:00
|
|
|
* start-up/IDLE thread before the ready-to-run list has been initialized.
|
2012-07-14 21:30:31 +02:00
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-03-21 23:47:09 +01:00
|
|
|
return IDLE_PROCESS_ID;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|