@@ -349,7 +349,7 @@ int hal_exit(int comp_id)
349349 -- ref_cnt ;
350350#ifdef ULAPI
351351 if (ref_cnt == 0 ) {
352- rtapi_print_msg (RTAPI_MSG_DBG , "HAL: releasing RTAPI resources" );
352+ rtapi_print_msg (RTAPI_MSG_DBG , "HAL: releasing RTAPI resources\n " );
353353 /* release RTAPI resources */
354354 rtapi_shmem_delete (lib_mem_id , lib_module_id );
355355 rtapi_exit (lib_module_id );
@@ -2781,19 +2781,29 @@ static void thread_task(void *arg)
27812781
27822782static int init_hal_data (void )
27832783{
2784- /* has the block already been initialized? */
2784+ /* has the hal_data block already been initialized? */
2785+
2786+ /* Lock hal_data by taking the mutex, so that two processes
2787+ don't both try to initialize hal_data at the same time. NOTE:
2788+ The first time through, the hal_data memory buffer is fresh from
2789+ rtapi_shmem_new(), which means it's initialized to all zero bytes.
2790+ This means hal_data->mutex is valid and unlocked. */
2791+ rtapi_mutex_get (& (hal_data -> mutex ));
2792+
27852793 if (hal_data -> version != 0 ) {
2786- /* yes, verify version code */
2787- if (hal_data -> version == HAL_VER ) {
2788- return 0 ;
2789- } else {
2790- rtapi_print_msg (RTAPI_MSG_ERR ,
2791- "HAL: ERROR: version code mismatch\n" );
2792- return -1 ;
2793- }
2794+ /* hal_data has been initialized already, verify version code */
2795+ if (hal_data -> version == HAL_VER ) {
2796+ rtapi_mutex_give (& (hal_data -> mutex ));
2797+ return 0 ;
2798+ } else {
2799+ rtapi_print_msg (RTAPI_MSG_ERR , "HAL: ERROR: version code mismatch\n" );
2800+ rtapi_mutex_give (& (hal_data -> mutex ));
2801+ return -1 ;
2802+ }
27942803 }
2795- /* no, we need to init it, grab the mutex unconditionally */
2796- rtapi_mutex_try (& (hal_data -> mutex ));
2804+
2805+ /* hal_data has *NOT* been initialized yet, we get the honor */
2806+
27972807 /* set version code so nobody else init's the block */
27982808 hal_data -> version = HAL_VER ;
27992809 /* initialize everything */
0 commit comments