[PATCH] pthreadgetcpuclockid.c: Implement pthread_getcpuclockid to return thread Id

Gedare Bloom gedare at rtems.org
Sun Apr 5 15:06:17 UTC 2020


Hi Joel,

Something about the critical section logic here bugs me, see my crude
analysis below:

On Thu, Mar 12, 2020 at 2:00 PM Joel Sherrill <joel at rtems.org> wrote:
>
> ---
>  cpukit/posix/src/pthreadgetcpuclockid.c | 31 ++++++++++++++++++++++++++-----
>  1 file changed, 26 insertions(+), 5 deletions(-)
>
> diff --git a/cpukit/posix/src/pthreadgetcpuclockid.c b/cpukit/posix/src/pthreadgetcpuclockid.c
> index cd7814b..c15962e 100644
> --- a/cpukit/posix/src/pthreadgetcpuclockid.c
> +++ b/cpukit/posix/src/pthreadgetcpuclockid.c
> @@ -7,8 +7,10 @@
>
>  /*
>   *  20.1.6 Accessing a Thread CPU-time Clock, P1003.4b/Draft 8, p. 58
> - *
> - *  COPYRIGHT (c) 1989-2007.
> + */
> +
> +/*
> + *  COPYRIGHT (c) 1989-2007,2020.
>   *  On-Line Applications Research Corporation (OAR).
>   *
>   *  The license and distribution terms for this file may be
> @@ -23,12 +25,31 @@
>  #include <pthread.h>
>  #include <errno.h>
>
> -#include <rtems/seterr.h>
> +#include <rtems/score/threadimpl.h>
>
>  int pthread_getcpuclockid(
> -  pthread_t    pid,
> +  pthread_t    thread,
>    clockid_t   *clock_id
>  )
>  {
> -  rtems_set_errno_and_return_minus_one( ENOSYS );
> +  Thread_Control               *the_thread;
> +  ISR_lock_Context              lock_context;
> +
> +  if ( clock_id == NULL ) {
> +    return EINVAL;
> +  }
> +
> +  the_thread = _Thread_Get( thread, &lock_context );
> +
This lock_context now holds an ISR lock

> +  if ( the_thread == NULL ) {
> +    return ESRCH;
> +  }
> +
> +  _Thread_State_acquire_critical( the_thread, &lock_context );
> +
In SMP the lock_context is now overwritten by the thread's TQ lock.

> +  *clock_id = the_thread->Object.id;
> +
> +  _Thread_State_release( the_thread, &lock_context );
Releasing the TQ lock

> +
> +  return 0;
Leaking the ISR lock, returning with ISRs disabled.

>  }
> --
> 1.8.3.1
>
> _______________________________________________
> devel mailing list
> devel at rtems.org
> http://lists.rtems.org/mailman/listinfo/devel


More information about the devel mailing list