core spinlock wait returns immediately

Andrei Dimitrief-Jianu andrei.dimitrief.jianu at gmail.com
Thu Oct 11 23:35:13 UTC 2012


On Thu, Oct 11, 2012 at 6:37 PM, Joel Sherrill
<joel.sherrill at oarcorp.com> wrote:
> On 10/11/2012 04:56 PM, Gedare Bloom wrote:
>>
>> Are you doing this at user-level? I don't think core spinlock part of
>> the public API...
>
> It is promoted out through pthread_spin*
>
> There are tests for it. If it isn't behaving right, then the
> tests are malformed.
>
> Post a complete and simple test case and we can look at
> it.
>>
>> I haven't looked closely at the code to tell if it supports using the
>> attributes the way you suggest, so I don't know the answer to your
>> question.
>>
>> -Gedare
>>
>> On Thu, Oct 11, 2012 at 4:46 PM, Andrei Dimitrief-Jianu
>> <andrei.dimitrief.jianu at gmail.com>  wrote:
>>>
>>> On Thu, Oct 11, 2012 at 9:58 AM, Gedare Bloom<gedare at rtems.org>  wrote:
>>>>
>>>> Is the spinlock already locked? if not it would return immediately
>>>>
>>>> Did the thread that's attempting to gain the lock previuosly lock it?
>>>> if so it would return immediately
>>>>
>>>> -Gedare
>>>>
>>>> On Wed, Oct 10, 2012 at 11:59 PM, Andrei Dimitrief-Jianu
>>>> <andrei.dimitrief.jianu at gmail.com>  wrote:
>>>>>
>>>>> Hello,
>>>>>
>>>>> I initialize a core spinlock using the code below:
>>>>>
>>>>>    CORE_spinlock_Attributes spinlock_attributes;
>>>>>    spinlock_attributes.XXX = 0;
>>>>>    _CORE_spinlock_Initialize(&spinlock,&spinlock_attributes );
>>>>>
>>>>>
>>>>> When calling wait() on the same spinlock from a worker task using the
>>>>> code below, the task returns w/o waiting for the spinlock to be
>>>>> released.
>>>>>
>>>>>    CORE_spinlock_Status spinlock_status;
>>>>>    spinlock_status = _CORE_spinlock_Wait(&spinlock, true, 0 );
>>>>>
>>>>>
>>>>>
>>>>> Anything that is obvious that I am doing wrong?
>>>>>
>>>>> Thanks!
>>>>> _______________________________________________
>>>>> rtems-devel mailing list
>>>>> rtems-devel at rtems.org
>>>>> http://www.rtems.org/mailman/listinfo/rtems-devel
>>>
>>>
>>> I need to have a worker task waiting on the spinlock until
>>> the main task is releasing the lock or the spinlock times out.
>>>
>>>
>>> The CORE_spinlock exposes 3 functions:
>>>
>>> void _CORE_spinlock_Initialize(
>>>    CORE_spinlock_Control       *the_spinlock,
>>>    CORE_spinlock_Attributes    *the_spinlock_attributes
>>> );
>>>
>>> CORE_spinlock_Status _CORE_spinlock_Wait(
>>>    CORE_spinlock_Control  *the_spinlock,
>>>    bool                    wait,
>>>    Watchdog_Interval       timeout
>>> );
>>>
>>> CORE_spinlock_Status _CORE_spinlock_Release(
>>>    CORE_spinlock_Control *the_spinlock
>>> );
>>>
>>>
>>> I am assuming that I can initialize the spinlock as locked or
>>> unlocked by passing CORE_SPINLOCK_LOCKED or CORE_SPINLOCK_UNLOCKED
>>> as attributes to _CORE_spinlock_Initialize().
>>>
>>> Once the spinlock is initialized as locked by the main task, I can
>>> call _CORE_spinlock_Wait() from the worker task to block until
>>> _CORE_spinlock_Release() is called by the main task or the call times
>>> out.
>>>
>>>
>>> The problem that I have is that the worker task does not block. I am not
>>> sure that the approach described above is the right one.
>>>
>>> Thanks!
>>
>> _______________________________________________
>> rtems-devel mailing list
>> rtems-devel at rtems.org
>> http://www.rtems.org/mailman/listinfo/rtems-devel
>
>
>
> --
> Joel Sherrill, Ph.D.             Director of Research&  Development
> joel.sherrill at OARcorp.com        On-Line Applications Research
> Ask me about RTEMS: a free RTOS  Huntsville AL 35806
> Support Available               (256) 722-9985
>


