Changeset 0b1be9f in rtems-libbsd


Ignore:
Timestamp:
Oct 24, 2013, 3:37:50 PM (6 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
4.11, afaeccc05a556f6aa25ba044a7e49d6aa634a59e, freebsd-9.3, master
Children:
4adeb59
Parents:
761956d
git-author:
Sebastian Huber <sebastian.huber@…> (10/24/13 15:37:50)
git-committer:
Sebastian Huber <sebastian.huber@…> (10/31/13 12:18:51)
Message:

RWLOCK(9): Use RTEMS mutex to support recursion

Support for recursive RWLOCK(9) is mandatory otherwise dead-lock happens
in the TCP protocol layer.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rtemsbsd/rtems/rtems-bsd-rwlock.c

    r761956d r0b1be9f  
    1515 *  USA
    1616 *  <kevin.kirspel@optimedical.com>
     17 *
     18 * Copyright (c) 2013 embedded brains GmbH.  All rights reserved.
     19 *
     20 *  embedded brains GmbH
     21 *  Dornierstr. 4
     22 *  82178 Puchheim
     23 *  Germany
     24 *  <rtems@embedded-brains.de>
    1725 *
    1826 * Redistribution and use in source and binary forms, with or without
     
    3947
    4048#include <machine/rtems-bsd-config.h>
     49#include <machine/rtems-bsd-thread.h>
    4150#include <machine/rtems-bsd-support.h>
    4251
    43 #include <rtems/score/objectimpl.h>
    44 #include <rtems/posix/rwlockimpl.h>
     52#include <rtems.h>
    4553
    4654#include <rtems/bsd/sys/param.h>
     
    4957#include <rtems/bsd/sys/lock.h>
    5058#include <sys/rwlock.h>
     59#include <sys/mutex.h>
    5160
    5261#ifndef INVARIANTS
     
    129138  struct lock_class *class;
    130139  int i;
    131   pthread_rwlock_t lock;
    132   int iret;
     140  rtems_status_code sc;
     141  rtems_id id;
     142  rtems_attribute attr = RTEMS_LOCAL | RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY | RTEMS_INHERIT_PRIORITY;
    133143
    134144  if ((opts & RW_RECURSE) != 0) {
     
    152162  KASSERT(i < LOCK_CLASS_MAX, ("unknown lock class %p", class));
    153163
    154   iret = pthread_rwlock_init( &lock, NULL );
    155   BSD_ASSERT( iret == 0 );
     164  sc = rtems_semaphore_create(
     165    rtems_build_name('_', '_', 'R', 'W'),
     166    1,
     167    attr,
     168    BSD_TASK_PRIORITY_RESOURCE_OWNER,
     169    &id
     170  );
     171  BSD_ASSERT_SC(sc);
    156172
    157173  rw->lock_object.lo_name = name;
    158174  rw->lock_object.lo_flags |= LO_INITIALIZED;
    159   rw->lock_object.lo_id = lock;
     175  rw->lock_object.lo_id = id;
    160176
    161177  rtems_chain_append(&rtems_bsd_rwlock_chain, &rw->lock_object.lo_node);
     
    165181rw_destroy(struct rwlock *rw)
    166182{
    167   int iret;
    168   pthread_rwlock_destroy( rw->lock_object.lo_id );
    169   BSD_ASSERT( iret == 0 );
    170   rtems_chain_extract( &rw->lock_object.lo_node );
    171   rw->lock_object.lo_id = 0;
    172   rw->lock_object.lo_flags &= ~LO_INITIALIZED;
     183  mtx_destroy((struct mtx *) rw);
    173184}
    174185
     
    189200}
    190201
    191 /* XXX add pthread_rwlock_is_wlocked_np( id, &wlocked )
    192  * XXX    returns 0 or -1 w/error
    193  * XXX    wlocked = 1 if write locked
    194  * XXX
    195 /* XXX add pthread_rwlock_is_rlocked_np( id, &wlocked )
    196  * XXX    similar behavior
    197  * XXX probably want to add "unlocked" state to RTEMS SuperCore rwlock
    198  * XXX
    199  * XXX Rationale: This violates the API layering BADLY!!!!!
    200  * XXX Consider: Adding pthread_np.h to hold np methods like FreeBSD
    201  * XXX           This would avoid polluting pthread.h
    202  */
    203202int
    204203rw_wowned(struct rwlock *rw)
    205204{
    206   int                   is_locked_for_write = 0;
    207   Objects_Locations     location;
    208   POSIX_RWLock_Control *the_rwlock;
    209 
    210   the_rwlock = _POSIX_RWLock_Get(&rw->lock_object.lo_id, &location);
    211   switch ( location ) {
    212 
    213     case OBJECTS_LOCAL:
    214       if (the_rwlock->RWLock.current_state == CORE_RWLOCK_LOCKED_FOR_WRITING)
    215         is_locked_for_write = 1;
    216       _Thread_Enable_dispatch();
    217       return is_locked_for_write;
    218 
    219 #if defined(RTEMS_MULTIPROCESSING)
    220     case OBJECTS_REMOTE:
    221 #endif
    222     case OBJECTS_ERROR:
    223       break;
    224   }
    225   _Thread_Enable_dispatch();
    226 
    227   BSD_PANIC("unexpected semaphore location or attributes");
     205  return mtx_owned((struct mtx *) rw);
    228206}
    229207
     
    231209_rw_wlock(struct rwlock *rw, const char *file, int line)
    232210{
    233   int iret;
    234 
    235   iret = pthread_rwlock_wrlock( &rw->lock_object.lo_id );
    236   BSD_ASSERT( iret == 0 );
    237 
    238   return 0;
     211  _mtx_lock_flags((struct mtx *) rw, 0, file, line);
    239212}
    240213
     
    242215_rw_try_wlock(struct rwlock *rw, const char *file, int line)
    243216{
    244   int iret;
    245 
    246   iret = pthread_rwlock_trywrlock( &rw->lock_object.lo_id );
    247   if (iret == 0) {
    248     return 1;
    249   } else {
    250     return 0;
    251   }
     217  return _mtx_trylock((struct mtx *) rw, 0, file, line);
    252218}
    253219
     
    255221_rw_wunlock(struct rwlock *rw, const char *file, int line)
    256222{
    257   int iret;
    258 
    259   iret = pthread_rwlock_unlock( &rw->lock_object.lo_id );
    260   BSD_ASSERT( iret == 0 );
     223  _mtx_unlock_flags((struct mtx *) rw, 0, file, line);
    261224}
    262225
     
    264227_rw_rlock(struct rwlock *rw, const char *file, int line)
    265228{
    266   int iret;
    267 
    268   iret = pthread_rwlock_rdlock( &rw->lock_object.lo_id );
    269   BSD_ASSERT( iret == 0 );
     229  _mtx_lock_flags((struct mtx *) rw, 0, file, line);
    270230}
    271231
     
    273233_rw_try_rlock(struct rwlock *rw, const char *file, int line)
    274234{
    275   int iret;
    276 
    277   iret = pthread_rwlock_tryrdlock( &rw->lock_object.lo_id );
    278   if (iret == 0) {
    279     return 1;
    280   } else {
    281     return 0;
    282   }
     235  return _mtx_trylock((struct mtx *) rw, 0, file, line);
    283236}
    284237
     
    286239_rw_runlock(struct rwlock *rw, const char *file, int line)
    287240{
    288   int iret;
    289 
    290   iret = pthread_rwlock_unlock( &rw->lock_object.lo_id );
    291   BSD_ASSERT( iret == 0 );
    292 }
    293 
    294 /*
    295  * Attempt to do a non-blocking upgrade from a read lock to a write
    296  * lock.  This will only succeed if this thread holds a single read
    297  * lock.  Returns true if the upgrade succeeded and false otherwise.
    298  */
     241  _mtx_unlock_flags((struct mtx *) rw, 0, file, line);
     242}
     243
    299244int
    300245_rw_try_upgrade(struct rwlock *rw, const char *file, int line)
    301246{
    302   return 0; /* XXX */
    303 }
    304 
    305 /*
    306  * Downgrade a write lock into a single read lock.
    307  */
     247  return 1;
     248}
     249
    308250void
    309251_rw_downgrade(struct rwlock *rw, const char *file, int line)
    310252{
    311   /* XXX */
     253  /* Nothing to do */
    312254}
    313255
Note: See TracChangeset for help on using the changeset viewer.