Changeset 9ace2648 in rtems
- Timestamp:
- 02/08/18 08:37:25 (5 years ago)
- Branches:
- 5, master
- Children:
- a49dfb1
- Parents:
- abc2164
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpukit/libmisc/rtems-fdt/rtems-fdt.c
rabc2164 r9ace2648 18 18 19 19 #include <rtems/rtems-fdt.h> 20 #include <rtems/ libio_.h>20 #include <rtems/thread.h> 21 21 22 22 /** … … 58 58 typedef struct 59 59 { 60 rtems_ idlock; /**< The FDT lock id */60 rtems_mutex lock; /**< The FDT lock id */ 61 61 rtems_chain_control blobs; /**< List if loaded blobs. */ 62 62 const char* paths; /**< Search paths for blobs. */ 63 63 } rtems_fdt_data; 64 64 65 /** 66 * Semaphore configuration to create a mutex. 67 */ 68 #define RTEMS_MUTEX_ATTRIBS \ 69 (RTEMS_PRIORITY | RTEMS_BINARY_SEMAPHORE | \ 70 RTEMS_INHERIT_PRIORITY | RTEMS_NO_PRIORITY_CEILING | RTEMS_LOCAL) 71 72 /** 73 * The FDT data. 74 */ 75 static rtems_fdt_data* fdt_data; 76 77 static bool 78 rtems_fdt_unlock (void) 79 { 80 /* 81 * Not sure any error should be returned or an assert. 82 */ 83 rtems_status_code sc; 84 sc = rtems_semaphore_release (fdt_data->lock); 85 if ((sc != RTEMS_SUCCESSFUL) && (errno == 0)) 86 { 87 errno = EINVAL; 88 return false; 89 } 90 return true; 91 } 92 93 static bool 94 rtems_fdt_data_init (void) 95 { 96 /* 97 * Lock the FDT. We only create a lock if a call is made. First we test if a 98 * lock is present. If one is present we lock it. If not the libio lock is 99 * locked and we then test the lock again. If not present we create the lock 100 * then release libio lock. 101 */ 102 if (!fdt_data) 103 { 104 rtems_libio_lock (); 105 106 if (!fdt_data) 107 { 108 rtems_status_code sc; 109 rtems_id lock; 110 111 /* 112 * Always in the heap. 113 */ 114 fdt_data = malloc (sizeof (rtems_fdt_data)); 115 if (!fdt_data) 116 { 117 errno = ENOMEM; 118 return false; 119 } 120 121 *fdt_data = (rtems_fdt_data) { 0 }; 122 123 /* 124 * Create the FDT lock. 125 */ 126 sc = rtems_semaphore_create (rtems_build_name ('F', 'D', 'T', ' '), 127 1, RTEMS_MUTEX_ATTRIBS, 128 RTEMS_NO_PRIORITY, &lock); 129 if (sc != RTEMS_SUCCESSFUL) 130 { 131 free (fdt_data); 132 return false; 133 } 134 135 sc = rtems_semaphore_obtain (lock, RTEMS_WAIT, RTEMS_NO_TIMEOUT); 136 if (sc != RTEMS_SUCCESSFUL) 137 { 138 rtems_semaphore_delete (lock); 139 free (fdt_data); 140 return false; 141 } 142 143 fdt_data->lock = lock; 144 145 /* 146 * Initialise the blob list. 147 */ 148 rtems_chain_initialize_empty (&fdt_data->blobs); 149 } 150 151 rtems_libio_unlock (); 152 153 rtems_fdt_unlock (); 154 } 155 156 return true; 65 static void 66 rtems_fdt_unlock (rtems_fdt_data *fdt) 67 { 68 rtems_mutex_unlock (&fdt->lock); 157 69 } 158 70 … … 160 72 rtems_fdt_lock (void) 161 73 { 162 rtems_status_code sc; 163 164 if (!rtems_fdt_data_init ()) 165 return NULL; 166 167 sc = rtems_semaphore_obtain (fdt_data->lock, 168 RTEMS_WAIT, RTEMS_NO_TIMEOUT); 169 if (sc != RTEMS_SUCCESSFUL) 170 { 171 errno = EINVAL; 172 return NULL; 173 } 174 175 return fdt_data; 74 static rtems_fdt_data fdt_instance = { 75 .lock = RTEMS_MUTEX_INITIALIZER ("FDT"), 76 .blobs = RTEMS_CHAIN_INITIALIZER_EMPTY (fdt_instance.blobs) 77 }; 78 rtems_fdt_data *fdt = &fdt_instance; 79 80 rtems_mutex_lock (&fdt->lock); 81 return fdt; 176 82 } 177 83 … … 439 345 if (from && to) 440 346 { 441 (void) rtems_fdt_lock (); 347 rtems_fdt_data* fdt; 348 349 fdt = rtems_fdt_lock (); 442 350 to->blob = from->blob; 443 351 ++to->blob->refs; 444 rtems_fdt_unlock ( );352 rtems_fdt_unlock (fdt); 445 353 } 446 354 } … … 470 378 } 471 379 472 rtems_fdt_unlock ( );380 rtems_fdt_unlock (fdt); 473 381 474 382 handle->blob = NULL; … … 493 401 if (handle->blob == blob) 494 402 { 495 rtems_fdt_unlock ( );403 rtems_fdt_unlock (fdt); 496 404 return true; 497 405 } … … 499 407 } 500 408 501 rtems_fdt_unlock ( );409 rtems_fdt_unlock (fdt); 502 410 } 503 411 … … 530 438 ++temp_handle.blob->refs; 531 439 handle->blob = temp_handle.blob; 532 rtems_fdt_unlock ( );440 rtems_fdt_unlock (fdt); 533 441 return offset; 534 442 } … … 537 445 } 538 446 539 rtems_fdt_unlock ( );447 rtems_fdt_unlock (fdt); 540 448 541 449 return -FDT_ERR_NOTFOUND; … … 694 602 blob->refs = 1; 695 603 696 rtems_fdt_unlock ( );604 rtems_fdt_unlock (fdt); 697 605 698 606 handle->blob = blob; … … 738 646 blob->refs = 1; 739 647 740 rtems_fdt_unlock ( );648 rtems_fdt_unlock (fdt); 741 649 742 650 handle->blob = blob; … … 755 663 rtems_fdt_unload (rtems_fdt_handle* handle) 756 664 { 757 (void) rtems_fdt_lock (); 665 rtems_fdt_data* fdt; 666 667 fdt = rtems_fdt_lock (); 758 668 759 669 if (!rtems_fdt_valid_handle (handle)) 760 670 { 761 rtems_fdt_unlock ( );671 rtems_fdt_unlock (fdt); 762 672 return -RTEMS_FDT_ERR_INVALID_HANDLE; 763 673 } … … 765 675 if (handle->blob->refs > 1) 766 676 { 767 rtems_fdt_unlock ( );677 rtems_fdt_unlock (fdt); 768 678 return -RTEMS_FDT_ERR_REFERENCED; 769 679 } … … 775 685 handle->blob = NULL; 776 686 777 rtems_fdt_unlock ( );687 rtems_fdt_unlock (fdt); 778 688 779 689 rtems_fdt_release_index(&handle->blob->index);
Note: See TracChangeset
for help on using the changeset viewer.