I intended the code as a test, just to make sure I got the API description from
the header file right. I do not want to call directly the core objects
from the userland. I understand that the POSIX spinlock is using the
core spinlock.



#include <bsp.h>

#include <stdlib.h>
#include <stdio.h>

#include "config.h"
#include <rtems/confdefs.h>


#include <rtems/score/corespinlock.h>
CORE_spinlock_Control spinlock;


rtems_task worker_task( rtems_task_argument argument );

rtems_task
Init( rtems_task_argument ignored )
{
  rtems_id  task_id;
  rtems_status_code status_code;
  rtems_name task_name;

  // initialize the core spinlock
  CORE_spinlock_Attributes spinlock_attributes;
  spinlock_attributes.XXX = CORE_SPINLOCK_LOCKED;
  _CORE_spinlock_Initialize( &spinlock, &spinlock_attributes );
  printf( "\nmain task initialized spinlock...\n" );

  // start a worker task
  task_name = rtems_build_name( 'T', 'S', 'K', '1' );

  status_code = rtems_task_create(
    task_name, 1, RTEMS_MINIMUM_STACK_SIZE,
    RTEMS_NO_PREEMPT, RTEMS_FLOATING_POINT, &task_id );
  if( RTEMS_SUCCESSFUL != status_code )
  {
    printf( "\nrtems_task_create failed with status of %d.\n", status_code );
    exit( 1 );
  }

  status_code = rtems_task_start( task_id, worker_task, 1 );
  if( RTEMS_SUCCESSFUL != status_code )
  {
    printf( "\nrtems_task_start failed with status of %d.\n", status_code );
    exit( 1 );
  }
  printf( "\nmain task started worker task...\n" );

  rtems_task_wake_after( RTEMS_MILLISECONDS_TO_TICKS( 30000 ) );

  // release any task waiting on the spinlock
  printf( "\nmain task releasing the spinlock...\n" );
  CORE_spinlock_Status spinlock_status;
  spinlock_status = _CORE_spinlock_Release( &spinlock );
  if( CORE_SPINLOCK_SUCCESSFUL != spinlock_status )
  {
    printf( "\n_CORE_spinlock_Release() failed with status of %d.\n",
spinlock_status );
    exit( 1 );
  }
  printf( "\nmain task released the spinlock...\n" );

  rtems_task_wake_after( RTEMS_MILLISECONDS_TO_TICKS( 30000 ) );

  printf( "\nmain task is exiting...\n" );

  exit( 0 );
}

rtems_task
worker_task( rtems_task_argument argument )
{
  int index = 0;
  while( index++ < 10 * argument )
  {
    printf( "\ntask [%d] > doing something in the user task [%d].\n",
(uint16_t)argument, index );
    rtems_task_wake_after( RTEMS_MILLISECONDS_TO_TICKS( 1000 ) );
  }

  printf( "\ntask [%d] > waiting for the spinlock...\n", (uint16_t)argument );
  CORE_spinlock_Status spinlock_status;
  spinlock_status = _CORE_spinlock_Wait( &spinlock, true, 0 );
  if( CORE_SPINLOCK_SUCCESSFUL != spinlock_status )
  {
    printf( "\n_CORE_spinlock_Wait() failed with status of %d.\n",
spinlock_status );
  }
  printf( "\ntask [%d] > the spinlock was released...\n", (uint16_t)argument );

  printf( "\ntask [%d] is exiting...\n", (uint16_t)argument );
  rtems_task_delete( rtems_task_self() );

}



More information about the devel mailing list